while循环
/*
While循环的使用
一、 循环结构的4个要素
初始化条件
循环条件 boolean类型
循环体
迭代条件
二、 while循环结构
1
while(2) {3;4;}
执行过程 1 - 2 - 3 - 4 - 2 - 3 - 4 - ... - 2
说明:1.写while条件千万小心不要丢了迭代条件,一旦丢了,就可能导致进入死循环!
2.我们写程序,要避免出现死循环
3.for循环和while循环是可以相互转换的!
区别:for循环和while循环的初始化条件的作用范围不同
算法:有限性,不可以无限的执行
*/
class WhileTest
{
public static void main(String [] args)
{
//遍历100以内的所有偶数
int i = 1;
while (i<=100)
{
if (i%2 == 0)
{
System.out.println(i);
}
i++;
}
//出了while循环以后,仍然可以调用
System.out.println(i);//101
}
}