实用指南:JAVA基础知识-循环

目录

一、什么是循环

二、while与do while循环

1、while循环

2、do while循环

3、while与do while循环的区别

三、for与增强for循环

1、for循环

2、增强for循环

四、break和continue关键字

1、break关键字

2、continue关键字


一、什么是循环

一种可以重复执行特点代码段的控制结构。简单而言就是一段在条件符合的情况下,可以自动重复执行的代码。

二、while与do while循环

1、while循环

1、结构

while( 布尔表达式 ) {

        //循环内容

};

:当布尔表达式为true时,while循环会不断重复执行。

2、代码示例

/**
 * while 代码示例
 */
public class LoopTest {
    public static void main(String [] args){
        LoopTest looptest = new LoopTest();
        looptest.whileTest(10);
    }
public void whileTest(int a){
        while(a > 0){
            System.out.println("while倒计时:" + a);
            a--;
        }
        System.out.println("END");
    }
}

while 示例输出结果

2、do while循环

1、结构

do{

        //循环内容

}while(布尔表达式);

2、代码示例

/**
 * do while 代码示例
 */
public class LoopTest {
    public static void main(String [] args){
        LoopTest looptest = new LoopTest();
        looptest.whileTest(10);
    }
public void doWhileTest(int a){
        do {
            System.out.println("do while倒计时:" + a);
            a--;
        }
        while (a > 0);
        System.out.println("END");
    }
}

do while 示例输出结果

3、while与do while循环的区别

对于while循环,如果不满足布尔表达式,则不会进入循环。但是对于do while循环而言,即便不满足布尔表达式也至少会执行一次。

示例。根据前面的示例代码,全都将条件由 a>0 改为 a<0来观察执行结果。

/**
 * while和do while 区别代码示例
 */
public class LoopTest {
    public static void main(String [] args){
        LoopTest looptest = new LoopTest();
        looptest.whileTest(10);
        looptest.doWhileTest(10);
    }
    //while
    public void whileTest(int a){
        while(a < 0){
            System.out.println("while倒计时:" + a);
            a--;
        }
        System.out.println("while END");
    }
    //do while
    public void doWhileTest(int a){
        do {
            System.out.println("do while倒计时:" + a);
            a--;
        }
        while (a < 0);
        System.out.println("do while END");
    }
}

输出结果

三、for与增强for循环

1、for循环

1、结构

for(初始化 ; 布尔表达式 ; 更新语句) {

        //循环内容

};

说明

初始化:可以声明一个类型,但是可初始化一个或多个变量,也可以是空语句。

布尔表达式:如果是true,循环执行;如果为false,循环终止并执行循环后面的语句。

更新语句:用于每执行完一次循环后,更新变量。

2、代码示例

/**
 * for 代码示例
 */
public class LoopTest {
    public static void main(String [] args){
        LoopTest looptest = new LoopTest();
        looptest.forTest(10);
    }
    public void forTest(int a){
        for (;a>0;a--){
            System.out.println("for 倒计时:"+ a);
        }
        System.out.println("END");
    }
}

for 示例输出结果

2、增强for循环

1、结构

for(声明语句 : 表达式) {

        //循环内容

};

说明

声明语句:声明的变量为局部变量,该变量的类型必须和数组元素的类型匹配。

表达式:要访问的数组。

2、代码示例

/**
 * 增强for 代码示例
 */
public class LoopTest {
    public static void main(String [] args){
        LoopTest looptest = new LoopTest();
        looptest.upForTest();
    }
   public void upForTest(){
        String [] array = {"Monday","Tuesday","Wednesday","Thursday","Friday"};
        for(String date : array){
            System.out.println("upfor日期:"+date);
        }
        System.out.println("放假了");
    }
}

增强for 运行输出结果

四、break和continue关键字

1、break关键字

作用:跳出当前的循环,并执行循环下面的语句。

代码示例

/**
 * break 代码示例
 */
public class LoopTest {
    public static void main(String [] args){
        LoopTest looptest = new LoopTest();
        looptest.breakTest();
    }
   public void breakTest(){
        String [] array = {"Monday","Tuesday","Wednesday","Thursday","Friday"};
        for(String date : array){
            if ("Wednesday".equals(date)){
                System.out.println("上四休三");
                break;
            }
            System.out.println("break日期" + date);
        }
        System.out.println("break放假了");
    }
}

break 示例输出结果

2、continue关键字

作用:让程序跳出当次循环,执行下一次循环。

代码示例

/**
 * continue 代码示例
 */
public class LoopTest {
    public static void main(String [] args){
        LoopTest looptest = new LoopTest();
        looptest.continueTest();
    }
   public void continueTest(){
        String [] array = {"Monday","Tuesday","Wednesday","Thursday","Friday"};
        for (String date : array){
            if ("Wednesday".equals(date)){
                System.out.println("休息一天");
                continue;
            }
            System.out.println("工作日:" + date);
        }
        System.out.println("continue放假了");
    }
}

continue 示例输出结果

posted @ 2025-11-06 09:52  ycfenxi  阅读(3)  评论(0)    收藏  举报