You are on page 1of 23

CONTROL STATEMENTS

Kiran Jot Singh


CONTROL STATEMENTS
 Control statements are used to control the flow of
execution of a program.
 if/else statements are used to steer or branch the
operation in one of two directions.
 while, do/while, and for statements are used to
control the repetition of a block of instructions.
 switch/case statements are used to allow a
single decision to direct the flow of the program
to one of many possible blocks of instructions in a
clean and concise fashion.
WHILE LOOP

 When the execution of the program enters the top of the while
loop, the expression is evaluated.

 If the result of the expression is TRUE (non-zero), then the


statements within the while loop are executed.
 When execution reaches the bottom of the loop, the program
flow is returned to the top of the while loop, where the
expression is tested again.
 Whenever the expression is TRUE, the loop is executed.
 Whenever the expression is FALSE, the loop is completely
bypassed and execution continues at the first statement
following the while loop.
WHILE LOOP
 In this example, c is initialized to 0
and the text string “Start of program”
is printed.
 The while loop will then be executed,
printing the value of c each pass as c
is incremented from 0 to 100.
 When c reaches 100, it is no longer
less than 100 and the while loop is
bypassed.
 The “End of program” text string is
printed and the program then sits
forever in the while(1) statement.

 Also note the cast of c to an integer


inside the printf() function, inside the
while loop. Thisis necessary because
the printf() function in most
embedded C compilers will handle
onlyinteger-size variables correctly.
WHILE LOOP
DO/WHILE LOOP

 When execution reaches the bottom of the do/while construct, the


expression is evaluated. If the result of the expression is TRUE (non-
zero), then the program flow is returned to the top of the do/while
loop.
 But if the expression is FALSE, the program continues on with the
instructions that follow the construct.
DO/WHILE LOOP
 In this example, c is
initialized to 0 and the
text string “Start of
program” is printed.
 The do/while loop will
then be executed, printing
the value of c each pass as
c is incremented from 0 to
100.
 When c reaches 100, it is
no longer less than 100
and the do/while loop is
bypassed.
 The “End of program” text
string is printed, and the
program then sits forever
in the while(1) statement.
FOR LOOP
FOR LOOP
 When the execution of the program
enters the top of the for loop, expr1
is executed.
 Expr2 is evaluated and if the result
of expr2 is TRUE (non-zero), then
the statements within the for loop
are executed—the program stays in
the loop.
 When execution reaches the bottom
of the construct, expr3 is executed,
and the program flow is returned to
the top of the for loop, where the
expr2 expression is tested again.
 Whenever expr2 is TRUE, the loop is
executed.Whenever expr2 is FALSE,
the loop is completely bypassed.
 The for loop structure could be
represented with a while loop in
this fashion:
FOR LOOP
 In this example, the text
string “Start of program”
is printed. c is then
initialized to 0 within the
for loop construct.
 The for loop will then be
executed, printing the
value of c each pass as c is
incremented from 0 to
100, also within the for
loop construct.
 When c reaches 100, it is
no longer less than 100
and the for loop is
bypassed.
 The “End of program” text
string isprinted, and the
program then sits forever
in the while(1) statement.
IF/ELSE
IF/ELSE
IF/ELSE
 If the value of c is less
than 33, then the text
string “0<c<33” is printed.
 If the value of c is between
32 and 65, the text string
“33<c<66” is printed.
 If the value of c is not
within either of the
preceding cases, the text
string “66<c<100” is
printed.
 When c reaches 100, it is
no longer less than 100
and the for loop is
bypassed.
 The “End of program” text
string is printed, and the
program then sits forever
in the while(1) statement.
CREATE A PROGRAM THAT EFFICIENTLY TESTS
EACH BIT OF AN INPUT PORT AND PRINTS A
MESSAGE TO TELL THE STATE OF THE BIT.
CONDITIONAL EXPRESSION
SWITCH / CASE
SWITCH / CASE
 The expression is evaluated and its value is then
compared against the constants (const1, const2, . . .
constx).
 Execution begins at the statement following the
constant that matches the value of the expression.
The constants must be integer or character values.
 All of the statements following the matching constant
will be executed, to the end of the switch construct.
 Since this is not normally the desired operation,
break statements can be used at the end of each
block of statements to stop the “fall-through” of
execution and allow the program flow to resume after
the switch construct.
SWITCH / CASE
 If the character is a 0, 1, 2, or
3, the text string “c is a
number less than 4” will be
printed to the standard
output.
 If the character is a 5, the text
string “c is a 5” will be printed
to the standard output.
 If the character is none of
these (a 4 or a number greater
than 5), the default
statements will be executed
and the text string “c is 4 or is
> 5” will be printed.
 Once the appropriate case
statements have been
executed, the program will
return to the top of the while
loop and repeat.
BREAK
 The break statement is used to exit from a for, while, do/while, or
switch statement.
 If the statements are nested one inside the other, the break
statement will exit only from the immediate block of statements.

• In the inner while loop, c is


incremented until it reaches 100,
and then the break statement is
executed.
• The break statement causes
the program execution to exit the
inner while loop and continue
execution of the outer while
loop.
• In the outer while loop, c is set
to 0, and control is returned to
the inner whileloop. This process
continues to repeat itself forever.
CONTINUE
 The continue statement will allow the program to start the next iteration of
a while, do/while, or for loop.
 The continue statement is like the break statement in that both stop the
execution of the loop statements at that point.
 The difference is that the continue statement starts the loop again, from the
top, where break exits the loop entirely.

• In this example, the


value of c will be
displayed until it
reaches 100.
• The program will
appear at this point as
if it has stopped, when
in fact, it is still
running.
• It is simply skipping
the increment and
printf() statements.
GOTO
 The goto statement is used to literally “jump”
execution of the program to a label marking the
next statement to be executed.
 but in an embedded system it can be a very good
way to save some coding and the memory usage
that goes with it.
GOTO
GOTO

You might also like