循环语句
(1)while语句
1 class WhileDemo 2 { 3 public static void main(String[] args) 4 { 5 int i=1,s=0;//初始化语句 6 while (i<=100)//循环条件 7 { 8 s=s+i;//循环体 9 i++;//迭代语句 10 } 11 System.out.println(s); 12 } 13 }
1 class WhileDemo2 2 { 3 public static void main(String[] args) 4 { 5 int i=0,s=0; 6 while (i++<100)//++优先级大于< 7 { 8 s=s+i; 9 } 10 System.out.println(s); 11 } 12 }
(2)do while
1 class DoWhileDemo 2 { 3 public static void main(String[] args) 4 { 5 int i=0,s=0; 6 do 7 { 8 s=s+i; 9 i++; 10 } 11 while (i<=100); 12 System.out.println(s); 13 } 14 }
(3)for
1 class ForDemo 2 { 3 public static void main(String[] args) 4 { 5 int s=0; 6 for (int i=1;i<=100 ;i++ ) 7 { 8 s+=i; 9 } 10 System.out.println(s); 11 } 12 }
(4)循环嵌套
练习1:打印九九乘法表
1 class MultiplicationTable 2 { 3 public static void main(String[] args) 4 { 5 for (int b=1;b<10 ;b++ ) 6 { 7 for (int a=1;a<=b ;a++ ) 8 { 9 System.out.print(a+"*"+b+"="+a*b+"\t"); 10 } 11 System.out.println(); 12 } 13 } 14 } 15 /* 16 1*1=1 17 1*2=2 2*2=4 18 1*3=3 2*3=6 3*3=9 19 1*4=4 2*4=8 3*4=12 4*4=16 20 1*5=5 2*5=10 3*5=15 4*5=20 5*5=25 21 1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36 22 1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49 23 1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64 24 1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81 25 */
控制循环结构
(1)break
1 class MultiplicationTable 2 { 3 public static void main(String[] args) 4 { 5 6 for (int b=1;b<10 ;b++ ) 7 { 8 for (int a=1;a<=b ;a++ ) 9 { 10 if(a==6) 11 break; 12 System.out.print(a+"*"+b+"="+a*b+"\t"); 13 } 14 System.out.println(); 15 } 16 } 17 } 18 /* 19 1*1=1 20 1*2=2 2*2=4 21 1*3=3 2*3=6 3*3=9 22 1*4=4 2*4=8 3*4=12 4*4=16 23 1*5=5 2*5=10 3*5=15 4*5=20 5*5=25 24 1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 25 1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 26 1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 27 1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 28 29 */
break不仅可以结束其所在的循环,还可以直接结束其外循环。此时需要break后紧跟一个标签,这个标签用于标识一个外层循环。
1 class MultiplicationTable 2 { 3 public static void main(String[] args) 4 { 5 Outer: 6 for (int b=1;b<10 ;b++ ) 7 { 8 for (int a=1;a<=b ;a++ ) 9 { 10 if(a==6) 11 break Outer; 12 System.out.print(a+"*"+b+"="+a*b+"\t"); 13 } 14 System.out.println(); 15 } 16 } 17 } 18 /* 19 1*1=1 20 1*2=2 2*2=4 21 1*3=3 2*3=6 3*3=9 22 1*4=4 2*4=8 3*4=12 4*4=16 23 1*5=5 2*5=10 3*5=15 4*5=20 5*5=25 24 1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 25 26 */
(2)使用continue结束本次循环
1 class MultiplicationTable 2 { 3 public static void main(String[] args) 4 { 5 for (int b=1;b<10 ;b++ ) 6 { 7 for (int a=1;a<=b ;a++ ) 8 { 9 if(a==6) 10 continue; 11 System.out.print(a+"*"+b+"="+a*b+"\t"); 12 } 13 System.out.println(); 14 } 15 } 16 } 17 /* 18 1*1=1 19 1*2=2 2*2=4 20 1*3=3 2*3=6 3*3=9 21 1*4=4 2*4=8 3*4=12 4*4=16 22 1*5=5 2*5=10 3*5=15 4*5=20 5*5=25 23 1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 24 1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 7*7=49 25 1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 7*8=56 8*8=64 26 1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 7*9=63 8*9=72 9*9=81 27 28 */
与break类似,continue也可以标识一个标签,用于标签所标识循环的当次循环。
class MultiplicationTable { public static void main(String[] args) { Outer: for (int b=1;b<10 ;b++ ) { for (int a=1;a<=b ;a++ ) { if(a==6) continue Outer; System.out.print(a+"*"+b+"="+a*b+"\t"); } System.out.println(); } } } /* 1*1=1 1*2=2 2*2=4 1*3=3 2*3=6 3*3=9 1*4=4 2*4=8 3*4=12 4*4=16 1*5=5 2*5=10 3*5=15 4*5=20 5*5=25 1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 */
(3)return语句
return语句是结束一个方法。return后可以跟常量,变量和表达式。
浙公网安备 33010602011771号