当前位置:网站首页>Chapter V: process control
Chapter V: process control
2022-07-25 16:53:00 【Take charge of [email protected]】
The fifth chapter : Process control
5.1、 Branch statement
brief introduction :Java The branch statement of is executed only when part of the program code meets certain conditions .
5.1.1、if else sentence
Basic grammar :
if( Boolean expression ){
Program code block
}else{
Program code block
}
matters needing attention :
- if The following expression must be a Boolean expression , It cannot be of numeric type .
- if hinder else Statement is not necessary .
- Join in if After statement or else The program code block after the statement contains multiple statements , Must be stored in {} Inside . If there is only one sentence in the code block, it can not be used {}
- if else Statements have a special kind of concatenation programming lattice .
public void method(int x){
if(x>0){
System.out.println(" Greater than 0");
}else if(x==0){
System.out.println(" be equal to 0");
}else{
System.out.println(" Less than 0");
}
}
Classic case :
// Judge whether a certain year is a leap year
// Method 1
public boolean idleapYear(int year){
if((year%4==0&&year%100!==0)||(year%400==0))
return true;
else
return false;
}
5.1.2、switch sentence
brief introduction :switch A statement is a multi branch statement , Basic grammar
switch (expr){
case value1:
...
break;
case value2:
...
break;
...
default:
...
break;
}
matters needing attention :
- The parameter value types in the expression include the following
- And int Type compatible types
- String type
- Enumeration type
- switch There can be at most one... In the statement default Clause .
- switch Sentence with that case Matching starts from that place . encounter break Just jump out of the whole switch
- switch Statement can be used if else Statement to implement
Classic case :
public void sortScore(char grade){
switch(grade){
case'A':
System.out.println(grade+"is 85~100");
break;
case'B':
System.out.println(grade+"is 70~84");
break;
case'C':
System.out.println(grade+"is 60~69");
break;
case'D':
System.out.println(grade+"is<60");
break;
default:
System.out.println(" There is no hierarchy ");
break;
}
}
String type parameters
String color="red";
switch(color){
case "blue":
System.out.println(" Blue ");
break;
case "red":
System.out.println(" Red ");
break;
default:
System.out.println(" Other colors ");
break;
}
Enumeration type parameters :
public class SwitchTest{
enum Color{
red,blue,yellow}// Define an enumeration class
public static void main(String [] args){
Color c=Color.blue;
switch(c){
case red:
System.out.println(" Red ");
break;
case blue:
System.out.println(" Blue ");
break;
case yellow:
System.out.println(" yellow ");
break;
default:
System.out.println(" Other colors ");
}
}
}
5.2、 Loop statement
brief introduction : The function of a loop statement is to execute a piece of code repeatedly , Until the conditions are not met .
Four major parts :
- Initialization part : Used to set some initial conditions of the cycle , Set the initial value of the loop control variable
- The loop condition : This is a Boolean expression
- The loop body : This is the main content of the cycle
- Iteration part : To change the value of a loop variable .
5.2.1、while sentence
brief introduction :while Loop statements are the most basic loop statements in loop statements . The basic grammar is as follows :
[ Initialization part ]
while( The loop condition ){
The loop body ( Include iteration part )
}
Precautions for use :
- If the loop body contains multiple statements , Must be placed in braces , If the loop body has only one statement , You don't need braces .
- while The expression is judged at the beginning of the loop , If the return value is false Then the loop body will not be executed once .
- The loop body can be empty
- Ensure that there are conditions for terminating the cycle , Avoid the dead cycle .
Classic case :
public static int max(int [] array){
if(array==null||array.length==0){
throw new IllegalArgumentException(" Invalid array ");
int index=1,location=0;// Initialization part
while(index<array.length){
// The loop condition ,index Is the loop control variable
// The loop body
if(array[location]<array[index]){
location=index;
index++;
}
}
return array[location];
}
}
5.2.2、do while sentence
brief introduction : And while Statements are all circular statements , however do while The loop will first execute the loop body , Then judge the conditions , In any case, the loop body will be executed once .
Grammar format :
[ Initialization part ]
do{
The loop body , Include iteration part
}while( The loop condition );
5.2.3、for sentence
brief introduction :for Circulation and while Like the cycle, we judge the cycle condition first , Then execute the loop body . The grammar is as follows :
for( Initialization part , The loop condition , Iteration part ){
The loop body
}
matters needing attention :
- If for The body of the loop has only one line , May not be used {}
- control for Loop variables are often used only for this loop , It cannot be used elsewhere in the program . Its scope is current for Cycle cannot be in for Use it outside the loop .
- You can use comma statements in the initialization and iteration sections , Comma statements are used to separate statement sequences .
- for The initialization part and iteration part of the loop can be empty
- for Loop statements are generally used when the number of loops can be determined in advance .while Circulation and do while Cycles are used when the number of cycles is not clear .
5.2.4、foreach sentence
brief introduction :foreach The sentence is for A special simplified version of the statement , It can simplify the code of traversing arrays and collections .
Grammar format :
for( Element type Element variables X : Array to be traversed ){
References to variables x Of Java sentence
}
5.2.5、 Multiple cycles
brief introduction : Various loop statements can be nested with each other , Form multiple loops ;
Classic case : multiplication table
public static void main(String[] args){
for (int i = 1; i < 10; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(j+"*"+i+"="+j*i+"\t");
}
System.out.println();
}
}
5.3、 Process jump statement
brief introduction :break、continue、return Statements are all process jump statements , But they have different use conditions and functions
- break: To jump out of switch Or the current cycle .
- continue: Jump out of this cycle , Execute next cycle .
- return: Means to exit this method , Return the method called by the previous layer , If the method is not void, You need to provide the corresponding return value .
版权声明
本文为[Take charge of [email protected]]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/206/202207251650543039.html
边栏推荐
- IaaS基础架构云 —— 云网络
- Chain game development ready-made version chain game system development detailed principle chain game source code delivery
- 城市燃气安全再拉警钟,如何防患于未“燃”?
- Multi tenant software development architecture
- Two methods of importing sqllite table from MySQL
- Fudan University emba2022 graduation season - graduation does not forget the original intention and glory to embark on the journey again
- How to deploy applications on IPFs using 4everland cli
- What are the free low code development platforms?
- 2D semantic segmentation -- deeplabv3plus reproduction
- 【obs】发送前丢帧及帧优先级
猜你喜欢

