Java循环结构+关键字

目录

一.循环结构

1.while循环

2.do-while循环

3.for循环

4.for-each循环(主要用于数组)

二.关键字

1.continue关键字

2.break关键字

3.out标签


一.循环结构

1.while循环

while(布尔表达式){
    //循环内容
}

(while循环流程图) 

示例:

public class Example {
    public static void main(String[] args) {
        int i=0;
        while(i<10){
            System.out.print(i+" ");
            i++;
        }
    }
}
0 1 2 3 4 5 6 7 8 9 

 

2.do-while循环

do{
    //代码语句
}while(布尔表达式);

 (do-while循环流程图)

示例:

public class Example {
    public static void main(String[] args) {
        int i=0;
        do{
            System.out.print(i+" ");
            i++;
        }while(i<10);
        System.out.println("");
        System.out.println("------分割线------");
        do{
            System.out.print(i+" ");
            i++;
        }while(i<5);
    }
}
0 1 2 3 4 5 6 7 8 9 
------分割线------
10 

 

3.for循环

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

 (for循环流程图)

示例:

public class Example {
    public static void main(String[] args) {
        for(int i=0;i<10;i++){
            System.out.print(i+" ");
        }
    }
}
0 1 2 3 4 5 6 7 8 9 

 

4.for-each循环(主要用于数组)

JDK 1.5 引进了一种新的循环类型,被称为 For-Each 循环,又称加强型循环,它能在不使用下标的情况下遍历数组。

for(声明语句 : 表达式)
{
   //代码句子
}
public class Example {
    public static void main(String[] args) {
        int[] number=new int[10];
        for(int i=0;i<number.length;i++){
            number[i]=i;
        }
        for(int num:number){
            System.out.print(num+" ");
        }
    }
}
0 1 2 3 4 5 6 7 8 9 

 

二.关键字

1.continue关键字

continue关键字适用于任何循环控制结构中,让程序立刻结束本次循环,跳转到下一次的循环,即本次循环中不继续执行continue后的语句,continue关键字也可以配合out标签使用,continue out跳到out标记的那一层,继续执行接下来的语句

在while或者do-while循环中,程序立刻跳转到布尔表达式的判断语句

在for循环中,程序立刻跳转到更新语句

在for-each循环中,程序立刻进入下一个角标的遍历

public class Example {
    public static void main(String[] args) {
        int i=0;
        while(i<10){
            i++;
            if(i==3){
                continue;
            }
            System.out.print(i+" ");
        }
        System.out.println("");
        System.out.println("-------分割线--------");
        i=0;
        do{
            i++;
            if(i==5){
                continue;
            }
            System.out.print(i+" ");
        }while(i<10);
    }
}
1 2 4 5 6 7 8 9 10 
-------分割线--------
1 2 3 4 6 7 8 9 10 
public class Example {
    public static void main(String[] args) {
        int[] number=new int[10];
        for(int i=0;i<number.length;i++){
            if(i==3){
                continue;
            }
            number[i]=i;
            System.out.print(number[i]+" ");
        }
        System.out.println("");
        System.out.println("-------分割线--------");
        for(int num:number){
            if(num==5){
                continue;
            }
            System.out.print(num+" ");
        }
    }
}
0 1 2 4 5 6 7 8 9 
-------分割线--------
0 1 2 0 4 6 7 8 9 
public class Example {
    public static void main(String[] args) {
        int row=3,list=3;
        int[][] number=new int[row][list];
        for(int i=0;i<row;i++){
            for(int j=0;j<list;j++){
                number[i][j]=i+j;
                if(i+j==3){
                    continue;
                }
            System.out.print(number[i][j]);
            }
            System.out.println(" ");
        }
        System.out.println(" ");
        System.out.println("-------分割线--------");
    }
}
012 
12 
24 
public class Example {
    public static void main(String[] args) {
        int row=3,list=3;
        int[][] number=new int[row][list];
        out:for(int i=0;i<row;i++){
            for(int j=0;j<list;j++){
                number[i][j]=i+j;
                if(i+j==3){
                    continue out;
                }
            System.out.print(number[i][j]);
            }
            System.out.println(" ");
        }
        System.out.println(" ");
        System.out.println("-------分割线--------");
    }
}
012 
122 

 

2.break关键字

break关键字主要在循环语句或者switch语句中,用来跳出整个语句块

如果有多层循环,break跳出最里层的循环,并且继续执行该循环下面的语句

break out直接跳出被out标记的循环

public class Example {
    public static void main(String[] args) {
        int row=3,list=3;
        int[][] number=new int[row][list];
        for(int i=0;i<row;i++){
            for(int j=0;j<list;j++){
                number[i][j]=i+j;
                if(i+j==3){
                    break ;
                }
                System.out.print(number[i][j]);
            }
            System.out.println(" ");
        }
        System.out.println(" ");
        System.out.println("-------分割线--------");
    }
}
012 
12 
2 
 
-------分割线--------
public class Example {
    public static void main(String[] args) {
        int row=3,list=3;
        int[][] number=new int[row][list];
        out:for(int i=0;i<row;i++){
            for(int j=0;j<list;j++){
                number[i][j]=i+j;
                if(i+j==3){
                    break out;
                }
                System.out.print(number[i][j]);
            }
            System.out.println(" ");
        }
        System.out.println(" ");
        System.out.println("-------分割线--------");
    }
}
012 
12 
-------分割线--------

3.out标签

比较少用,配合continue关键字或者break关键字,跳出到指定标签层,再执行continue或者break的效果,示例如上


  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值