选择结构
- if(布尔表达式) 单种case
 - if(布尔表达式)……….else 两种case
 if(布尔)………..else if(布尔)………else 可以多种case
/*** 选择结构示例*/public void ifTest(){int a = 10;//单种caseif (a == 10 ){System.out.println("a is 10");}//两种caseif (a < 10){System.out.println("a < 10");}else{System.out.println("a >= 10");}//多种caseif (a <10){System.out.println("a < 10");}else if (a == 10 ){System.out.println("a = 10 ");}else{System.out.println("a > 10");}}
多重选择结构 switch(表达式)
- 多个case分支
 - 满足一个分支后,需要break
 最后一个分支为default
/*** switch选择示例*/public void switchTest(){int a1 = 1;int a2 = 2;switch (a1+a2){case 0:System.out.println("a1 + a2 = 0");break;case 1:System.out.println("a1 + a2 =1");break;case 2:System.out.println("a1 + a2 = 2");break;case 3:System.out.println("a1 + a2 = 3");break;default:System.out.println("a1 + a2 = ?");}String a = "hh";switch (a){case "hh": System.out.println("true");break;case "h": System.out.println("false");break;case "f": System.out.println("false");break;default:System.out.println("?");}}
循环结构
while
- do……..while
 - for
 - break //终端循环并退出
 continue //跳出本次循环,继续下次循环
/*** 循环示例*/public void loopTest(){//while循环int x = 10;while (x<20){System.out.println("x is "+x);x++;}//do while循环int y = 0;do{System.out.println("y is "+y);y++;}while (y < 10);//for循环for (int i = 0;i<10;i++){System.out.println("i is "+i);}}
总结
根据分支数挑选合适的选择结构
- 根据循环次数选择合适的循环结构
 