jenkins的Role-based Authorization Strategy安装配置

How to deploy applications on IPFs using 4everland cli

为什么 4EVERLAND 是 Web 3.0 的最佳云计算平台

Fudan University emba2022 graduation season - graduation does not forget the original intention and glory to embark on the journey again

Data analysis and privacy security become the key factors for the success or failure of Web3.0. How do enterprises layout?

QT listview list display component notes

【目标检测】YOLOv5跑通VOC2007数据集(修复版)

3D semantic segmentation - PVD

EasyUI DataGrid control uses

IaaS基础架构云 —— 云网络
随机推荐
Replicate swin on Huawei ascend910_ transformer
从数字化到智能运维:有哪些价值,又有哪些挑战?
3D semantic segmentation - scribed supervised lidar semantic segmentation
第五章:流程控制
Talk about how to use redis to realize distributed locks?
[mathematical modeling and drawing series tutorial] II. Drawing and optimization of line chart
城市燃气安全再拉警钟,如何防患于未“燃”?
【obs】发送前丢帧及帧优先级
What are the free low code development platforms?
Automatic reply of wechat official account development message
Don't believe these "rumors" in the process of preparing for the exam!
测试框架-unittest-命令行操作、断言方法
C#入门基础教程
谁动了我的内存,揭秘 OOM 崩溃下降 90% 的秘密
自定义mvc项目登录注册和树形菜单
第六章 继承
ReBudget:通过运行时重新分配预算的方法,在基于市场的多核资源分配中权衡效率与公平性
[MySQL] takes you to the database
Enterprise live broadcast: witness focused products, praise and embrace ecology
Homepage portal classification query