Java循环及控制语句详细级教程(下篇)续集

目录

 

前言

三、循环语句

1.while循环语句

2.do…while循环

3.for循环

1.for循环

2.foreach语句

五、循环控制

1.break语句

2.continue语句

六、结尾


前言

🌟在上一篇Java条件语句的介绍后就来到了我们的重点"循环语句"以及"循环控制"。在本篇文章中我将会带领大家学习并掌握循环语句以及循环控制的使用!

三、循环语句

循环语句类型有很多种:while循环、do…while、for循环

1.while循环语句

🧸whlie循环语句又称为条件判断语句

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

以上代码运行结果如下

value of x : 10
value of x : 11
value of x : 12
value of x : 13
value of x : 14
value of x : 15
value of x : 16
value of x : 17
value of x : 18
value of x : 19

2.do…while循环

🌟do…while循环与while循环类似                                                                                                                        区别在于:while循环是符合条件之后再执行循环体;而do…while循环是先执行一次,再判断是否符合条件最后决定是否执行循环体。

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 );
   }
}
以上代码运行结果如下
value of x : 10
value of x : 11
value of x : 12
value of x : 13
value of x : 14
value of x : 15
value of x : 16
value of x : 17
value of x : 18
value of x : 19

3.for循环

for循环是Java中最有用的循环语句之一,一个for循环可以用来重复执行某条语句,直到满足条件        for循环有两种语句一种是foreach语句,另一个就是传统的for语句

1.for循环

 以下为代码演示

/*
for(初始化; 布尔表达式; 更新) {
    //代码语句
}
*/
public class Test {
   public static void main(String[] args) {
 
      for(int x = 10; x < 20; x = x+1) {
         System.out.print("value of x : " + x );
         System.out.print("\n");
      }
   }
}

以上代码运行结果

value of x : 10
value of x : 11
value of x : 12
value of x : 13
value of x : 14
value of x : 15
value of x : 16
value of x : 17
value of x : 18
value of x : 19

2.foreach语句

foreach语句又称为增强for语句

以下为代码演示

/*for(声明语句 : 表达式)
{
   //代码句子
}
*/
//    声明语句:声明新的局部变量,该变量的类型必须和数组元素的类型匹配。其作用域限定在循环语句块,其值与此时数组元素的值相等。
//表达式:表达式是要访问的数组名,或者是返回值为数组的方法。
public class Test {
   public static void main(String[] args){
      int [] numbers = {10, 20, 30, 40, 50};
 
      for(int x : numbers ){
         System.out.print( x );
         System.out.print(",");
      }
      System.out.print("\n");
      String [] names ={"James", "Larry", "Tom", "Lacy"};
      for( String name : names ) {
         System.out.print( name );
         System.out.print(",");
      }
   }
}

运行结果如下

10,20,30,40,50,
James,Larry,Tom,Lacy,

五、循环控制

1.break语句

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

public class Test {
   public static void main(String[] args) {
      int [] numbers = {10, 20, 30, 40, 50};
 
      for(int x : numbers ) {
         // x 等于 30 时跳出循环
         if( x == 30 ) {
            break;
         }
         System.out.print( x );
         System.out.print("\n");
      }
   }
}

运行结果为10与20

break可以强制跳出循环或者是switch语句块!🙈

2.continue语句

continue语句的作用与break不同,他的作用就是强制跳过本次循环进入到下一次循环

public class Test {
   public static void main(String[] args) {
      int [] numbers = {10, 20, 30, 40, 50};
 
      for(int x : numbers ) {
         if( x == 30 ) {
        continue;
         }
         System.out.print( x );
         System.out.print("\n");
      }
   }
}

以上运行结果为10,20,40,50

六、结尾

这不仅仅是本篇的结尾,更是本章的结尾!

希望大家能支持一下博主的文章!


​​​​​​​

  • 62
    点赞
  • 39
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 76
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小菜元

敲代码不容易请支持一下吧

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

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

打赏作者

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

抵扣说明:

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

余额充值