In programming, a loop is used to repeat a block of code until the specified condition is met.
For Loop
When you know exactly how many times you want to loop through a block of code, use the for
loop instead of a C While Loops
Syntax
How for loop works?
- The initialization statement is executed only once.
- Then, the test expression is evaluated. If the test expression is evaluated to false, the
for
loop is terminated. - However, if the test expression is evaluated to true, statements inside the body of the
for
loop are executed, and the update expression is updated. - Again the test expression is evaluated.
This process goes on until the test expression is false
Note: testExpression
it’s made by C Comparison Operators and C Logical Operators.
For loop Flowchart
Example 1:
Output: 1 2 3 4 5 6 7 8 9 10
Example 2:
Oputput: Sum = 55