顺序结构的程序语句只能被执行一次。
如果您想要同样的操作执行多次,就需要使用循环结构。
Java中有三种主要的循环结构:
- while 循环
- do…while 循环
- for 循环
在 Java5 中引入了一种主要用于数组的增强型 for 循环。
一、while 循环
while是最基本的循环,它的结构为:
while( 布尔表达式 ) {
//循环内容
}
实例
public class Test {
public static void main(String[] args) {
int x = 10;
while (x < 20) {
System.out.println("value of x : " + x);
x++;
}
}
}
以上实例编译运行结果如下:
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