while循环
大多数不明确循环次数的用while循环,明确次数的用for循环。
while循环格式:
初始化语句;
while(条件判断语句){
循环体语句;
条件控制语句;
}
需求:打印0-100的数,每2个数换行一次
public class Demowhile{
public static void main(String[] args){
int i = 0;
while(i <= 100){
System.out.print(i + " ");
i++;
/*每打印2个数换行一次 */
if(i % 2 == 0){
System.out.println();
}
}
}
}
珠穆朗玛峰
do while循环(一般很少使用)
格式:
案例:
打印10次我是程序员。
public class DemoDoWhile{
public static void main(String[] args){
int i = 1;
do {
System.out.println("我是程序员!");
i++;
}while(i < 11);
}
}