比较类似术语之间的差异

之间的区别

Home / 技术 / 它 / 编程 /循环和foreach循环之间的区别

循环和foreach循环之间的区别

2018年3月13日发表Lithmee

关键区别 - 用于循环与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.

循环和foreach循环之间的区别

Figure 01: Program with for loop and foreach loop

根据上述程序,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).在这里可用

Related posts:

气泡排序和插入排序之间的差异 ASP和ASP.NET之间的差异 Ajax和JQuery之间的区别Ajax和JQuery之间的区别 之间的区别this and super in Java_Figure 02之间的区别this and super in Java 实际参数和正式参数之间的差异实际参数和正式参数之间的差异

提交以下:编程Tagged With:比较循环和foreach循环,,,,增强循环,,,,用于循环,,,,用于循环and foreach Loop Differences,,,,用于循环和foreach循环相似性,,,,用于循环定义,,,,用于循环元素,,,,用于循环可读性,,,,用于循环用法,,,,用于循环vs foreach Loop,,,,foreach循环,,,,foreach循环Definition,,,,foreach循环元素,,,,foreach循环可读性,,,,foreach循环使用

关于作者:Lithmee

LithmeeMandula is a BEng (Hons) graduate in Computer Systems Engineering. She is currently pursuing a Master’s Degree in Computer Science. Her areas of interests in writing and research include programming, data science, and computer systems.

Comments

  1. 汤姆says

    July 26, 2020 at 2:47 pm

    凉爽的。

    回复
  2. 杰里说says

    February 2, 2022 at 12:26 am

    wow really cool.

    回复

Leave a ReplyCancel reply

您的电子邮件地址不会被公开。必需的地方已做标记*

Request Article

精选文章

冠状病毒和冷症状之间的差异

冠状病毒和冷症状之间的差异

之间的区别Coronavirus and SARS

之间的区别Coronavirus and SARS

冠状病毒和流感的差异

冠状病毒和流感的差异

冠状病毒和covid 19之间的差异

冠状病毒和covid 19之间的差异

You May Like

亲核和亲电的差异

亲核和亲电的差异

之间的区别Cataphoresis and Anaphoresis

之间的区别Cataphoresis and Anaphoresis

心源和低血容量休克有什么区别

心源和低血容量休克有什么区别

热量和温度之间的差异

爱尔兰和北爱尔兰之间的差异

爱尔兰和北爱尔兰之间的差异

最新的帖子

  • 等渗和等电物种有什么区别
  • What is the Difference Between Achalasia and GERD
  • What is the Difference Between Collagen and Keratin
  • k和氩有什么区别
  • 汞细胞和隔膜细胞有什么区别
  • 炎症和过敏有什么区别
  • Home
  • 空缺
  • 关于
  • Request Article
  • Contact Us

版权所有©2010-2018之间的区别。版权所有。使用条款and Privacy Policy:Legal。