for、while、do...while循环结构

循环结构分别有:
  • while 循环

  • do...while 循环

  • for 循环

 

  • 在Java5中引入了一种主要用于数组的增强型for循环

 

while 循环
  • while是最基本的循环,它的结构为:

while(布尔表达式){
   //循环结构
}

 

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

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

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

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

 

实例:

public class WhileDemo01 {

   public static void main(String[] args) {

       //输出1-100
       int i =0; //定义变量、赋值

       while (i<100){
           i++;
           System.out.println(i);
      }
  }
}

 

死循环:

public class WhileDemo02 {
   public static void main(String[] args) {
       //死循环
       while (true){
           //等待客户端连接
           //定时检查

           //使用循环要尽量避免死循环,除非必要时
      }
  }
}

 

练习:计算1+2+3+...+100=?

public class WhileDemo03 {
   public static void main(String[] args) {
       //计算1+2+3+...+100=?
       int i = 0;
       int sun = 0;

       while (i<=100){
           sun = sun + i;
           i++;
           System.out.println(sun);
      }
  }
}

 

 

do...while 循环
  • 对于while 语句而言,,如果不满足条件,则不能进入循环。但是有时候我们需要即使不满足条件,也至少执行一次。

  • do...while 循环和 while 循环相似,不同的是,do...while 循环至少执行一次。

语法:

do {
   //代码语句
}while

 

  • While和do...While的区别:

    • while先判断后执行。do...while是先执行后判断!

    • Do...while总是保证循环体会被至少执行一次!这是它们的主要差别。

 

实例:

public class DoWhileDemo01 {
   public static void main(String[] args) {
       //定义变量、赋值
       int i = 0;
       int sun = 0;

       do {
           sun = sun + i;
           i++;
      }while (i<=100);
       System.out.println(sun);
  }
}

 

测试:

通过代码来了解 while 和 do...while 的区别

public class DoWhileDemo02 {
   //通过实操来了解 while 和 do...while 的区别

   public static void main(String[] args) {
       int a = 0;
       while (a<0){  //先判断后执行,
           
           System.out.println(a);
           a++;

      }
       System.out.println("===========");//分割线

       do {    //先执行后判断!
           //Do...while总是保证循环体会被至少执行一次
           
           System.out.println(a);
           a++;
      }while (a<0);

       /*
       输出结果:
       ===========
       0
        */
  }
}

 

 

for循环
  • 虽然所有循环结构都可以用 while 或者 do...while表示,但Java提供了另一种语法 for循环,使一些循环结构变得更加简单。

  • for循环语句是支持迭代的一种通用结构,是最有效、最灵活的循环结构

  • for循环执行的次数是在执行前就确定的。

语法格式如下:

for(初始值;布尔表达式;更新){
   //代码语句
}

 

实例:

public class ForDemo01 {
   public static void main(String[] args) {
       int a = 0;  //初始化条件

       while (a<=100){  //条件判断
           System.out.println(a);  //循环体
           a+=2;  //迭代
      }
       System.out.println("while循环结束!");

       //初始化 //条件判断 //迭代
       for (int i = 0;i<=100;i++){
           System.out.println(i);
      }
       System.out.println("for循环结束!");

       /*
       关于 for 循环有以下几点说明:
       最先执行初始化步骤,可以声明一种类型。但可初始化一个或多个循环控制变量,也可以是空语句。
       然后,检测布尔表达式的值。如果为 true,循环被执行。如果为 false,循环终止,开始执行循环体后面的语句。
       执行一次循环后,更新循环控制变量(迭代因子控制循环变量的增减)。
       再次检测布尔表达式。循环执行上面的过程。
        */

  }
}

 

练习1:计算0到100之间的奇数和偶数的和

public class ForDemo02 {
   public static void main(String[] args) {
       //练习1:计算0到100之间的奇数和偶数的和

       int oddSum = 0; //定义奇数
       int evenSum = 0; //定义偶数

       for (int i = 0; i < 100; i++){
           if (i%2!=0){ //奇数
               oddSum+=i; //oddSum = oddSum + i;

          }else {  //偶数
               evenSum+=i; //eveSum = eveSum + i;
          }
      }
       System.out.println("奇数的和:"+oddSum);
       System.out.println("偶数的和:"+evenSum);

  }
}

 

练习2:用 while 或 for 循环输出1-1000之间能被5整除的数,并且每行输出3个

public class ForDemo03 {
   public static void main(String[] args) {
       //练习2:用 while 或 for 循环输出1-1000之间能被5整除的数,并且每行输出3个

       for (int i = 0; i <= 1000; i++) {
           if (i%5==0){
               System.out.print(i+"\t");

          }if (i%(5*3)==0){ //每行
               System.out.println();
              // System.out.print("\n");
          }
      }
       //println 输出完会换行
       //print 输出完不会换行
  }
}

 

练习4:打印九九乘法表

public class ForDemo04 {
   public static void main(String[] args) {
       //打印九九乘法表


       //1.我们先打印第一列,
       //2.我们把固定的1再用一个循环包起来
       //3.去掉重复项,i <= j
       //4.调整样式

       for (int j = 1; j <= 9; j++) {
           for (int i = 1; i <= j; i++) {
               System.out.print(i+"*"+j+"="+(j*i) +"\t");
          }
           System.out.println(); //换行
      }

  }
}

 

 

增强for循环
  • 这里我们先只是见一面,做个了解,之后数组我们重点使用

  • Java5 引入了一种主要用于数组或集合的增强型 for 循环。

  • Java 增强 for 循环语法格式如下:

for(声明语句 :表达式)
{
   //代码句子
}

 

  • 声明语句:声明新的局部变量,该变量的类型必须和数组元素类型匹配。其作用域限定在循环语句块,其值与此时数组元素值相等。

  • 表达式;表达式是要访问的数组名,或者返回值为数组的方法。

 

实例:

public class ForDemo05 {
   public static void main(String[] args) {
       int[] numbers = {10,20,30,40,50,}; //定义了一个数组

       for (int i = 0;i<5;i++){
           System.out.println(numbers[i]);
      }
       //遍历数组元素
       for (int x:numbers){ //增强for循环
           System.out.println(x);
      }
  }
}

 

posted @ 2022-01-19 20:46  小瑭  阅读(162)  评论(0编辑  收藏  举报