0% found this document useful (0 votes)
2 views

Switch & Loop Statements PPS

The document discusses control statements in C programming, focusing on switch-case and loop statements. It explains the syntax and functionality of switch-case for multiway decision making, as well as different types of loops (while, do-while, for) for repetitive tasks. Additionally, it covers loop control mechanisms like break, continue, and goto statements for managing flow within loops.

Uploaded by

keerthiteja156
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Switch & Loop Statements PPS

The document discusses control statements in C programming, focusing on switch-case and loop statements. It explains the syntax and functionality of switch-case for multiway decision making, as well as different types of loops (while, do-while, for) for repetitive tasks. Additionally, it covers loop control mechanisms like break, continue, and goto statements for managing flow within loops.

Uploaded by

keerthiteja156
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Control Statements

Switch Statements and


Loop Statements

Dr. V.Sireesha
Dept. of CSE
switch case control statement
• At times, the if condition may increase the
complexity of the program when one of many
alternatives is to be selected.
• C has built-in multiway decision statement known
as switch-case. The switch statement requires
only one argument variable or expression. It tests
the value of a given variable against a list of case
values and when a match is found, a block of
statements associated with that case is executed,
if not such match, then default statement is
executed.
switch case control statement
Syntax: switch(variable or expression)
{
case value-1: block-1
break;
case value-2: block-2
break;
..
..
..
..
default: default-block
}
next statement
switch case control statement
• The expression is an integer expression or character.
Value-1, value-2 are either integer constants or character
constants. These values should be unique with in a
switch statement. Case labels end with colon ( : ).
• The break statement signals the end of a particular case
and causes an exit from the switch statement,
transferring the control to the next statement following
the switch.
• The default is an optional case when present. It will be
executed if the value of the expression doesn’t match
with any of the case values. If not present, no action
takes place if all matches fail and control goes to the next
statement.
Flow chart of switch case
switch case statement
Example: switch(op)
{
case ‘+’: c=a+b; break;
case ‘-’: c=a-b; break;
case ‘*’: c=a*b; break;
case ‘/’: c=a/b;break;
default: printf(“wrong option”);
}
Loop Statements
Loop control statements
• Many tasks are needed to be done with the help of a computer and they are
repetitive in nature. Such type of actions can be easily done by using loop control
statements.
• A loop is defined as a block of statements which are repeatedly executed for
certain number of times to do a specific task.

Steps in loops:-
• Loop variable: It is a variable used in loop to evaluate.
• Initialization: It is the step in which starting value or final values are assigned to the
loop variable.
• Test-condition: It is to check the condition to terminate the loop. It is any relational
expression with the help of logical operators.
• Update statement: It is the numerical value added or subtracted to the loop
variable in each round of the loop.
• C language supports three types of loop control statements.
– while
– do-while
– for
while Statement
while: This is the simplest looping structure in C. the
while is an entry-controlled loop statement.
Syntax:
initial statement
while(test condition)
{
Statement(s)
Update statement
}
Flow chart for while Statement
while Statement
The test condition may be any expression, is evaluated and if it is true then the body of the loop is executed. The
test condition is once again executed for updated values, and if it is true the body of the loop is executed once
again. This process is repeated until the test condition is finally becomes false and control is transferred out of
the loop to the next statement. The body of the loop may contain one or more statements. The braces are
needed if the body of the loop contains more than one statement.

Example: main()
{
int i,sum;
i = 1; sum=0;
while (i<=10)
{
sum = sum + i ;
i + +;
}
printf(“sum=%d”,sum);
}
Here the value, sum of first ten numbers is stored into the variable sum; i is called as loop variable.
The loop is repeated for ten times to do that process, each time by incrementing the value of i by one. Once the
value of i becomes 11 then the test condition becomes false and the control is out of the loop.
do-while Statement
do-while: On some occasions it might be necessary to execute
the body of the loop before the test condition is performed.
Such situations can be handled by the do-while statement.
Do-while id exit controlled loop statement.
Syntax:
initial statement
do
{
Statement(s)
Update statement
} while(test – condition);

Next statement
Flow chart for do-while
do-while Statement
The body of the loop is executed first, and then at the end of the loop the test
condition is evaluated, if it is true then the statements are executed once again. The
process of execution continues until the test condition finally becomes false and the
control is transferred to the next statement

