三大流程控制语句:顺序、选择、循环。java
选择结构:express
循环结构:数组
一个if语句包含一个布尔表达式和一条或多条执行语句;code
布尔表达式值为true,执行if 语句;ip
格式:作用域
if(布尔表达式) { //布尔表达式值为true,执行语句; }
布尔表达式值为true,执行 if 语句;it
布尔表达式值为false,执行 else 语句;io
格式:for循环
if(布尔表达式) { //布尔表达式值为true,执行语句; } else { //布尔表达式值为false,执行语句; }
格式:class
if(布尔表达式1) { //布尔表达式1值为true,执行语句; } else if(布尔表达式2) { //布尔表达式2值为true,执行语句; } else if(布尔表达式3) { //布尔表达式值3为true,执行语句; } else { //若是以上全部表达式的值都为false,则执行语句; }
格式:
if(布尔表达式1) { //布尔表达式1值为true,执行语句 if(布尔表达式2) { //布尔表达式2值为true,执行语句 } }
格式:
switch(常量值/expression) { case value1: //执行语句 break; //可选 case value2: //执行语句 break; //可选 ...... default : //执行语句 }
switch语句有以下规则:
if 结构
switch 结构
public class MultiplicationTable { public static void main(String[] args) { for (int i = 1; i <= 9; ++i) { for (int j = 1; j <= 9; j++) { if(j < i) { //输出的空格由"%d * %d = %2d "决定 System.out.print(" "); } else { System.out.printf("%d * %d = %2d ", i ,j , i*j); } } System.out.println(); } } } Output: 1 * 1 = 1 1 * 2 = 2 1 * 3 = 3 1 * 4 = 4 1 * 5 = 5 1 * 6 = 6 1 * 7 = 7 1 * 8 = 8 1 * 9 = 9 2 * 2 = 4 2 * 3 = 6 2 * 4 = 8 2 * 5 = 10 2 * 6 = 12 2 * 7 = 14 2 * 8 = 16 2 * 9 = 18 3 * 3 = 9 3 * 4 = 12 3 * 5 = 15 3 * 6 = 18 3 * 7 = 21 3 * 8 = 24 3 * 9 = 27 4 * 4 = 16 4 * 5 = 20 4 * 6 = 24 4 * 7 = 28 4 * 8 = 32 4 * 9 = 36 5 * 5 = 25 5 * 6 = 30 5 * 7 = 35 5 * 8 = 40 5 * 9 = 45 6 * 6 = 36 6 * 7 = 42 6 * 8 = 48 6 * 9 = 54 7 * 7 = 49 7 * 8 = 56 7 * 9 = 63 8 * 8 = 64 8 * 9 = 72 9 * 9 = 81
public class Days { public static void main(String[] args) { int days = 0; Scanner sc = new Scanner(System.in); System.out.println("请输入要肯定的年份:"); int year = sc.nextInt(); System.out.println("请输入要肯定的月份:"); int month = sc.nextInt(); switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: days = 31; break; case 4: case 6: case 9: case 11: days = 30; break; case 2: if((year %400 == 0) || (year % 100 != 0 && year %4 == 0)) { days = 29; break; } else { days = 28; break; } default: System.out.println("您输入的月份有误!"); System.exit(0); } System.out.printf("%4d 年 %2d 月 共有 %2d 天\n",year,month,days); } } Output: 请输入要肯定的年份: 2018 请输入要肯定的月份: 02 2018 年 2 月 共有 28 天 请输入要肯定的年份: 2008 请输入要肯定的月份: 02 2008 年 2 月 共有 29 天
while
循环只要布尔表达式值为true
,就会执行循环内容。直到布尔表达式值为false
,退出循环;
while(布尔表达式) { //布尔表达式值为true,执行循环内容 }
do ... while
循环只要布尔表达式值为true
,就会执行循环内容。直到布尔表达式值为false
,退出循环;和while
相似,不一样的是do...while
语句至少会被执行一次;
do { //布尔表达式值为true,执行循环内容 }while(布尔表达式)
for
循环for
循环执行次数在执行前肯定。
for(初始化;布尔表达式;更新) { //执行代码 }
关于for
的几点说明:
声明语句:声明新的局部变量,该变量的类型必须和数组元素的类型匹配。其做用域限定在循环语句块,其值与此时数组元素的值相等。
表达式:表达式是要访问的数组名,或者是返回值为数组的方法。
for(声明语句:表达式) { //执行代码 }
public class OutputLetters { public static void main(String[] args) { //循环输出26个英文小写字母,要求分两行输出 char ch = 'a'; int count = 0;//控制换行 while(ch <= 'z') { System.out.printf(ch + " "); ch ++; count ++; if (count % 13 == 0) { System.out.println(); } } System.out.println(); ch = 'a'; count = 0; for(count = 1, ch = 'a';ch <= 'z';ch ++,count ++) { System.out.printf(ch + " "); if (count % 13 == 0) { System.out.println(); } } } } Output: a b c d e f g h i j k l m n o p q r s t u v w x y z a b c d e f g h i j k l m n o p q r s t u v w x y z
break
break
语句
break
语句能够结束当前循环的执行;break
语句,循环体中位于break
语句后面的语句就不会被执行;break
语句至关于向外跳一层;continue
continue
语句:
continue
语句只能用在循环里;continue
语句能够结束当前循环的执行,可是要继续下一次循环的执行;public class OutputLettersDemo { public static void main(String[] args) { //循环输出26个英文小写字母,要求分两行输出 //练习break,cotinue char ch = 'a'; int count = 0;//控制换行 while(ch <= 'z') { if(ch == 'x') break; System.out.printf(ch + " "); ch ++; count ++; if (count % 13 == 0) { System.out.println(); } } System.out.println(); ch = 'a'; count = 0; for(count = 1, ch = 'a';ch <= 'z';ch ++,count ++) { if(ch == 'x') continue; System.out.printf(ch + " "); if (count % 13 == 0) { System.out.println(); } } } } Output: a b c d e f g h i j k l m n o p q r s t u v w a b c d e f g h i j k l m n o p q r s t u v w y z
从上面的例子能够看出,break语句直接退出当层循环(到x直接退出,再也不输出),而continue语句只是结束当前循环,并无退出(只是没有输出x)。