public static void main(String args[]) { for(int x = 10; x < 20; x = x+1) { System.out.print(“value of x : " + x ); System.out.print(”\n"); } } }
上图程序for循环执行演示:
<2>while-do
public class Test {
public static void main(String args[]) {
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
<3>do-while
public class Test {
public static void main(String args[]){ int x = 10;
</span><span style="color: #0000ff;">do</span><span style="color: #000000;">{
System.out.print(</span>"value of x : " +<span style="color: #000000;"> x );
x</span>++<span style="color: #000000;">;
System.out.print(</span>"\n"<span style="color: #000000;">);
}</span><span style="color: #0000ff;">while</span>( x < 20<span style="color: #000000;"> );
} }
运行结果:
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