Edited "For Loop" entry for grammar and clarity (#24875)

This commit is contained in:
GiraffeWarrior
2018-12-14 19:16:16 -08:00
committed by Manish Giri
parent 77d2e5e755
commit f188c3cd9d

View File

@ -2,7 +2,6 @@
title: For Loop
---
A for-loop is a repetitive statement that is used to check for some condition and then, based upon the condition a block of code, is executed repeatedly until the specified condition is satisfied.
The for-loop is distinguished from other looping statements through an explicit loop counter or loop variable which allows the body of the loop to know the exact sequencing of each iteration.
@ -11,6 +10,7 @@ Hence a for-loop is a repetition control structure that allows you to efficientl
For loop is an entry controlled loop unlike do-while loop.
## Syntax
```
@ -40,10 +40,10 @@ Though you can't ignore the condition, because empty condition is defaulted as f
### init
This step allows you to declare and initialize any loop control variables. This step is performed first and only once.
Note that the variables declared in init can only be used inside the brackets of the for loop.
Note that the variables declared in init can only be used inside the brackets of the For Loop.
### condition
Next the condition is evaluated. If it holds true, the body of the loop is executed. If it holds false, the body of the loop does not execute and flow of control jumps to the next iteration(repetition of a process).
Next, the condition is evaluated. If it holds true, the body of the loop is executed. If it holds false, the body of the loop does not execute and flow of control jumps to the next iteration(repetition of a process).
### increment
@ -83,9 +83,10 @@ value of a: 18
value of a: 19
```
##Single lined loop
The body of the for loop need not be enclosed in braces if the loop iterates over only one satatement.
##Example
## Single lined loop
The body of the for loop need not be enclosed in braces if the loop iterates over only one statement.
#### Example
```c++
#include<iostream.h>
using namespace std;
@ -116,10 +117,11 @@ value of a: 19
```
## Explanation
Here, the initialization condition is first set to a=10. The loop first checks for this condition. It then checks for the condition expression i.e. a<20 which holds true as 10<20(for the first case). Now the body of the loop is executed and we get the output "Value of a: 10". Then the update expression is executed which adds the number 1 to 'a' and the value of 'a' gets updated to 11 and the same steps are followed (as above) until the value of v reaches less than 20 i.e 19.
# Range-based for-loop
C++ also has what we call range-based for loops which iterates through all the elements of a container (e.g. array).
C++ also has what we call "range-based" for loops which iterates through all the elements of a container (e.g. array).
## Syntax