2 Switch and Looping Satatements
2 Switch and Looping Satatements
Example
switch (no)
{
case 1:
printf(“Rose”);
break;
case 2:
printf(“Lotus”);
break;
case 3:
printf(“Sunflower”);
break;
case 4:
printf(“Lily”);
break;
case 5:
printf(“Jasmine”);
break;
default:
printf(“invalid flower name”);
}
2) Looping Statements.
Looping statements are used to perform set of statements for
specified number of times. They are classified into 3 types.
a) for loop
b) while loop
c) do…..while loop
a) for loop
for loop is an entry control or top based loop. It is used to repeat
statement by specified number of times or till the condition returns
true.
Syntax
for(initial value; condition; increment/decrement)
{
// body of for loop
}
Here,
The initial value part initializes the value of variable. The
condition part tested the condition, if the condition return true then
body of loop will be executed and if condition return false body of
loop will be terminated. Increment/decrement part increase/decrease
value of the variable by 1.
e.g for(i=1;i<=10;i++)
{
printf(“\n welcome”);
}
** Nested For loop
A for loop having another for loop is known as “Nested for loop”.
In nested for loop, loop execution start from outer (outside) for
loop and then control passes to inner for loop.
The inner for loop start the execution till the
condition of the inner loop is true. When inner for loop is completed
then control passes to outer loop and so on.
e.g for(i=1;i<=4;i++)
{
for(j=1;j<=4;j++)
{
printf(“%d”,j);
}
}