编程语言中的循环 是一种功能,当某些条件评估为真时,它有助于重复执行一组指令/函数。在Java中,任何编程语言都不太可能提供四种执行循环的方法,即while循环、for循环、for each循环、do while循环,或者我们基本上可以调用它 三种类型的循环 在一些书中,as-for-each循环被视为增强for-loop。让我们详细讨论for循环。
一般来说,我们倾向于使用while循环,因为如果我们学习循环,我们会更好地理解它,但在饱和之后,作为程序员,我们倾向于使用for循环,因为它更干净,而且基础是直接进行的,为此,我们必须仔细掌握以下语法:
语法: 它由以下三部分组成:
- 变量初始化
- 根据需要迭代这些定义变量的具体条件
- 一种终止部分,我们通常在其中处理变量以达到终止条件状态。
for(initialization; boolean expression; update statement) { // Body of for loop }
这通常是for loop的基本朝圣结构。
让我们看一下使用for循环的一些基本示例,以及使用for循环时的常见陷阱,这使我们能够更好地了解内部工作,因为我们将在玩代码时描述内部工作。
用例1: 必须在for循环中提供表达式
For循环必须由loop语句中的有效表达式组成,否则可能导致无限循环。声明
for ( ; ; ) is similar towhile(true)
注: 这是高级编程的关键,因为它是编程中逻辑构建的起源。
实例
JAVA
// Java program to illustrate Infinite For loop // Main class public class GFG { // Main driver method public static void main(String[] args) { // For loop for (;;) { // Print statement everytime condition holds // true making body to execute System.out.println( "This is an infinite loop" ); } } } |
输出: 重复打印语句“这是一个无限循环”。
This is an infinite loopThis is an infinite loopThis is an infinite loopThis is an infinite loop......This is an infinite loop
用例2: 初始化多个变量
在Java中,无论是否在循环中使用for循环,都可以在for循环的初始化块中初始化多个变量。
例子:
JAVA
// Java Program to Illustrate Initializing Multiple // Variables in Initialization Block // Main class public class GFG { // Main driver method public static void main(String[] args) { // Declaring an integer variable int x = 2 ; // For loop to iterate for ( long y = 0 , z = 4 ; x < 10 && y < 10 ; x++, y++) { // Printing value/s stored in variable named y // defined inside body of for loop System.out.println(y + " " ); } // Printing value/s stored in variable named x // defined outside body of for loop System.out.println(x); } } |
0 1 2 3 4 5 6 7 10
在上面的代码中,for循环有一个简单的变化。在初始化块中声明并初始化两个变量。变量“z”未被使用。另外,其他两个组件包含额外的变量。因此,可以看出,这些块可能包含彼此不引用的额外变量。
用例3:在初始化块中重新声明变量
假设初始化变量已声明为整数。在这里,我们不能用另一种数据类型在for循环中重新声明它,如下所示:
例1:
JAVA
// Java Program to Illustrate Redeclaring a Variable // in Initialization Block // Main class public class GFG { // Main driver method public static void main(String[] args) { // Declaring an integer variable int x = 0 ; // Redeclaring above variable // as long will not work for ( long y = 0 , x = 1 ; x < 5 ; x++) { // Printing the value inside the variable System.out.print(x + " " ); } } } |
输出:
Example3.java:12: error: variable x is already defined in method main(String[]) for(long y = 0, x = 1; x < 5; x++)
在这里,x已经作为一个整数初始化为零,并在循环中用long数据类型重新声明。但是这个问题可以通过稍微修改代码来解决。这里,变量x和y以不同的方式声明。
例2:
JAVA
// Java Program to Illustrate Redeclaring a Variable // in Initialization Block // Main class public class GFG { // main driver method public static void main(String[] args) { // Declaring and initializing variables int x = 0 ; long y = 10 ; // For loop to iterate over till // custom specified check for (y = 0 , x = 1 ; x < 5 ; x++) { // Printing value contained in memory block // of the variable System.out.print(x + " " ); } } } |
输出:
1 2 3 4
用例4:初始化块中声明的变量必须是相同的类型
通常情况下,当我们声明一个变量时,如下所示:
int x, y;
这里两个变量的类型相同。它也是同样的for循环初始化块。
例子:
JAVA
// Java program to Illustrate Declaring a Variable // in Initialization Block // Main class public class GFG { // Main driver method public static void main(String[] args) { // Declaring integer variable // int x; // Note: This will cause error; // Redeclaring x as long will not work for ( long y = 0 , x = 1 ; x < 5 ; x++) { // Printing the value stored System.out.print(x + " " ); } } } |
1 2 3 4
用例5:循环中的变量只能在
初始化块中声明的变量只能在循环中访问,正如我们根据 变量范围的概念。
例子:
JAVA
// Java Program to Illustrate Scope of Initializing // Variables Within the oop // Main class public class GFG { // Main driver method public static void main(String[] args) { // x and y scope is declared only // within for loop for ( int x = 0 , y = 0 ; x < 3 && y < 3 ; x++, y++) { // Printing value stored in variable named y System.out.println(y + " " ); } // Printing value stored in variable named x // after inner loop is over System.out.println(x); } } |
错误:
Example5.java:13: error: cannot find symbol System.out.println(x);
在上面的例子中,变量x在循环之外是不可访问的。被注释的语句给出了一个编译器错误。
本文由 普里蒂·帕德西 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。