Home > Java > javaTutorial > body text

JAVA Tutorial | Chapter 6 Loop Structure

黄舟
Release: 2017-02-25 10:00:32
Original
1302 people have browsed it

Java Loop Structure

The code in the program is executed sequentially, which means it can only be executed once. If you want to perform the same operation multiple times, the headquarters may copy the code several times! Therefore, a loop structure needs to be used here.

There are three main loop structures in Java:

  • ##while loop

  • do…while loop

  • for loop



##while loop

while is the most basic loop. As long as the condition inside while is true, the while loop will continue. The syntax is as follows
while(true){
        //...
}
Copy after login

Example

public class Test {
   public static void main(String args[]) {
      int x = 15;
      while( x < 20 ) {
         System.out.print("X is : " + x );
         x++;
         System.out.println("");
      }
   }
}
Copy after login
The above example compilation and running results are as follows:

X is  : 15 
X is  : 16 
X is  : 17 
X is  : 18
X is  : 19
Copy after login



do...while loop

do...while loop is the same as while loop. The difference is that the do operation will be executed first, and then it will be judged whether the while condition is true. If it is true, it will continue to execute the do statement. If it is not true, it will exit the loop body. .

Even if the conditions are not met, the execution statement will be executed once.

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

Note:

The Boolean expression is after the loop body, so the statement block has been executed before detecting the Boolean expression. If the Boolean expression evaluates to true, the block of statements executes until the Boolean expression evaluates to false.

Example

public class Test {
   public static void main(String args[]){
      int x = 10;
 
      do{
         System.out.print("值为 : " + x );
         x++;
         System.out.println("");
      }while( x < 20 );
   }
}
Copy after login
The above example compilation and running results are as follows:
值为 : 10
值为 : 11
值为 : 12
值为 : 13
值为 : 14
值为 : 15
值为 : 16
值为 : 17
值为 : 18
值为 : 19
Copy after login



for Loop

Although the loop structure can use while or do...while, Java provides another statement-the for loop, which makes the loop structure simpler.

The number of times the for loop is executed is determined before execution. The syntax format is as follows:
for(初始化; 布尔表达式; 操作) {
        //...
        //代码语句
        //...
}
Copy after login

There are several instructions about the for loop:

      The initialization step is executed first. A type can be declared, but one or more loop control variables can be initialized, or it can be an empty statement.
    • Then, detect the value of the Boolean expression. If true, the loop body is executed. If it is false, the loop terminates and execution of the statements following the loop body begins.
    • After executing a loop, update the loop control variables.
    • Detect Boolean expression again. Perform the above process in a loop.
Example

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");
      }
   }
}
Copy after login

以上实例编译运行结果如下:

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
Copy after login


Java 增强 for 循环

Java5 引入了一种主要用于数组的增强型 for 循环。其实学过C#的同学应该知道,这个和foreach循环是一样的。

Java 增强 for 循环语法格式如下:

for(声明语句 : 表达式){
   //代码句子
}
Copy after login
声明语句:声明新的局部变量,该变量的类型必须和数组元素的类型匹配。其作用域限定在循环语句块,其值与此时数组元素的值相等。

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

这个方法一般会用在对象输出的方式上。

实例


public class Test {
   public static void main(String args[]){
      int [] numbers = {1, 2, 3, 4, 5};
 
      for(int x : numbers ){
         System.out.print( x );
         System.out.print(",");
      }
      System.out.println("");
      String [] names ={"Karl", "DuJinYang", "JiMi", "Lucy"};
      for( String name : names ) {
         System.out.print( name );
         System.out.print(",");
      }
   }
}
Copy after login


以上实例编译运行结果如下:


10,20,30,40,50,
Karl,DuJinYang,JiMi,Lucy,
Copy after login


break 关键字

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

break 中文就是结束的意思,顾名思义,就是跳出最里层(当前涵盖)的循环,并且继续执行该循环下面的语句。

语法

break 的用法很简单,就是循环结构中的一条语句:

break;
Copy after login

实例


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");
      }
   }
}
Copy after login
以上实例编译运行结果如下:
10
20
Copy after login

continue 关键字

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

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

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

语法

continue;
Copy after login

实例

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");
      }
   }
}
Copy after login
以上实例编译运行结果如下:
10
20
40
50
Copy after login

嵌套循环

do...while、while、for 循环都是可以相互嵌套的,这里不做过多的演示,大家可以自己去试验一下。

 以上就是JAVA 入坑教程 | 章节六  循环结构体的内容,更多相关内容请关注PHP中文网(www.php.cn)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!