以下文章提供了 Java 中 For 循环的概述。循环是Java中的一个概念,当某个条件成立时,重复执行一组语句。 Java 提供了三种执行循环的方法。
他们是:
开始您的免费软件开发课程
网络开发、编程语言、软件测试及其他
下面是提到的步骤:
Java 是一个入口控制循环,因为在执行语句之前检查条件。
Java 程序中 for 循环的语法可以使用以下内容轻松执行。
语法:
for (initialization condition; testing condition; increment/decrement) { statement(s) or print statement }
流程图:
下面给出的是提到的示例::
在第一个示例中,我们将使用 for 循环在 Java 程序中生成前 10 个数字。下面给出了示例代码以及输出。类的名称是forLoopDemo。循环语句分为三个阶段。它从 1 到 10 运行,生成其间的所有自然数。
代码:
class forLoopDemo { public static void main(String args[]) { // for loop 0begins when x=1 // and runs till x <=10 System.out.println("OUTPUT OF THE FIRST 10 NATURAL NUMBERS"); for (int x = 1; x <= 10; x++) System.out.println(+ x) } }
输出:
第一个示例之后,我们继续第二个示例,其中我们引入一个数组并打印数组中的某些元素。打印数组中元素的语法如下。
语法:
for (T element:Collection obj/array) { statement(s) }
示例代码以及输出如下所示。换句话说,它也被称为增强型 for 循环。下面的代码也显示了简单的循环格式。
代码:
// Java program to illustrate enhanced for loop public class enhanced for loop { public static void main(String args[]) { String array[] = {"Ron", "Harry", "Hermoine"}; //enhanced for loop for (String x:array) { System.out.println(x); } /* for loop for same function for (int i = 0; i < array.length; i++) { System.out.println(array[i]); } */ } }
输出:
在示例 3 中,我们将检查无限 for 循环。无限 for 循环是不停顿地运行的循环。这是使用 for 循环的缺点之一。可以故意创建无限循环。在大多数情况下,无限 for 循环是错误创建的。在下面的代码中,由于没有提供更新语句,因此创建了无限循环。
代码:
//Java program to illustrate various pitfalls. public class LooppitfallsDemo { public static void main(String[] args) { // infinite loop because condition is not apt // condition should have been i>0. for (int i = 5; i != 0; i -= 2) { System.out.println(i); } int x = 5; // infinite loop because update statement // is not provided. while (x == 5) { System.out.println("In the loop"); } } }
输出:
上面显示了示例输出以及 Java 虚拟机的运行情况。 Java虚拟机无限期地运行,并且不会停止。可以通过右键单击 JVM 图标(如图所示)然后停止它来停止 JVM。另外,还显示了快捷键,即 Control + Shift + R。
在示例 4 中,我们将看到另一个 for 循环应用程序,它是一个嵌套的 for 循环。嵌套 for 循环是指 for 循环内有一个 for 循环。这意味着两个 for 循环彼此内部。它们通常用于在 Java 平台中打印复杂的图案。下面显示了嵌套 for 循环的示例。
这里的类名为 PyramidExample。然后声明 main()。之后,声明两循环控制变量。一个是循环控制变量“i”,另一个是循环控制变量“j”。然后在循环控制中打印“*”。给出新行以便维持金字塔结构的给定格式。在此代码中,程序运行了 5 次。然而,通过增加第“i”个循环控制变量的值,我们可以确保金字塔更大。
代码:
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 } } }
Output:
In this example, we are going to see how a for loop goes through each and every element of an array and prints them.
In the below code, the class name is GFG. The package java. io .* is imported here. Also, the throws IO Exception is used at the main(), which throws and removes any exception arriving at the piece of code. The ar.length() returns the length of the array. The variable x stores the element at the “i”th position and prints it. This code is one of the easiest ways of showing how to access array elements using for loop function.
Code:
// Java program to iterate over an array // using for loop import java.io.*; class GFG { public static void main(String args[]) throws IOException { int ar[] = { 1, 2, 3, 4, 5, 6, 7, 8 }; int i, x; // iterating over an array for (i = 0; i < ar.length; i++) { // accessing each element of array x = ar[i]; System.out.print(x + " "); } } }
Output:
In this example, we are going to see whether a number is a palindrome or not. In this also, a for loop is used. A palindrome number is one which when reversed, represents the same number.
Examples are 121, 1331, 4334, etc.
Code:
import java.util.*; class PalindromeExample2 { public static void main(String args[]) { String original, reverse = ""; // Objects of String class Scanner in = new Scanner(System.in); System.out.println("Enter a string/number to check if it is a palindrome"); original = in.nextLine(); int length = original.length(); for ( int i = length - 1; i >= 0; i-- ) reverse = reverse + original.charAt(i); if (original.equals(reverse)) System.out.println("Entered string/number is a palindrome."); else System.out.println("Entered string/number isn't a palindrome."); } }
Output:
In this article, we saw how a for loop is used in many cases. The condition is checked at the beginning of the loop, and then if the condition is satisfied, then it is used in the remaining part of the loop. It is very similar to a while loop which is also an entry-controlled loop. It is in contrast to the do-while loop in which the condition is checked at the exit of the loop.
For loops are used in Java and used in C, C++, Python, and many other programming languages. Mostly they are used to print patterns in menu-driven programs to check the behavior of a number and much more.
以上是Java 中的 For 循环的详细内容。更多信息请关注PHP中文网其他相关文章!