java循环_Java循环

java循环

Loop is an important concept of a programming that allows to iterate over the sequence of statements.

循环是编程的重要概念,它允许迭代语句序列。

Loop is designed to execute particular code block till the specified condition is true or all the elements of a collection(array, list etc) are completely traversed.

循环被设计为执行特定的代码块,直到指定的条件为真或完全遍历一个集合的所有元素(数组,列表等)为止。

The most common use of loop is to perform repetitive tasks. For example if we want to print table of a number then we need to write print statement 10 times. However, we can do the same with a single print statement by using loop.

循环最常见的用途是执行重复性任务。 例如,如果我们要打印一个数字表,则需要编写10次打印语句。 但是,我们可以使用循环对单个print语句执行相同的操作。

Loop is designed to execute its block till the specified condition is true.

循环旨在执行其块,直到指定条件为真为止。

Java provides mainly three loop based on the loop structure.

Java主要提供基于循环结构的三个循环。

  1. for loop

    for循环

  2. while loop

    while循环

  3. do while loop

    做while循环

We will explain each loop individually in details in the below.

我们将在下面分别详细解释每个循环。

对于循环 (For Loop)

The for loop is used for executing a part of the program repeatedly. When the number of execution is fixed then it is suggested to use for loop. For loop can be categories into two type.

for循环用于重复执行程序的一部分。 当执行次数固定时,建议使用for循环。 For循环可以将类别分为两种类型。

  1. for loop

    for循环

  2. for-each loop

    每个循环

For Loop Syntax:

对于循环语法:

Following is the syntax for declaring for loop in Java.

以下是在Java中声明for循环的语法。

for(initialization;condition;increment/decrement)
{  
//statement 
}

For循环参数: (For loop Parameters:)

To create a for loop, we need to set the following parameters.

要创建一个for循环,我们需要设置以下参数。

1)初始化 (1) Initialization)

It is the initial part, where we set initial value for the loop. It is executed only once at the starting of loop. It is optional, if we don’t want to set initial value.

这是初始部分,我们在其中设置循环的初始值。 在循环开始时仅执行一次。 如果我们不想设置初始值,它是可选的。

2)条件 (2) Condition)

It is used to test a condition each time while executing. The execution continues until the condition is false. It is optional and if we don’t specify, loop will be inifinite.

每次执行时都用于测试条件。 执行继续直到条件为假。 它是可选的,如果不指定,循环将是无限的。

3)声明 (3) Statement)

It is loop body and executed every time until the condition is false.

它是循环体,每次执行直到条件为假。

4)增减 (4) Increment/Decrement)

It is used for set increment or decrement value for the loop.

用于设置循环的增量或减量值。

for循环的数据流程图 (Data Flow Diagram of for loop)

This flow diagram shows flow of the loop. Here we can understand flow of the loop.

此流程图显示了循环流程。 在这里我们可以了解循环的流程。

for loop DFD

For loop Example

For循环示例

In this example, initial value of loop is set to 1 and incrementing it by 1 till the condition is true and executes 10 times.

在此示例中,循环的初始值设置为1,然后将其递增1,直到条件为true,然后执行10次。

public class ForDemo1
{  
public static void main(String[] args) 
	{  
		int n, i;
		n=2;
		for(i=1;i<=10;i++)
		{  
			System.out.println(n+"*"+i+"="+n*i);  
		}  
}  	
}
for-loop-output

Example for Nested for loop

嵌套for循环示例

Loop can be nested, loop created inside another loop is called nested loop. Sometimes based on the requirement, we have to create nested loop.

循环可以嵌套, 在另一个循环内部创建的循环称为嵌套循环。 有时根据需求,我们必须创建嵌套循环。

Generally, nested loops are used to iterate tabular data.

通常,嵌套循环用于迭代表格数据

public class ForDemo2
{  
public static void main(String[] args) 
{  
for(inti=1;i<=5;i++)
{  
for(int j=1;j<=i;j++)
{  
				System.out.print("* ");  
}  
System.out.println(); 
}  
}  
}
nested-for-loop program

每个循环 (for-each Loop)

In Java, for each loop is used for traversing array or collection elements. In this loop, there is no need for increment or decrement operator.

