The syntax of the traditional for loop is as follows:
for (initialization; condition; increment/decrement) {
// Code to be executed
}
增强型for循环,也被称为“foreach”循环,具有不同的语法:
for (datatype variable : collection) {
// Code to be executed
}
The conventional for loop consists of three parts: initialization, condition, and increment/decrement. The initialization step is executed as it were once at the starting. The condition is evaluated before each cycle, and on the off chance that it is genuine, the code inside the loop is executed. After each cycle, the increment/decrement step is performed.
On the other hand, the improved for loop simplifies the language structure by eliminating the requirement for initialization, condition, and increment/decrement steps. It directly iterates over a collection or array.
Initialize a variable.
Specify the condition for executing the loop.
Execute the code block inside the loop.
Increment or decrement the variable.
的中文翻译为:
public class TraditionalForLoopExample {
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
System.out.println("Iteration: " + i);
}
}
}
Iteration: 0
Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4
的中文翻译为:
代码以声明一个名为TraditionalForLoopExample的公共类开始
在课堂的范围内,人们可以发现一个被称为主方法的基本过程。这个组件作为程序开始执行的入口。
The for keyword indicates the start of the loop construct.
int i = 0 initializes a loop control variable i with an initial value of 0.
i < 5 is the condition that determines whether the loop should continue executing. As long as i is less than 5, the loop will continue iterating.
This code employs an iteration statement in order to update an incrementing integer variable named ‘i’. In each subsequent cycle through our program loop implementation, we add one (via ‘++’, as mentioned) to whatever present value for ‘i’ we encounter via our command stream here- allowing us to track current iterators with ease. Contained within a block cited via brackets {}, we have everything that comes under our programmatic umbrella when we talk about ‘the loop.’ Herein lies a special command – System.out.println(“Iteration: ” + i); – outputting data comprising both text (“Iteration”) and variables on-screen at present when run.
循环会继续执行,直到条件 i < 5 变为假。在这种情况下,当 i 达到值为 5 时,条件不再为真,循环终止。
声明一个变量来保存集合中的每个元素。
Specify the collection to be iterated.
在循环中执行代码块,使用声明的变量访问每个元素。
Consider the following example of the enhanced for loop
的中文翻译为:
public class EnhancedForLoopExample {
public static void main(String[] args) {
String[] fruits = {“Apple”, “Banana”, “Orange”};
for (String fruit : fruits) {
System.out.println(“Fruit: ” + fruit);
}
}
}
Fruit: Apple
Fruit: Banana
Fruit: Orange
的中文翻译为:
代码以声明一个名为EnhancedForLoopExample的公共类开始。
在课堂的范围内,人们可以发现一个被称为主方法的基本过程。这个组件作为程序开始执行的入口。
声明了一个名为fruits的String类型的数组。这行代码创建了一个名为fruits的数组,可以存储String值。该数组被初始化为三个元素:”Apple”,”Banana”和”Orange”。
The enhanced for loop simplifies the process of iterating over arrays and collections.
循环遍历水果数组中的每个元素,将当前元素赋值给循环变量fruit。
对于每次迭代,执行用花括号{}括起来的代码块,可以轻松地打印出水果数组中的每个单独元素。输出包括一个静态标签“Fruit:”和一个表示当前迭代过程中任意特定项的变量值,通过System.out.println(“Fruit: ” + fruit);。这种方法消除了与手动索引技术常用于遍历数组等数据集相关的顺序错位或索引间隙的风险。
差异点 |
Traditional for Loop |
增强型for循环 |
---|---|---|
Syntax |
Requires explicit initialization, condition, and increment/decrement steps |
简化语法,无需初始化、条件或增减步骤 |
Iteration Control |
提供了更多对初始化、条件和增量/减量步骤的控制 |
自动迭代集合或数组的元素 |
访问元素 |
Can access elements using an index variable and array/collection size |
直接访问元素,无需索引或大小 |
代码可读性 |
Requires explicit handling of iteration details |
通过抽象迭代细节来提高代码可读性 |
Use Cases |
Suitable for situations where explicit control over iteration is necessary |
Ideal for iterating over collections or arrays without complex iteration requirements |
Both the traditional for loop and the enhanced for loop have their own significance in Java programming. The conventional for loop gives more adaptability and control over the emphasis handle, permitting the software engineer to characterize the initialization, condition, and increment/decrement steps. It is commonly utilized when the number of cycles or the particular conditions are known in development.
如果您发现该资源为电子书等存在侵权的资源或对该资源描述不正确等,可点击“私信”按钮向作者进行反馈;如作者无回复可进行平台仲裁,我们会在第一时间进行处理!
加入交流群
请使用微信扫一扫!