JAVA Tutorial | Chapter 6 Loop Structure
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
Examplewhile(true){ //... }Copy after login
The above example compilation and running results are as follows: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 loginX is : 15 X is : 16 X is : 17 X is : 18 X is : 19Copy 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:ExampleThe 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.
The above example compilation and running results are as follows: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值为 : 10 值为 : 11 值为 : 12 值为 : 13 值为 : 14 值为 : 15 值为 : 16 值为 : 17 值为 : 18 值为 : 19Copy 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:
There are several instructions about the for loop:for(初始化; 布尔表达式; 操作) { //... //代码语句 //... }Copy after login
- 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.
- 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.
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 : 19Copy 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 login10 20Copy 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 login10 20 40 50Copy after login
嵌套循环
do...while、while、for 循环都是可以相互嵌套的,这里不做过多的演示,大家可以自己去试验一下。
以上就是JAVA 入坑教程 | 章节六 循环结构体的内容,更多相关内容请关注PHP中文网(www.php.cn)!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Guide to Square Root in Java. Here we discuss how Square Root works in Java with example and its code implementation respectively.

Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Guide to Random Number Generator in Java. Here we discuss Functions in Java with examples and two different Generators with ther examples.

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.