在Java中,每个循环用于遍历数组或集合元素。 在此循环中,不需要增量或减量运算符。

For-each loop syntax

每个循环的语法

Following is the syntax to declare for-each loop in the Java.

以下是在Java中声明for-each循环的语法。

for(Type var:array)
{  
//code for execution
}

Example:

例:

In this example, we are traversing array elements using the for-each loop. For-each loop terminates automatically when no element is left in the array object.

在此示例中,我们使用for-each循环遍历数组元素。 当array对象中没有剩余元素时, for-each 循环会自动终止

public class ForEachDemo1
{  
public static void main(String[] args) 
		{    
			inta[]={20,21,22,23,24};   
			for(int i:a)
{  
				System.out.println(i);  
			}  
}  
}
for-each-loop

While循环 (While Loop)

Like for loop, while loop is also used to execute code repeatedly. a control statement. It is used for iterating a part of the program several times. When the number of iteration is not fixed then while loop is used.

像for循环一样,while循环也用于重复执行代码。 控制声明。 它用于多次迭代程序的一部分。 如果迭代次数不固定,则使用while循环。

Syntax:

句法:

while(condition)
{  
//code for execution
}
While块的数据流图 (Data-flow-diagram of While Block)
while loop DFD

Example:

例:

In this example, we are using while loop to print 1 to 10 values. In first step, we set conditional variable then test the condition and if condition is true execute the loop body and increment the variable by 1.

在此示例中,我们使用while循环打印1到10个值。 第一步,我们设置条件变量,然后测试条件,如果条件为true,则执行循环体并将变量增加1。

public class WhileDemo1
{  
	public static void main(String[] args) 
	{  
		inti=1;  
		while(i<=10)
		{  
			System.out.println(i);  
			i++;  
		}  
	}  
}
while-loop-output

Example for infinite while loop

无限while循环示例

A while loop which conditional expression always returns true is called infinite while loop. We can also create infinite loop by passing true literal in the loop.

条件表达式始终返回true的 while循环称为无限while循环。 我们还可以通过在循环中传递真实文字来创建无限循环。

Be careful, when creating infinite loop because it can issue memory overflow problem.

创建无限循环时要小心,因为它可能会引起内存溢出问题。

public class WhileDemo2 
{  
	public static void main(String[] args) 
	{  
		while(true)
		{  
		System.out.println("infinitive while loop");  
		}  
	}  
}
infinite-while-loop jpg

循环执行 (do-while loop)

In Java, the do-while loop is used to execute statements again and again. This loop executes at least once because the loop is executed before the condition is checked. It means loop condition evaluates after executing of loop body.

在Java中,do-while循环用于一次又一次地执行语句。 该循环至少执行一次,因为该循环是在检查条件之前执行的。 这意味着在执行循环体后评估循环条件

The main difference between while and do-while loop is, in do while loop condition evaluates after executing the loop.

while和do-while循环之间的主要区别在于 ,在do while循环中条件是在执行循环后评估的。

Syntax:

句法:

Following is the syntax to declare do-while loop in Java.

以下是在Java中声明do-while循环的语法。

do
{  
//code for execution 
}
while(condition);
同时执行数据块的流程图 (Data Flow Diagram of do-while Block)
do-while-dfd diagram

Example:

例:

In this example, we are printing values from 1 to 10 by using the do while loop.

在此示例中,我们使用do while循环打印从1到10的值。

public class DoWhileDemo1 
{  
public static void main(String[] args) 
{  
inti=1;  
do
{  
System.out.println(i);  
i++;  
}while(i<=10);  
        }  
}
output-of-do-while-program

Example for infinite do-while loop

无限do-while循环示例

Like infinite while loop, we can create infinite do while loop as well. To create an infinite do while loop just pass the condition that always remains true.

像无限while循环一样,我们也可以创建无限do while循环。 要创建一个无限的do while循环,只需传递始终保持为真的条件即可。

public class DoWhileDemo2 
{  
public static void main(String[] args) 
{  
        do
{  
System.out.println("infinitive do while loop");  
}while(true);  
}  
}
output-of-do-while-infinte-program

翻译自: https://www.studytonight.com/java/loops-in-java.php

java循环

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值