Home Java javaTutorial [java tutorial] Java loop structure - for, while and do...while

[java tutorial] Java loop structure - for, while and do...while

Dec 26, 2016 am 11:42 AM

Java loop structure - for, while and do...while

The program statements of the sequence structure can only be executed once. If you want to perform the same operation multiple times, you need to use a loop structure.

There are three main loop structures in Java:

while loop

do...while loop

for loop

in Java5 Introduced an enhanced for loop primarily for arrays.

while loop

while is the most basic loop. Its structure is:

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

As long as the Boolean expression is true, the loop body will continue to execute.

Example

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

The compilation and running results of the above example are as follows:

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

do...while loop

For the while statement, if the condition is not met, then Cannot enter the loop. But sometimes we need to execute it at least once even if the conditions are not met.

do…while loop is similar to while loop. The difference is that do…while loop will be executed at least 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 statement block is executed until the Boolean expression evaluates to false.

Example

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

The compilation and running results of the above example are as follows:

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

for loop

Although all loop structures can use while or do...while means, but Java provides another statement-the for loop, which makes some loop structures 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 for the for loop:

The initialization step is executed first. One or more loop control variables can be declared and 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 the loop once, update the loop control variable.

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

The compilation and running results of the above example are as follows:

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

Java enhanced for loop

Java5 introduces an enhanced type mainly used for arrays for loop.

Java enhanced for loop syntax format is as follows:

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

Declaration statement: declare a new local variable. The type of the variable must match the type of the array element. Its scope is limited to the loop statement block, and its value is equal to the value of the array element at this time.

Expression: The expression is the name of the array to be accessed, or a method that returns an array.

Example

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

The compilation and running results of the above example are as follows:

10,20,30,40,50,
James,Larry,Tom,Lacy,
Copy after login

break keyword

break is mainly used in loop statements or switch statements. Jump out of the entire statement block.

break jumps out of the innermost loop and continues to execute the statements below the loop.

语法

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 ) {
         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就是循环体中一条简单的语句:

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

 以上就是【java教程】Java循环结构 - for, while 及 do...while的内容,更多相关内容请关注PHP中文网(www.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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Top 4 JavaScript Frameworks in 2025: React, Angular, Vue, Svelte Top 4 JavaScript Frameworks in 2025: React, Angular, Vue, Svelte Mar 07, 2025 pm 06:09 PM

Top 4 JavaScript Frameworks in 2025: React, Angular, Vue, Svelte

How does Java's classloading mechanism work, including different classloaders and their delegation models? How does Java's classloading mechanism work, including different classloaders and their delegation models? Mar 17, 2025 pm 05:35 PM

How does Java's classloading mechanism work, including different classloaders and their delegation models?

How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading? How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading? Mar 17, 2025 pm 05:43 PM

How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?

How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution? How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution? Mar 17, 2025 pm 05:46 PM

How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?

Iceberg: The Future of Data Lake Tables Iceberg: The Future of Data Lake Tables Mar 07, 2025 pm 06:31 PM

Iceberg: The Future of Data Lake Tables

How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache? How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache? Mar 17, 2025 pm 05:44 PM

How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?

Node.js 20: Key Performance Boosts and New Features Node.js 20: Key Performance Boosts and New Features Mar 07, 2025 pm 06:12 PM

Node.js 20: Key Performance Boosts and New Features

Spring Boot SnakeYAML 2.0 CVE-2022-1471 Issue Fixed Spring Boot SnakeYAML 2.0 CVE-2022-1471 Issue Fixed Mar 07, 2025 pm 05:52 PM

Spring Boot SnakeYAML 2.0 CVE-2022-1471 Issue Fixed

See all articles