while 循环
while(condition){
//xxx
}
for循环
for(initialization;expression;update){//注意是分号的
//xxxx
}
for(int x = 10; x < 20; x = x + 1) {
System.out.print("value of x : " + x );
System.out.print("\\n");
}
do{
//xxxx
}while(condition);
int x = 10;
do {
System.out.print("value of x : " + x );
x++;
System.out.print("\\n");
}while( x < 20 );
循环控制声明
break
int [] numbers = {10, 20, 30, 40, 50};
for(int x : numbers ) {
if( x == 30 ) {
break;
}
System.out.print( x );
System.out.print("\\n");
}
continue
int [] numbers = {10, 20, 30, 40, 50};
for(int x : numbers ) {
if( x == 30 ) {
continue;
}
System.out.print( x );
System.out.print("\\n");
}
java 循环增强
for(declaration : expression) {
// Statements
}
public class Test {
public static void main(String args[]) {
int [] numbers = {10, 20, 30, 40, 50};
for(int x : numbers ) {
System.out.print( x );
System.out.print(",");
}
System.out.print("\\n");
String [] names = {"James", "Larry", "Tom", "Lacy"}; //大括号
for( String name : names ) {
System.out.print( name );
System.out.print(",");
}
}
}