table of Contents
Java selection structure
Java loop structure
return、break、continue
1, Java selection structure
1.1, if (condition) ... else structure
- Statement under if executed when the condition == true, otherwise execute statement under else
if(3<=0) System.out.println("3<=0"); else System.out.println("3>0");
1.2, if (condition 1) ... else if (condition 2) ... else structure
- the next statement if (condition 1) is executed when the condition 1 == true
- Statement under else if (condition n) is executed when the condition n == true
- Statement Under else execute when not satisfied
int e = 3; int f = 4; if(e+f>7) System.out.println("a"); else if(e+f==7) System.out.println("b"); else if(e+f<7) System.out.println("c"); else System.out.println("d");
1.3, switch (operation statement) ... Case Value Structure
- when the operation result value == case statement, the statement is executed enter the corresponding case
- default: no matter into which case, as long as there is no break, and finally wants to go to default
- break: out of the current selection structure, enter the case if there break, then directly out switch
1.3.1, no default no break
int a = 5 switch(a - 3)
{ case 1: System.out.println("one"); case 2: System.out.println("two"); case 3: System.out.println("three");
}
1.3.2, there is no break default
int a = 5 switch(a - 3) { case 1: System.out.println("one"); case 2: System.out.println("two"); case 3: System.out.println("three"); default: System.out.println("default"); }
1.3.3、有default有break
int a = 5 switch(a - 3) { case 1: System.out.println("one"); break; case 2: System.out.println("two"); break; case 3: System.out.println("three"); break; default: System.out.println("default"); }
2、Java循环结构
2.1、while(条件)循环,适用于不清楚循环次数,但知道进入循环的条件时
- 当条件==true时,进入循环
int a = 1; while(a<=5) { System.out.println(a); a++; }
2.2、do…while(条件)循环,适用于不清楚循环次数,但知道停止循环的条件时
- 值型循环中的语句,直到条件==false时,跳出该循环
int b = 1; do { System.out.println(b); b++; }while(b<=5);
2.3、for循环,适用于知道循环的次数的情况
- 格式:for(定义一个变量;条件;运算语句){每次循环要执行的语句}
for(int i=0;i<5;i++) { System.out.println(i); }
3、return、break、continue
- return:跳出当前方法,并返回数据(可以不返回)
- break:跳出当前逻辑结构
- continue:跳出本次循环,执行下一次循环(本次循环中continue后面的代码都不执行)