一、while循环
1、while是最基本的循环,它的结构为:
while(布尔表达式){
//循环内容
}
2、只要布尔表达式为true,循环就会一直执行下去。
3、我们大多数情况是会让循环停止下来的,我们需要一个让表达式失效的方式来结束循环。
4、少部分情况需要循环一直执行,比如服务器的请求响应监听等。
5、循环条件一直为true就会造成无限循环【死循环】,我们正常的业务编程中应该尽量避免死循环。会影响程序性能或者造成程序卡死崩溃!
6、思考:计算1+2+3+…+100=?
package struct;
public class Demo07 {
public static void main(String[] args) {
//输出1~100
int i = 1;
while (i<100){
i++;
System.out.println(i);
}
}
}
package struct;
public class Demo08 {
public static void main(String[] args) {
//死循环
while (true){
//等待客户端连接
//定时检查
//........
}
}
}
package struct;
public class Demo09 {
public static void main(String[] args) {
//计算1+2+3+…+100=?
int i = 0;
int sum = 0;
while (i<=100){
sum = sum + i;
i++;
}
System.out.println(sum);
}
}



