Entry Control Loops
Entry Control Loops
There are two types of entry control loops, they are:
1. For loop
2. While loop
For loop:
If the programmer knows exact number of times that the block of code should iterate then for loop is used.
For example, you are given a job to fill a bucket with a mug. bucket holds 15 liters of water and mug can hold a total of 5 liters of water. In such case programmer knows the number of time that he has to iterate the process(filling). In this case 5*3(times)=15 liters.
While loop:
If the programmer doesn't know the exact number of times that the block of code should iterate (just opposite of for loop) then While loop is used.
For example, you are given a job to fill a bucket with a mug. you don't know the exact capacity of the bucket or the mug. All you have to do is fill the bucket with the help of mug until its full.
Above explanation was logical point of view now lets view the syntax.
Syntax for For loop:
FOR(INITIALIZATION; CONDITION; UPDATING)
{
statement
}
eg: for(i=0; i<5; i++)
{statement}
Syntax for while loop:
while(condition)
{statement}
Eg:
i=0
while(i<5)
{
I++}
Comments
Post a Comment