In a certain process, specific blocks of codes are repeated until certain conditions are satisfied, to avoid repetition of codes in Loop control statements are used.
The different types of Loop control statements are
- while loop
- do-while loop
- for loop
while loop in C:
The while loop statements are used, when the number of iterations is not known in advance. The block of codes is repeated until the given condition is satisfied.
Syntax: while(condition)
{
statements;
}Points To Remember:
- The while must have the condition expression and multiple conditional expressions are allowed.
- The condition will be true if it returns a non-zero value and the condition becomes false when zero value is returned.
- The body of the loop is optional.
- The non-zero value inside while gives an infinite loop.
Example:1
<script src="https://gist.github.com/GeethaaSrinivasan/bc4570f11eb9f4dc67f12173e3de1679.js"></script>
Example 2:
<script src="https://gist.github.com/GeethaaSrinivasan/46bf69533f2aa603482b9fe933dfd375.js"></script>
Example 3:
<script src="https://gist.github.com/GeethaaSrinivasan/855563873b3b8b122f0a407081e83664.js"></script>
Explanation:
When we change the position of expression inside the while loop, we get different output, this is because when we assign multiple values to a variable, the recent value assigned to the variable is considered as the final value of the variable. Similarly, the final expression is considered the condition of the while loop, and the statements inside the loop are executed until it becomes zero.
In the first example, the value of a and b will be printed until a becomes zero. In the second example, the a and b will be printed until b becomes zero.
Note: Non-zero value inside while loop makes an infinite loop.
do-while Loop:
The do-while loop checks the condition at the end of the loop. The do-while loops are used when the code inside the loop must be executed at least once.
Syntax:
do{ code;
}while(condition);
Example 4:
<script src="https://gist.github.com/GeethaaSrinivasan/f0436b91a7f5a51977e115b9728e5830.js"></script>
Explanation:
The code inside the loop will be executed for the first time then it checks the condition, only if the condition is true, it execute code inside the loop.
Initially value of a=3,b=10 it enters the loop now b=b=5=>10-5=5, prints a=3,b=5 and a–=>2 then checks condition 5>=0, which is true again it enters loop 5-5=0 and a is 2 now checks the condition 0>=2 which returns false so it won’t enter the loop again.
For Loop:
The for loop is used when the number of times, the block of codes needs to be executed is known in advance.
Syntax:
for(initialization; condition; increment or decrement)
{
statements;
}
Work Flow Of For Loop:
- The first step inside for loop is variable initialization, then the condition is checked. If the condition is satisfied, then the block of codes inside the loop is executed.
- The value of the variable is incremented or decremented based on the given expression.
- Now the condition is again checked if true enters the loop or exits the loop if false and jumps to statement following the ‘for’ loop.
- Again the variable is incremented or decremented, the condition is checked and enters loop if satisfied, this process continues until the condition becomes false.
Example 5:
<script src="https://gist.github.com/GeethaaSrinivasan/2aa15ee35b5b58704e5520e72586f232.js"></script>
Explanation:
The square of all even numbers is printed first. Then ‘i’ value is initialized to 2 then the condition is checked, here 2 is less than 10, so it enters the loop and again checks if i =2 divided by 2 gives remainder 0. If it is true, then the square of 2 is assigned to j=>4. Then the square of 2 is 4 is printed.Now, ‘i’ value is incremented now i=3, then 3%2=>1 so ‘if ‘ condition fails, now flow jumps to for loop and i is incremented, now i=4, 4%2=0, the condition is satisfied, executes code inside ‘if ‘ so j=4*4=>16 is printed and the process continues until ‘i’ value is less than 10.
Thanks for your blog, nice to read. Do not stop.