一、while循环控制
基本语法:
while(循环条件) {
循环体(语句);
循环变量迭代;
}
二、流程图
三、入门练习
public class While01 {
public static void main(String[] args) {
int i = 1;
while(i <= 10) {
System.out.println("hello" + i);
i++;
}
}
}
四、注意事项
1.循环条件是返回一个布尔值的表达式
2.while循环是先判断再执行语句
五、练习一
打印1-100之间所有能被3整除的数(使用while)
public class WhileExercise01 {
public static void main(String[] args) {
//打印1-100之间所有能被3整除的数(使用while)
int i = 1;
while(i <= 100) {
if(i % 3 == 0) {
System.out.println(i);
}
i++;
}
}
}
练习二
//打印40-200之间所有的偶数(使用while)
public class WhileExercise02 {
public static void main(String[] args) {
//打印40-200之间所有的偶数(使用while)
int i = 40;
while(i <= 200) {
if(i % 2 == 0) {
System.out.println(i);
}
i++;
}
}
}