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循环之后的下一个语句。
这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.
根据上述程序,总和初始化为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.
发表评论