比较类似术语之间的差异

Difference Between

家 / Technology / IT / Programming /循环和循环之间的区别

循环和循环之间的区别

March 12, 2018Posted byLithmee

Key Difference – forvs循环

程序是用编程语言编写的一组指令,以执行特定任务。它可以是逻辑操作或数学操作。通常,程序中的语句彼此执行。有时有必要一次又一次地执行一组语句。控制结构用于完成此任务。其中两个是为了循环。这些结构有助于执行一系列代码,直到给定条件为真。FO循环的语法包括初始化,测试表达式和更新表达式。While循环的语法包含测试表达式。本文讨论了for和while循环之间的区别。 Thekey differencebetween for and while loop is that当数量的数量时,可以使用for循环iterations已知,当迭代次数尚不清楚时,可以使用while循环。

内容

1.Overview and Key Difference
2.What is for Loop
3.What is while Loop
4.循环之间的相似之处
5.Side by Side Comparison – for vs while Loop in Tabular Form
6.概括

What is for Loop?

这为了loop is used in many programming languages such asC, Javaetc. It is used to execute a set of statements multiple times. The syntax of the for loop is as follows.

为了(initialization; test expression, update){

// for循环中的语句

}

这initialization expression executes only once. Then, the test expression is evaluated. The test expression can containvariables,值,恒定和运算符。这是一个布尔的表达。如果评估的表达式为真,则在for循环内部执行代码。到达循环结束后,执行了更新表达式。它可以是增量或减少。再次检查测试表达式。如果评估的表达式为真,则for循环中的语句执行。在循环结束时,执行更新表达式。此过程重复直到测试表达式为错误。当测试表达式为false时,for循环将终止,并且控件将传递给for循环之后的下一个语句。

循环和循环之间的区别

图01:带有循环的程序来计算5个数字的总和

这above program is to find the summation of first five numbers, which are 1,2,3,4 and 5. In the for loop, i is 1. It is less than 5. So the sum is calculated. Initially, sum is 0. It is added to i which is 1. The total is assigned to variable sum. Now the sum is 1. Then the update expression is evaluated. The i is incremented by one. Now i is 2. It is less than 5. So, the sum is calculated. The previous sum value is 1 and it is added to i value which is 2. Now the sum is 3. The update expression is evaluated and i is incremented by 1. Now it is 3. This process repeats. When i becomes 6, the expression becomes false because 6 is not equal or less than 5. Therefore, the for loop terminates. Finally, the sum of all five numbers is printed to the screen.

What is while Loop?

只要给定条件为真,while循环就执行目标语句。while循环的语法如下。

while(test expression){

//statements inside the while loop

}

这while loop contains a test expression. It is a Boolean expression. If the expression evaluated is true, then the statements inside the while loop execute. At the end of those statements, the test expression is evaluated again. This process repeats until the test expression becomes false. When it becomes false, the while loop terminates and the control is passed to the statement after the while loop.

Key Difference Between for and while Loop

Figure 02: A program with while loop to calculate sum of 5 numbers

根据上述程序,总和初始化为0,I初始化为1。在while循环中,评估了i值。它小于5。因此,计算总和。总和的初始值为0。它添加到i值1中。现在总和为1。然后,i值由一个递增。现在我值为2。它小于5。因此总和是计算的。当前总和1添加到2为2的i值中。现在的总和为3。i值再次增加。现在i值为3。此过程重复。当我估算6时,表达式变为false,因为它不小于或等于5。因此,while循环终止。最后,将和值打印到屏幕上。如果没有诸如i ++的增量,则i值保持在相同的1个。它小于5。条件始终是正确的。因此,这将是一个无限的循环。

What are the Similarities Between for and while Loop?

  • Both for and while loop are repetition control structures in programming.
  • 循环的执行取决于测试表达式。

What is the Difference Between for and while Loop?

为了vs while Loop

这为了loop is a repetition control structure that allows the programmer to efficiently write a loop that needs to execute a specific number of times. While循环是一个重复控制结构,只要给定条件为真,就可以执行目标语句。
Usage
这为了loop can be used when the number of iterations is known. 这while loop can be used when the number of iterations is not known.
Initialization
这initialization occurs once in for loop. In while loop, if the initialization statement is inside the loop, then the initialization is done each time the loop iterates.

概括–为了vs循环

在编程中,有时需要多次重复一组语句。有重复控制结构可以实现此任务。其中两个是为了循环。for和while循环之间的区别在于,当迭代的数量已知时,使用for循环,并且当迭代数量尚不清楚时使用while循环。

Reference:

1.Point, Tutorials. “For loop in C.”,Tutorials Point, 8 Jan. 2018, www.tutorialspoint.com/cprogramming/c_for_loop.htm.
2.Point, Tutorials. “While loop in C.”,Tutorials Point, 8 Jan. 2018, www.tutorialspoint.com/cprogramming/c_while_loop.htm.

Related posts:

Difference Between Of and For in English GrammarDifference Between Of and For in English Grammar Difference Between To and For in English GrammarDifference Between To and For in English Grammar Difference Between Definite Loop and Indefinite Loop Difference Between For and SinceDifference Between For and Since java_figure中的差异和超级02与Java中的超级差异之间的区别

Filed Under:Programming标记为:比较循环时,为了,为了and while Loop Differences,为了and while Loop Similarities,为了definition,为了Function,为了Initialization,为了Usage,为了vs while Loop,while loop,循环Definition,循环Function,循环Initialization,循环Usage

About the Author:Lithmee

Lithmee Mandula是计算机系统工程的孟加(荣誉)。她目前正在攻读计算机科学硕士学位。她在写作和研究方面的兴趣领域包括编程,数据科学和计算机系统。

发表评论取消回复

Your email address will not be published.Required fields are marked*

请求文章

Featured Posts

Difference Between Coronavirus and Cold Symptoms

Difference Between Coronavirus and Cold Symptoms

冠状病毒和SARS之间的差异

冠状病毒和SARS之间的差异

Difference Between Coronavirus and Influenza

Difference Between Coronavirus and Influenza

Difference Between Coronavirus and Covid 19

Difference Between Coronavirus and Covid 19

你可能喜欢

Difference Between T Cell Dependent and Independent Antigens

Difference Between T Cell Dependent and Independent Antigens

Difference Between HDL And LDL

Difference Between Asian and American Business Culture

Difference Between Asian and American Business Culture

Difference Between ATP and ADP

Difference Between ATP and ADP

Difference Between Holandric and Sex Linked Gene

Difference Between Holandric and Sex Linked Gene

Latest Posts

  • 百里香林纳尔和百里香百里香有什么区别
  • What is the Difference Between MCT and LCT
  • 休闲服和睡衣有什么区别
  • What is the Difference Between BHA and BHT
  • What is the Difference Between Amyloidosis and Multiple Myeloma
  • What is the Difference Between Myristic and Stearic Acid
  • 家
  • Vacancies
  • About
  • 请求文章
  • 联系我们

Copyright © 2010-2018Difference Between. All rights reserved.Terms of Use和隐私政策:合法的.