Java循环结构
1:while循环
public static void Sel(string ags[]) {
int x = 10;
while( x < 20 ) {
System.out.print("value of x : " + x );
x++;
System.out.print("\n");
}
}
//运行结果
value of x : 10
value of x : 11
value of x : 12
value of x : 13
value of x : 14
value of x : 15
value of x : 16
value of x : 17
value of x : 18
value of x : 19
2:do…while 循环
public static void main(String args[]){
int x = 1;
do{
System.out.print("value of x : " + x );
x++;
System.out.print("\n");
}while( x < 10 );
}
//运行结果
value of x : 1
value of x : 2
value of x : 3
value of x : 4
value of x : 5
value of x : 6
value of x : 7
value of x : 8
value of x : 9
3:Break关键字
public static void main(string args[]) {
int [] number = {10, 20, 30, 40, 50};
for(int x : number) {
// x 等于 30 时跳出循环
if( x == 30 ) {
break;
}
System.out.print( x );
System.out.print("\n");
}
}
//运行结果
10
20