Java 循环结构

目录

while 循环
do…while 循环
for循环
Java 增强 for 循环
break 关键字
continue 关键字


Java中有三种主要的循环结构:

  • while 循环
  • do…while 循环
  • for 循环

while 循环

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

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

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

实例:

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

 结果展示:

value of a : 8
value of a : 9
value of a : 10
value of a : 11

do…while 循环

do…while 循环和 while 循环相似。不同的是,即使不满足条件,do…while 循环至少会执行一次。

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

注意:布尔表达式在循环体的后面,所以语句块在检测布尔表达式之前已经执行了。 如果布尔表达式的值为 true,则语句块一直执行,直到布尔表达式的值为 false。

实例:

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

结果展示:

value of a : 12

for循环

      for 循环,使一些循环结构变得更加简单。 

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

      最先执行初始化步骤。可以声明一种类型,但可初始化一个或多个循环控制变量,也可以是空语句。
      然后,检测布尔表达式的值。如果为 true,循环体被执行。如果为false,循环终止,开始执行循环体后面的语句。
      执行一次循环后,更新循环控制变量。
      再次检测布尔表达式。循环执行上面的过程。

语法格式如下:

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

 实例:

public class Test {
    public static void main(String args[]) {

        for(int a = 8; a < 12; ++a) {
            System.out.print("value of a : " + a );
            System.out.print("\n");
        }
    }
}

结果展示:

value of a : 8
value of a : 9
value of a : 10
value of a : 11

Java 增强 for 循环

       一种主要用于数组的增强型 for 循环。

语法格式如下:

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

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

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

实例:

public class Test {
    public static void main(String args[]){
        int [] numbers = {1, 2, 3, 4, 5, 6};

        for(int a : numbers ){
            System.out.print( a );
            System.out.print(",");
        }
        System.out.print("\n");
        String [] typenames ={"tall", "short", "long", "fat"};
        for( String tp : typenames ) {
            System.out.print( tp );
            System.out.print(",");
        }
    }
}

结果展示:

1,2,3,4,5,6,
tall,short,long,fat,

 

break 关键字

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

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

实例:

public class Test {
    public static void main(String args[]){
        int [] numbers = {1, 2, 3, 4, 5, 6};

        for(int a : numbers ){
            if(a > 4){
                break;
            }
            System.out.print( a );
            System.out.print(",");
        }
        System.out.print("\n");
        String [] typenames ={"tall", "short", "long", "fat"};
        for( String tp : typenames ) {
            System.out.print( tp );
            System.out.print(",");
        }
    }
}

 结果展示:

1,2,3,4,
tall,short,long,fat,

 

continue 关键字

continue 适用于任何循环控制结构中。作用是让程序立刻跳转到下一次循环的迭代。

在 for 循环中,continue 语句使程序立即跳转到更新语句。

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

 实例:

public class Test {
    public static void main(String args[]){
        int [] numbers = {1, 2, 3, 4, 5, 6};

        for(int a : numbers ){
            if(a > 4){
                break;
            }
            System.out.print( a );
            System.out.print(",");
        }
        System.out.print("\n");
        String [] typenames ={"tall", "short", "long", "fat"};
        for( String tp : typenames ) {
            if (tp == "short"){
                continue;
            }
            System.out.print( tp );
            System.out.print(",");
        }
    }
}

 结果展示:

1,2,3,4,
tall,long,fat,
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值