publicclassWhileDemo03{publicstaticvoidmain(String[] args){//计算1+2+3+...+100=?int i =0;int sum =0;while(i <=100){
sum += i;
i++;}
System.out.println(sum);}}
DoWhile
publicclassDoWhileDemo01{publicstaticvoidmain(String[] args){//int i =0;int sum =0;do{
sum = sum + i;
i++;}while(i <=100);
System.out.println(sum);}}/*
while和do-whlie的区别:
1. while先判断后执行,do-while是先执行后判断
2. Do...while总是保证循环体会背至少执行一次!这是他们的主要差别
*/
publicclassDoWhileDemo02{publicstaticvoidmain(String[] args){//int a =0;while(a <0){
System.out.println(a);
a++;}
System.out.println("===========");do{
System.out.println(a);
a++;}while(a<0);}}
Break
publicclassBreakDemo{publicstaticvoidmain(String[] args){int i =0;while(i <100){
i++;
System.out.println(i);if(i ==30){break;}}
System.out.println("break用于强行推出循环,并未终止程序!");}}