java学习之语法结构(三)while break continue

本文探讨了Java中的循环控制语句,包括break用于中断外层循环,continue用于跳过当前循环的剩余部分,以及while和do-while循环的使用与区别。重点讲解了while循环和do-while循环的语法结构以及它们先执行后判断或先判断后执行的特性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

break:终止,中断循环

public class test{
    public static void main(String[] args){
        for(int x = 0;x<=5;x++){
            if(x ==3){
                System.out.println(x);
                break;/当条件满足时,终端程序
            }
        }
    }
}

/*
Desktop\test>java test
3
*/

public class test{
    public static void main(String[] args){
        for(int x = 0;x<=5;x++){
            if(x ==3){
                break;//当条件满足时,终端程序
            }
                System.out.println(x);
        }
    }
}
/*
0
1
2
*/

 重点:

public class test{
    public static void main(String[] args){
        int x = 1; //在循环外声明变量
        int y =1;
        for(;x<=5;x++){
            for(;y<=5;y++){
                if(y ==3){ //循环时取到的变量是累积赋值过后的变量,也就是第2轮循环x,y分别为:x-->2,y-->3,只剩外层循环在继续,内层循环一直中断
                    break;//当条件满足时,终端程序
                }
                System.out.println("OK");
            }
        }
    }
}
/*
Desktop\test>java test
OK
OK
*/

public class test{
    public static void main(String[] args){
        for(int x =1;x<=5;x++){//在循环内声明变量
            for(int y = 1;y<=5;y++){ //每次循环开始都将变量初始为1
                if(y ==3){
                    break;//当条件满足时,终端程序
                }
                System.out.println("OK");
            }
        }
    }
}
/*
Desktop\test>java test
OK
OK
OK
OK
OK
OK
OK
OK
OK
OK
*/

break中断外层循环:

public class test{
    public static void main(String[] args){
        ok:for(int i = 1;i<=5;i++){  //循环标记
            ko:for(int j=1;j<=5;j++){
                if(j==3){
                    break ok;//中断标记的循环
                }
                System.out.println("i:"+i+"j:"+j);
            }
        }
    }
}
/*
Desktop\test>java test
i:1j:1
i:1j:2

*/

continue:

public class test{
    public static void main(String[] args){
        for(int i = 1; i <=5; i++){
             if(i==3){
                 continue;//中断本次循环,继续下一次循环
             }
             System.out.println(i);
        }
    }
}
/*
Desktop\test>java test
1
2
4
5

*/

while循环语法:

    初始值放在外面;

    while(只允许写一个终点判断条件){

        变化量;

    }

public class test{
    public static void main(String[] args){
        int i =1;
        while(i<=5){
            System.out.println(i);
            i++;
        }
    }
}

do{} while()语法:

do{

    变化量;

}while(条件);

public class test{
    public static void main(String[] args){
        int i =1;
        do{
            System.out.println(i);
            i++;
        }while(i<=5);
    }
}

public class test{
    public static void main(String[] args){
        int i =10;
        do{
            System.out.println(i);
            i++;
        }while(i<=5);
    }
}

do while与while区别:

    do while:先执行后判断,至少执行一次

    while:先判断,再执行

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值