📜  Java for循环

📅  最后修改于: 2020-09-25 04:25:41             🧑  作者: Mango

Java中的循环

在编程语言中,当某些条件变为真时,循环用于重复执行一组指令/功能。Java中有三种循环类型。

  • for循环
  • 循环执行

Java For循环vs While循环vs Do While循环

Comparison for loop while loop do while loop
Introduction The Java for loop is a control flow statement that iterates a part of the programs multiple times. The Java while loop is a control flow statement that executes a part of the programs repeatedly on the basis of given boolean condition. The Java do while loop is a control flow statement that executes a part of the programs at least once and the further execution depends upon the given boolean condition.
When to use If the number of iteration is fixed, it is recommended to use for loop. If the number of iteration is not fixed, it is recommended to use while loop. If the number of iteration is not fixed and you must have to execute the loop at least once, it is recommended to use the do-while loop.
Syntax
for(init;condition;incr/decr){  
// code to be executed 
}
while(condition){  
//code to be executed 
}
do{  
//code to be executed  
}while(condition); 
Example
//for loop  
for(int i=1;i<=10;i++){  
System.out.println(i);  
}  
//while loop  
int i=1;  
while(i<=10){  
System.out.println(i);  
i++;  
}  
//do-while loop  
int i=1;  
do{  
System.out.println(i);  
i++;  
}while(i<=10); 
Syntax for infinitive loop
for(;;){  
//code to be executed  
}
while(true){  
//code to be executed  
}
do{  
//code to be executed  
}while(true);  

Java For循环

Javafor循环用于多次迭代程序的一部分。如果迭代次数是固定的,则建议使用for循环。

Java中有三种类型的for循环。

  • 简单的循环
  • For-each或增强型For循环
  • 标记为循环

Java简单循环

一个简单的for循环与C/C++相同。我们可以初始化变量,检查条件和递增/递减值。它包括四个部分:

  • 初始化 :这是循环开始时执行一次的初始条件。在这里,我们可以初始化变量,也可以使用已经初始化的变量。这是一个可选条件。
  • 条件 :这是第二个条件,每次执行时都要测试循环条件。它继续执行直到条件为假。它必须返回布尔值true或false。这是一个可选条件。
  • 语句 :循环语句每次执行一次,直到第二个条件为假。
  • 递增/递减 :递增或递减变量值。这是一个可选条件。

句法:

"for(initialization;condition;incr/decr){  
//statement or code to be executed  
}  

流程图:

例:

"//Java Program to demonstrate the example of for loop  
//which prints table of 1  
public class ForExample {  
public static void main(String[] args) {  
    //Code of Java for loop  
    for(int i=1;i<=10;i++){  
        System.out.println(i);  
    }  
}  
}  

输出:

1
2
3
4
5
6
7
8
9
10

Java嵌套循环

如果在另一个循环中有一个for循环,则称为嵌套for循环。每当执行外循环时,内循环就完全执行。

例:

"public class NestedForExample {  
public static void main(String[] args) {  
//loop of i  
for(int i=1;i<=3;i++){  
//loop of j  
for(int j=1;j<=3;j++){  
        System.out.println(i+" "+j);  
}//end of i  
}//end of j  
}  
}  

输出:

1 1 1 2 1 3 2 1 2 2 2 3 3 1 3 2 3 3

金字塔示例1:

"public class PyramidExample {  
public static void main(String[] args) {  
for(int i=1;i<=5;i++){  
for(int j=1;j<=i;j++){  
        System.out.print("* ");  
}  
System.out.println();//new line  
}  
}  
}  

输出:

* 
* * 
* * * 
* * * * 
* * * * *

金字塔示例2:

"public class PyramidExample2 {  
public static void main(String[] args) {  
int term=6;  
for(int i=1;i<=term;i++){ for(int j=term;j>=i;j--){  
        System.out.print("* ");  
}  
System.out.println();//new line  
}  
}  
}  

输出:

* * * * * * 
* * * * * 
* * * * 
* * * 
* * 
*

Java for-each循环

for-each循环用于遍历Java中的数组或集合。它比简单的for循环更易于使用,因为我们不需要增加值并使用下标表示法。

它基于元素而不是索引工作。它在定义的变量中一一返回元素。

句法:

"for(Type var:array){  
//code to be executed  
}  

例:

"//Java For-each loop example which prints the  
//elements of the array  
public class ForEachExample {  
public static void main(String[] args) {  
    //Declaring an array  
    int arr[]={12,23,44,56,78};  
    //Printing array using for-each loop  
    for(int i:arr){  
        System.out.println(i);  
    }  
}  
}  

输出:

12
23
44
56
78

Java标记为循环

我们可以为每个Javafor循环命名。为此,我们在for循环之前使用label。如果我们嵌套了for循环,则可以中断/继续特定于for循环,这很有用。

通常,break和continue关键字仅中断/继续最里面的for循环。

句法:

"labelname:  
for(initialization;condition;incr/decr){  
//code to be executed  
}  

例:

"//A Java program to demonstrate the use of labeled for loop  
public class LabeledForExample {  
public static void main(String[] args) {  
    //Using Label for outer and for loop  
    aa:  
        for(int i=1;i<=3;i++){  
            bb:  
                for(int j=1;j<=3;j++){  
                    if(i==2&&j==2){  
                        break aa;  
                    }  
                    System.out.println(i+" "+j);  
                }  
        }  
}  
}  

输出:

1 1
1 2
1 3
2 1

如果使用breakbb;,它将仅中断内部循环,这是任何循环的默认行为。

"public class LabeledForExample2 {  
public static void main(String[] args) {  
    aa:  
        for(int i=1;i<=3;i++){  
            bb:  
                for(int j=1;j<=3;j++){  
                    if(i==2&&j==2){  
                        break bb;  
                    }  
                    System.out.println(i+" "+j);  
                }  
        }  
}  
}  

输出:

1 1
1 2
1 3
2 1
3 1
3 2
3 3

Java不定式循环

如果使用两个分号;;在for循环中,它将是不定式的for循环。

句法:

"for(;;){  
//code to be executed  
}  

例:

"//Java program to demonstrate the use of infinite for loop  
//which prints an statement  
public class ForExample {  
public static void main(String[] args) {  
    //Using no condition in for loop  
    for(;;){  
        System.out.println("infinitive loop");  
    }  
}  
}  

输出:

infinitive loop
infinitive loop
infinitive loop
infinitive loop
infinitive loop
ctrl+c

现在,您需要按ctrl+c退出程序。