290114lyp

导航

While循环

While循环

while(布尔表达式) {

//循环内容

◆只要布尔表达式为true,循环就会一直执行下去。

◆我们大多数情况是会让循环停止下来的,我们需要一个让表达式失效的方式来结束循环。

◆少部分情况需要循环一直执行,比如服务器的请求响应监听等。

◆循环条件一直为true就会造成无限循环[死循环],我们正常的业务编程中应该尽量避免死循环。会影响程序性能或者造成程序卡死奔溃!

package com.jiemo.struct;
public class WhileShabi1 {    
   public static void main(String[] args) {        
       //输出1~100        
       int i=0;        
       while (i<100){            
           i++;            
           System.out.println(i);       }   }}
package com.jiemo.struct;
public class WhileShabi2 {    
   public static void main(String[] args) {        
       //死循环        
       while (true){            
           //等待客户端链接            
           //定时检查            
           //。。。。。       }   }}
package com.jiemo.struct;
public class WhileShabi3 {    
   public static void main(String[] args) {        
       //计算10到100相加        
       int i= 0;        
       int sum =0;        
       while (i<=100){            
           sum = sum+i;            
           i++;       }        
       System.out.println(sum);   }}

posted on 2022-10-26 19:26  是芥末!日  阅读(11)  评论(0编辑  收藏  举报