CH05: Loop Control Structures in C
Loops control structures are used to execute and repeat a block of statements depending on the value of a condition.
Types of Loops
For Loop
While Loop
Do - While Loop
For Loop
For Loop
A for
loop is used to execute and repeat a block of
statements depending on a condition.
syntax :
for (<initial value>; (<condition>); <increment>)
{
-----------------
<statement block>
-----------------
}
Where initial value
is the assignment expression which
initializes the value of a variable. condition
is a
relational or logical expression which will have the value
true
or false
. increment
is
the increment value of the variable which will be added every time.
Example :
int i;
for (i = 0; i<=5 ; i++){
printf("This will execute 5 times ! \n");
}
-
The statement inside the for loop will execute upto 5 times
because the initialize value
i
, is set to0
, so ifi < 5
then will run for 4 times but there is=
sign making iti<=5
meaning it will execute untili
becomes5
.
Nested For Loop
When a for loop is place inside another for loop it is called nested for loop.
Example :
for (int i = 0 ; i<10; i++){
for(int j = 0 ; j<20 ; j++){
printf("Nested For Loop\n");
}
}
While Loop
A while loop is used to execute and repeat a block of statements depending on a condtion.
syntax:
while(<condition>)
{
-----------------
<statement block>
-----------------
}
Where condition
is a relational or logical expression
which will have the value true
or false
.
Example :
int i = 0;
while (i<=10){
printf("%d\n",i);
i++;
}
In the following code the initial value of the i is 0 but when we use while statement the while condition will be true because the value of i, which is 0 is less than 10 so it will continue until the value of i reached 10 and giving the result of printing 0 to 10.
Do-While Loop
A do-while statement is also used to execute and repeat a block of statements depending on a condition.
syntax:
do
{
-----------------
<statement block>
----------------
}
while (<condition>)
Where conditon
is a relational or logical expression
which will have the value true
and false
.
When this statement is executed the computer will execute the
statement block irrespective of the value of the condition. At the
end of statement block, the condition is evaluated. If the value of
the condition is true
the statement block is executed
again and is repeated until the condition is false
.
Goto Statement
The goto
statement is an unconditional transfer of
control statement. It is used to transfer the control from one part
to another.
syntax:
goto label ;
------------
------------
label:
------------
Where label
is the statement label which is available
anywhere in the program . Its the identifier which is used to mark
the beginning of the another part of the program which will be
transfer by the goto
statement.
Break statement
The break statement is use to transfer the control to the end of a statement block in a loop.
syntax:
break;
Break is frequently used in the case
block of
switch
statement.
Example :
char ch = 'a';
switch(ch){
case 'a' : printf("a is for apple");
break;
case 'b' : printf("b is for ball");
break;
case 'c' : printf("c is for cat");
break;
case 'd' : printf("d is for dog");
break;
default : printf("Invalid input ! please enter a,b,c and d only.";
break;
}
In the given example break
statement is used after
every end of the case because if break statement is not present ,
then if any of the condition is match it will execute all the case
meaning it will activate all case and cause error in the program.