📅  最后修改于: 2020-09-25 04:25:41             🧑  作者: Mango
在编程语言中,当某些条件变为真时,循环用于重复执行一组指令/功能。Java中有三种循环类型。
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); |
Javafor循环用于多次迭代程序的一部分。如果迭代次数是固定的,则建议使用for循环。
Java中有三种类型的for循环。
一个简单的for循环与C/C++相同。我们可以初始化变量,检查条件和递增/递减值。它包括四个部分:
句法:
"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
如果在另一个循环中有一个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
}
}
}
输出:
* * * * * * * * * * * * * * * * * * * * *
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
我们可以为每个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
如果使用两个分号;;在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退出程序。