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

举报
朱文元 发表于 2022/08/30 15:15:15 2022/08/30
936 0 0
【摘要】 循环语句类型有很多种:while循环、do…while、for循环,for循环是Java中最有用的循环语句之一,一个for循环可以用来重复执行某条语句,直到满足条件 for循环有两种语句一种是foreach语句,另一个就是传统的for语句.........

目录

前言

三、循环语句

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

六、结尾

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

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

编辑
​​​​​​​

【声明】本内容来自华为云开发者社区博主,不代表华为云及华为云开发者社区的观点和立场。转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息,否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

作者其他文章

评论(0

抱歉,系统识别当前为高风险访问,暂不支持该操作

    全部回复

    上滑加载中

    设置昵称

    在此一键设置昵称,即可参与社区互动!

    *长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

    *长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。