Java for 循环是一个控制流语句,它多次迭代程序的一部分。 For 循环是Java最常用的循环。如果我们提前知道迭代次数,那么 for-loop 是最好的选择。
句法:
for( intitializationsection ; conditional check ; increment/decrement section)
{
// Code to be executed
}
Curly braces in for loop is optional without curly braces in for-loop we can take only one statement under for-loop which should not be a declarative statement and if we write declarative statement there then we will get compile-time error.
例子
Java
// Java Program to illustrate the use of
// for loop
// Importing all input output classes
import java.io.*;
// Importing all classes from
// java.util package
import java.util.*;
// Class
class GFG {
// Main driver method
public static void main(String[] args)
{
// 1st for-loop
// Iteration ocer 5 elements using for loop
for (int i = 1; i <= 5; i++) {
// Print statement
System.out.println("GFG!");
}
// 2nd for-loop
// Declaring and intializatiization a variable
// so we will get compile time error
for (int i = 1; i <= 1; i++)
int x = 0;
}
}
Java
// Java Program to show usage of for-each loop
// Importing all classes from
// java.uti package
// Importing all input ouptput classes
import java.io.*;
import java.util.*;
// Class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Declaring and initializing the integer array
// Custom integer entries in an array
int[] array = { 1, 2, 3, 4, 5, 6 };
// Accessing the element of array
// using for-each loop
for (int a : array) {
// Print all elements of an array
System.out.println(a);
}
}
}
输出:
案例 1:没有第二个 for 循环
GFG
GFG
GFG
GFG
GFG
案例 2:存在第二个 for 循环
prog.java:28: error: variable declaration not allowed here
int x=0;
^
1 error
输出说明:
- 在初始化部分,这部分在循环生命周期中只会执行一次。在这里我们可以声明并初始化一个局部变量 for 循环。在这里,我们可以声明任意数量的变量,但应该有误同一类型的,如果我们试图声明不同的数据类型,那么我们将得到一个编译–时间错误。在本节中,我们可以采用任何有效的Java语句,包括“ System.out.println()” 。
- 在条件检查期间,我们可以采用任何应该是布尔类型的有效Java表达式。这部分是可选的,如果我们不采取任何东西那么编译器会一直在这里放置一个真正的价值。
- 在里面 Increment/Decrement 部分,我们可以增加或减少我们可以初始化的值。在本节中,我们可以采用任何有效的Java语句,包括“ System.out.println()”,并且本节也是可选的。
增强的 for 循环(for-each 循环)
这for循环在Java版本1.5引入d,它是迭代的计划的一部分,多次也是流程控制语句。这个 for 循环提供了另一种遍历数组或集合的方法,因此它主要用于遍历数组或集合。这个循环还使代码更具可读性,并减少了代码中出现错误的机会。
句法:
for(data-type variable : array | collection)
{
// Code to be executed
}
例子:
Java
// Java Program to show usage of for-each loop
// Importing all classes from
// java.uti package
// Importing all input ouptput classes
import java.io.*;
import java.util.*;
// Class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Declaring and initializing the integer array
// Custom integer entries in an array
int[] array = { 1, 2, 3, 4, 5, 6 };
// Accessing the element of array
// using for-each loop
for (int a : array) {
// Print all elements of an array
System.out.println(a);
}
}
}
1
2
3
4
5
6
在对这两个程序进行可视化之后,我们发现了一些决定性的差异,这些差异以表格形式描述,以帮助您清楚地理解。
Normal for-loop | Enhanced for-loop |
---|---|
This for-loop is present from JDK1 | This for loop is present from JDK5 |
In a normal for-loop, we can increase the counter as per our wish by using i=i+x( where x is any constant x=1,2,3…) |
But enhanced for loop will execute in a sequential manner i.e counter will always increase by one. |
Using this for loop we can iterate on any container object. | We can only iterate on that container by using this loop to implement the iterable interface. |
In this for-loop, we can iterate in both decrement or increment order. | But in this for-loop, we can iterate only in increment order. |
In this for-loop, we can replace elements at any specific index. | But in this for-loop, we don’t have access to the index, so we cannot replace elements at any specific index. |
By using normal for-loop we can print array elements either in the original order or in reverse order. | But in the for-each loop, we can print array element only in the original order, not in reverse order |
Example: Printing element in a 1D array int[ ] x={1,2,3}; for(int i=0;i { System.out.println(x[i]); } |
Example: Printing element in a 1D array using for-each loop int[ ] x={1,2,3}; for(int a : x) { System.out.println(a); } |
Example: Printing element in a 2D array using for loop int[ ][ ] x={{1,2,3},{4,5,6}}; for(int i=0;i for(int j=0; j System.out.println(x[i][j]); } } |
Example: Printing element in a 2D array using for-each loop int[ ][ ] x={{1,2,3},{4,5,6}} ; for(int[ ] x1 :x){ for(int x2 : x1) { System.out.println(x2); } } |