关键区别 - 用于循环与foreach循环
Both for loop and foreach loop are control structures that are used to repeat a block of statements. There are repetition control structures in programming to execute a block of statements again and again. One common control structure is for a loop. A for loop is a control flow structure used foriteration这允许代码重复执行。它包含初始化,测试表达式和更新表达式。要重复的语句包含在卷曲括号中。foreach循环改进了循环。它增加了代码的可读性,并且很容易编写。用于循环和foreach循环都用于重复一组语句,但语法是不同的。这关键区别between for Loop and foreach loop is that thefor loop is a general purpose control structure while the foreach loop is an enhanced for loop that is applicable only to数组and collections.
CONTENTS
1。概述和关键差异
2。循环是什么
3。What is foreach Loop
4。Similarities Between for Loop and foreach Loop
5。Side by Side Comparison – for Loop vs foreach Loop in Tabular Form
6.Summary
循环是什么?
这for loop is a common repetition structure. It helps to iterate through a statement or a set of statements in the program. The syntax of the for loop is as follows.
(初始化;测试表达式;更新表达式){
// for循环中的代码
}
首先初始化发生。那么测试实验ression is checked. If the evaluated answer is true, the code inside the for loop executes. At the end of the last statement of the for loop, the update expression is evaluated. Then the test expression is evaluated again. If it is true, the code inside the for loop executes. At the end of the for loop, the update expression is evaluated again and checked with the test expression. This process repeats until the test expression becomes false. When it becomes false, the for loop terminates.
根据上述程序,Array1可以存储类型整数的多个元素。在for循环中,i为0。它少于5。因此,打印了数组的0索引元素。它是数字10。然后,由于更新表达式,i被增加。现在的i值是1。它小于5。因此,阵列1的第一个索引元素是打印的。再次,我被递增。这个过程仍在继续。当i值变为5时,测试表达式为false,因为它不少于5。因此,循环终止。
What is foreach Loop?
foreach循环是检索阵列或集合元素的便捷方法。它可以用作用于循环的替代方案。它被称为foreach循环,因为它通过数组或集合的每个元素进行迭代。foreach循环的语法如下。
for(数据类型项目:集合){
//每个循环中的代码
}
该集合是应该迭代的数组或集合。该项目是集合中的单个元素。foreach循环通过每个元素迭代,并将该元素存储在变量项目中。然后执行foreach循环内的语句。
According to the above program, the array1 stores multiple integers. The enhanced for loop is used to iterate through the elements of the array. Each element is stored to the variable i and the code inside the foreach loop executes. The foreach loop achieves the same tasks as the for loop, but it is more readable and easy to write. Therefore, it is known as ‘增强循环‘。
What is the Similarity Between for Loop and foreach Loop?
- Both for loop and foreach loop can be used to executes a set of statements multiple times.
循环和foreach循环有什么区别?
用于循环vs foreach Loop |
|
这for loop is a control structure for specifying iteration that allows code to be repeatedly executed. | foreach循环是用于遍历数组或集合中的项目的控制结构。 |
元素检索 | |
A循环可用于检索一组特定的元素。 | foreach循环不能用于检索一组特定的元素。 |
可读性 | |
与foreach循环相比,for循环更难读写。 | 与for循环相比,foreach循环更容易读写。 |
用法 | |
for循环用作通用循环。 | foreach循环用于阵列和集合。 |
概括 -用于循环vs foreach Loop
在编程中,有时需要重复代码。FO循环通常用于完成此任务。循环A是用于迭代的控制流结构,允许代码重复执行。改进了易于读写的循环循环。循环和foreach循环之间的区别在于,for循环是一种通用控制结构,而foreach循环则是适用于循环的增强循环,仅适用于阵列和集合。
参考:
1。Programiz,Java for-Each Loop (Enhanced for Loop).在这里可用
汤姆says
凉爽的。
杰里说says
wow really cool.