0% found this document useful (0 votes)
13 views3 pages

2 Switch and Looping Satatements

The document explains the switch statement as a multi-way branching statement that simplifies multiple if-else conditions. It also covers looping statements, specifically for loops, while loops, and do-while loops, detailing their syntax and usage, including nested for loops. Examples are provided to illustrate the implementation of both switch and for loops.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views3 pages

2 Switch and Looping Satatements

The document explains the switch statement as a multi-way branching statement that simplifies multiple if-else conditions. It also covers looping statements, specifically for loops, while loops, and do-while loops, detailing their syntax and usage, including nested for loops. Examples are provided to illustrate the implementation of both switch and for loops.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

d) Switch statement.

Instead of writing many if……..else statements, we can use


the switch statement. switch is a multi way branching statement.
Syntax
switch (condition)
{
case 1:
statement1;
break;
case 2:
statement2;
break;
.
.
default:
statement n;
}

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.

for(initial value; condition; increment/decrement) // Outer loop


{

for(initial value; condition; increment/decrement) // inner ioop


{
// body of inner for loop
}
// body of Outer for loop
}

e.g for(i=1;i<=4;i++)
{
for(j=1;j<=4;j++)
{
printf(“%d”,j);
}
}

You might also like