Similarly, in programming when we need to perform the same steps again and again, we use looping or iteration. We add the code within a loop and specify the condition upon which the execution of the loop stops and the program moves out of the loop, to the next set of instructions.
In programming there are three types of loops:
While Loop-Pre condition check
Checks the condition before executing the instructions within the loop
Syntax:
while(condition)
code
end while
while(black is not white)
do something
end while
For example: Check if the fruit chosen by the user is Orange, then add the amount of vitamin C to the diet.
BEGIN
INITIALISE choice=”Orange”
INITIALIZE vitaminc=0
WHILE (choice==”Orange”)
PRINT: “Vitamin C Added”;
vitaminc++;
GET: choice;
END WHILE
PRINT: “Total amount of vitamin c:”+vitaminc;
END
- Here, the choice by default is kept to be Orange, since the first fruit that is used is by default Orange.
- Next the dosage of vitamin C is initialized to zero.
- Then, the condition is checked. Only if it evaluates to be true, the block of code for the while loop is entered otherwise not. Here it evaluates to true, Hence the message is printed and vitamin C dosage is incremented.
- Next, within the loop we take the choice from the user for the type of fruit. If the user again enters Orange, the condition in the while loop becomes true and the statements within its block are executed again. When the choice entered by the user is not Orange, then the execution moves out of the loop and total amount of vitamin C is displayed.
do while Loop- Post condition check
Executes the code within the loop at least once. Initial iteration is done before checking the condition, then iterate only after checking.
Syntax:
do
code
while(condition)
Continuing with the same example as while loop, after giving the first dose of vitamin C, check the user input for type of fruit and keep adding vitamin c accordingly.
do
something
while(black is not white)
BEGIN
INITIALISE choice=” “
INITIALISE vitaminc=0
DO
PRINT: “Vitamin C Added”;
vitaminc++;
GET: choice;
WHILE(choice==”Orange”);
PRINT: “Total amount of vitaminc: “+vitaminc;
END
- Here, the choice by default is kept empty, since the first fruit that is used is by default Orange. The statements within the loop are executed atleast once, whether the condition is true or false.
- Next the dosage of vitamin C is initialized to zero.
- Then the statements within the do block is executed for the first time, without checking the condition. The message is displayed and vitamin c dosage is incremented.
- Next, the user input for the choice of fruit is taken.
- Then, the condition is checked. Only if it evaluates to be true, the block of code for the do while loop is entered again.
- When the choice entered by the user is not Orange, then the execution moves out of the loop and total amount of vitamin C is displayed.
for loop- fixed iteration
This is a self initializing, conditional checking, and incrementing/decrementing loop. This is used when the required number of iterations is known.
Syntax:
for(initialize; condition; increment/decrement)
code
end for
Continuing with the same example as while and do while loop, here we check the user input for 10 fruits chosen. Out of the 10 fruits that the user chooses, we increment vitamin c when orange is selected.
for(finite number)
check and repeat statements
BEGIN
INITIALISE choice=” “
INITIALISE vitaminc=0
FOR (int a=0;a<10;a++)
GET: choice;
IF (choice==”Orange”)
{
PRINT: “Vitamin C Added”;
vitaminc++;
}
END FOR
PRINT: “Total amount of vitaminc: “+vitaminc;
END
- Here, the choice by default is kept empty, since the choice for the first fruit will be taken from the user within the ‘for loop’.
- Next the dosage of vitamin C is initialized to zero.
- Then the ‘for’ loop begins for 10 iterations. Notice, ‘a’ is a counter variable which is initialized to zero and will be checked till it is less than 10 i.e. 9. With every iteration the value of ‘a’ will increment.
- First, the value of ‘a’ is zero, which is less than 10. Condition being true, the ‘for’ loop is entered.
- Next, within the ‘for’ loop, the user choice of fruit is taken. If the fruit is Orange, then the ‘if’ block of code is entered and the message is displayed and value of vitamin c is incremented.
- Next, after the first iteration, the value of ‘a’ is incremented to be One. Since one is less than ten, the ‘for’ loop is again entered and the statements are executed. This continues for a total of ten iterations.
- When the value of ‘a’ becomes ten which is not less than ten, the condition becomes false. Hence, the execution moves out of the loop and total amount of vitamin C is displayed.