Java 循环结构 - for, while 及 do...while

目录

一、while循环:

1.1公式:   

二、For循环:

2.1公式:

2.2案例:

2.3 For循环案例合集(共3种):

三、do...while循环:

3.1定义公式:

3.2实例:

四、嵌套循环:

五、跳转控制语句:

六、Random随机数技术:

七、总结


一、while循环:

1.1公式:   

 初始化语句;

while (循环条件) {
    循环体语句(被重复执行的代码);

    迭代语句;

}

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

public static void main(String[] args) {
        System.out.println(" I Love You");
        System.out.println(" I Love You");
        int a=0;
        while (a<3){
            System.out.println("Iloveyou"+"---"+a);
            a++;
        }
        int i=0;
        int sum=0;
        while (i<=100){
            sum=sum+i;
            i++;
        }
        System.out.println("i累加到100的和:"+sum);
    }

二、For循环:

2.1公式:

for (初始化语句; 循环条件; 迭代语句)  {
  循环体语句(重复执行的代码);

}

2.2案例:

// 输出3次HelloWorld
for (int i = 0; i < 3; i++) {   
 System.out.println("Hello World");
}

执行流程:

循环一开始,执行int i = 0 一次。

然后判断循环条件:0 < 3 返回true ,进入到循环体中执行输出 :helloWorld ,然后执行迭代语句i++ , 此时i=1了。

然后判断循环条件:1 < 3 返回true ,进入到循环体中执行输出 :helloWorld ,然后执行迭代语句i++ , 此时i=2了。

然后判断循环条件:2 < 3 返回true ,进入到循环体中执行输出 :helloWorld, 然后执行迭代语句i++ , 此时i=3了。

然后判断循环条件:3 < 3 返回false, 循环立即结束!!

2.3 For循环案例合集(共3种):

/*  案例一:求和*/
        int qw = 0;
        for (int i = 0; i < 6; i++) {
            qw =qw+ i;

        }
        System.out.println("qw的和" + qw);
        System.out.println("------------------------------");
//    案例二:求奇数和
        for (int t = 1; t <= 10; t++) {
            if (t % 2 == 1) {
                System.out.println(t);
            }
        }
 /* 案例三:水仙花数*/
        int co=0;
        for (int I=100;I<1000;I++){
            int Sing=I%10;//个位
            int Song=I%100/10;//十位
            int Skng=I%1000/100;//百位
            if (Sing*Sing*Sing+Song*Song*Song+Skng*Skng*Skng==I)
                System.out.println(I+"是水仙花树");
            co++;
        }
        System.out.println("一共有"+co+"个水仙花树!");
        System.out.println("----------------------------------");

三、do...while循环:

3.1定义公式:

初始化语句;

do {

    循环体语句;

    迭代语句;

} while (循环条件);

do...while循环的特点:一定会先执行一次循环体。

举例:

int i = 0;
while (i < 3) {
   
System.out.println("Hello World");
    i++;
}

图解:

3.2实例:


public class Test {
   public static void main(String[] args){
      int x = 10;
 
      do{
         System.out.print("value of x : " + x );
         x++;
         System.out.print("\n");
      }while( x < 20 );
   }
}

四、嵌套循环:

for(int i = 0; i < 3; i++) {

     for(int j = 0; j < 5; j++) {System.out.println("我爱你");
     }
}

嵌套循环的特点:

外部循环每循环一次,内部循环全部执行完一次。

五、跳转控制语句:

  1. break :  跳出并结束当前所在循环的执行。
  2. continue:  用于跳出当前循环的当次执行,进入下一次循环。

注意事项:

break : 只能用于结束所在循环, 或者结束所在switch分支的执行。

continue : 只能在循环中进行使用。

六、Random随机数技术:

 

 

七、总结

  • for循环 和 while循环(先判断后执行)
  • do...while (第一次先执行后判断
  • for循环和while循环的执行流程是一模一样的。
  • 如果已知循环次数建议使用for循环,如果不清楚要循环多少次建议使用while循环。
  • for循环中,控制循环的变量只在循环中可以使用。While循环中,控制循环的变量在循环后还可以继续使用。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

KK在编程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值