Example:
main()
{
int i, sum;
i =1;
sum=0;
do
{
sum = sum + i;
i + +;
} while( i<=10);
printf(“Sum=%d”, sum);
}
for loop statement
The for statement: The for loop entry controlled loop
that provides a more concise loop control structure.

Syntax:
for( initialization ; test-condition ; increment/decrement)
{
Statement(s)
}
Next statement
for loop statement
The for loop allows to specify three things about the loop
in a single line.
• Setting a loop counter variable to an initial value using
assignment statement.
Eg: i=1 count=0;
• the test condition is a relational expression that
determines the number of iterations desired or it
determines when to exit from the loop. If the test
condition is true, the body of the loop is executed,
otherwise the loop is terminated and execution
continues with the next statement after the loop.
Eg: i<=10
for loop statement
• After evaluating the last statement of the body the loop, the control is transferred
to the increment/decrement statement of the loop. And the new value is again
tested to see whether it satisfies the loop condition or not.
Eg: i++ ++i i+=2
 The body of the loop may contain one or more statements. In case there is more
than one statement then braces
 The three sections of for loop must be separated by semicolons (;). Initialization
and incr/decr parts may contain more than one statement must be separated by
commas.
Example: for(i=1, j=10 ; i<=10 ; i++, j--)
 The test-condition may have any compound relation and the testing need not be
limited only to the loop variable.
Example: for(i=1; i<20 && sum <100 ; i++)
 It is permissible to use expressions in the assignment statement of initialization
and incr/decr sections.
Example: for( k=(a+b)/2; k>0;k=k/2)
 The sections of for loop my absent depends on requirement in the program. But it
leads to take some extra care about those sections.
Example: for( ; ; )
 This statement leads to infinite loop or never–ending process.
Nesting of loops
Nesting of loops: The way if statement can be nested, similarly whiles
and for can also be nested.
Example:
for(i=1;i<=3;i++)//outer for loop
{
for(j=1;j<=i;j++) // inner for loop
{
Printf(“\t %d ”, i,j);
}
Printf(“\n”);
}
1
2 2
3 3 3
Jumps in loops
• Jumps in loops: We often come across some
situations where we want to make a jump
from one statement to other statement, jump
out of a loop or to jump to next iteration of
the loop instantly,. This can be accomplished
by the statements like :
– break
– continue
– goto.
The break statement
The break statement:
• When we want to jump out of a loop instantly
without waiting to get back to the condition
test, then the keyword break allows us to do
this.
• The break statement provides an early exit
from the loop.
• A break is usually associated with an if.
break - Example
Example:
for(i=1;i<=3;i++)
{
for(j=1;j<=5;j++)
{
if(j = = 3)
break;
else
printf(“ %d %d”, i, j);
}
Printf(“\n”);
}
1112
2122
3132

In this example when j value equals 5, break takes the control outside the inner for loop only, since it is
placed inside the inner loop.
The continue statement
The continue statement:
• When we want to take the control to the
beginning of the loop by passing the
statements inside the loop which are not yet
been executed, then the keyword continue
allows us to do this.
• It causes the next iteration of the loop to
begin and it applies only to loops.
• A continue is usually associated with an if.
continue - Example
Example:
for(i=1;i<=2;i++)
{
for(j=1;j<=2;j++)
{
if(i = = j)
continue;
else
printf(“ \n%d %d”, i, j);
}
}
Output : 1 2
2 1
In this example when i and j values are equal, continue takes the control to
the inner for loop by ignoring rest of the statements in the inner for loop.
goto statement
The goto statement:
• The goto statement alerts the normal sequence of program execution and control transfers to some other part
of the program.
• The goto statement leads the control to go to the particular part of the program by indicating in its label name.

Syntax: goto label;

Where label is a unique identifier used to label the target statement to which control will be transferred. The
general format of the label statement is:
label : statement;

Example:
void main( )
{
int x;
read: scanf(“%d”,&x);
if(x<0)
goto read;
else
printf(“x=%d”,x);
}
Here read is label, whenever x value entered as negative(x<0) then the control goes to read label, and once again
scanf( ) statement executed to read next value.
Thank You

You might also like