04 Lec Control Structures
04 Lec Control Structures
Gabud.CS11.
Outline: (3 Kinds of Control
Structures in C)
• Sequence Control Structure
• Selection Control Structure
– if
– if-else
– switch
• Repetition Control Structure
– while
– do-while
– for
Gabud.CS11.
Sequence Control Structure
• sequential execution of statements -
one after the other in the order in which
they are written
• Example:
Total = Total + grade;
Counter = Counter + 1;
Gabud.CS11.
Selection Control Structure
• chooses among alternative program
statements
• uses condition an expression that is
either false(0) or true (usually
represented by 1)
• also called “conditional branching
construct”
Gabud.CS11.
Selection Control Structure
• 3 Types:
a) if – single selection, selects or ignores a
single action
b) if-else – double (or multiple) selection,
selects between two (or more) different
actions
c) switch – multiple selection, selects among
many different actions
Gabud.CS11.
Outline: (3 Kinds of Control
Structures in C)
• Sequence Control Structure
• Selection Control Structure
– if
– if-else
– switch
• Repetition Control Structure
– while
– do-while
– for
Gabud.CS11.
What does this do?
Gabud.CS11.
• What if the specification is added that the
program should be able to determine whether
a student passes the exam or not?
Gabud.CS11.
if
• The if construct is of this form:
if (<boolean_expression>)
<statement_1>
Gabud.CS11.
if-else
• The if-else construct is of this form:
if (<boolean_expression>)
<statement_1>
else
<statement_2>
Gabud.CS11.
if-else
• boolean_expression
– any expression that evaluates to either
TRUE or FALSE
– usually involves logical operators and
relational operators.
• statement_1 and statement_2
– a simple statement or a compound
statement
• The else portion is optional.
Gabud.CS11.
Gabud.CS11.
else-if (multiple)
if (<boolean_expression>)
<statement_1>
else if (<boolean_expression>)
<statement_2>
...
else
<statement_n>
Gabud.CS11.
if (average>=64)
printf(“You passed CS 11. Congratulations!\n”);
else if ((average>=60) && (average < 64))
printf(“You need to take the removal exam.\n”);
else
printf(“You failed CS 11. See you next sem!\n”);
Gabud.CS11.
• If the average is passing, do the
following:
– Say “You passed CS 11.”
– If the average is greater than or equal to
90, say “Very good!”.
– Otherwise (if the average is less than 90),
say “Good.”
• Otherwise, say “You failed CS 11.”
Gabud.CS11.
Consider this implementation:
Gabud.CS11.
IMPORTANT
• In using the if-else construct, we should
be careful with the pairing of ifs and
else’s.
Gabud.CS11.
? Operator
• The ? operator (conditional operator) is a more efficient
form for expressing simple if statements.
• It has the following form:
<expression1>?<expression2>:<expression3>
• It simply states:
if (<expression1>)
<expression2>
else
<expression3>
Gabud.CS11.
? Operator
• Example: Suppose we want to assign the
maximum of a and b to z. Using the ?
operator, we have the following statement:
z = (a > b) ? a : b;
• which is the same as (in if-else):
if (a > b)
z = a;
else
z = b;
Gabud.CS11.
switch
• switch construct is a multi-way decision that
tests whether an expression matches one of
a number of constant integer values, and
branches accordingly.
Gabud.CS11.
switch
The switch construct has the following form:
switch (<expression>)
{
case <const_expr_1> : <statements>
case <const_expr_2> : <statements>
:
:
case <const_expr_n> : <statements>
default : <statements>
}
Gabud.CS11.
switch
• expression should evaluate to a
constant character or integer.
• const_expr_1, const_expr_2, and so
on, are integer-valued (or character-
valued) constants or constant
expressions.
• statements is a sequence of simple
and/or compound statements.
• default is optional.
Gabud.CS11.
switch (position)
{
case 1:
printf(“You’re in first place.\n”);
printf(“Congratulations.\n”);
break;
case 2:
printf(“You’re the first runner up.\n”);
printf(“Nice try.\n”);
break;
case 3:
printf(“You’re the second runner up. \n”);
printf(“Not too bad.\n”);
break;
default:
printf(“You lost.\n”);
printf(“Better luck next time.\n”);
}
Gabud.CS11.
Outline: (3 Kinds of Control
Structures in C)
• Sequence Control Structure
• Selection Control Structure
– if
– if-else
– switch
• Repetition Control Structure
– while
– do-while
– for
Gabud.CS11.
Repetition Control Structure
• uses loops
• loop body contains the statements to be
repeated in the loop
• 3 types:
– while
– do-while
– for
• These are also called looping constructs.
Gabud.CS11.
while loop
• SYNTAX:
while (<boolean expression>)
<statement>
Gabud.CS11.
while loop
• Examples:
i=1
while (i<=5) {
printf(“%d”,i);
i++;
}
Gabud.CS11.
int Done = 0;
while (!Done)
{
printf(“Are
you done yet?”);
scanf(“%s”,
answer);
if
Gabud.CS11.
while loop
• IMPORTANT:
• The while loop must terminate
eventually. The boolean expression
must evaluate to FALSE at some
foreseeable point.
• Loops that do not terminate are called
infinite loops.
Gabud.CS11.
int Done = 0;
while (!Done) {
printf(“Are you done yet!? “);
scanf(“%s”, answer);
if (strcmp(answer, “yes”)==0)
printf(“Thank goodness!\n”);
else
printf(“Be quick about it!\n”);
}
An infinite loop!
Gabud.CS11.
do-while loop
• A variant of the while loop.
• SYNTAX:
do
<statement>
while (<boolean_expression>);
• boolean_expression can be any expression that
evaluates to either TRUE or FALSE.
• statement can be either a simple or a compound
statement.
• do-while loop executes the statements first, then
evaluates boolean_expression. Gabud.CS11.
do-while loop
• Example:
int i=1;
do
{
printf(“%d”, i);
i++;
}
while (i<=5);
Gabud.CS11.
int Done = 0;
do
{
printf(“Are you done yet!? ”);
scanf(“%s”, answer);
if (strcmp(answer,”yes”)==0)
{
printf(“Thank goodness!\n”);
Done = 1;
}
else
printf(“Be quick about it!\n”);
}
while (!Done);
Gabud.CS11.
for loop
• SYNTAX:
for(<expr1>; <expr2>;<expr3>)
<statement>
• equivalent to:
<expr1>;
while (<expr2>)
{
<statement>
<expr3>; Gabud.CS11.
for loop
• usually, expr1 and expr3 are assignment
statements,
• and expr2 is a boolean expression.
• Any of the three parts can be omitted, but the
semicolons must remain.
• If the expr2, is not present, it is taken as
permanently true.
Gabud.CS11.
for loop
• Example:
Gabud.CS11.
numStudents = 10;
for (i=1;i<=numStudents; i++) {
printf(“Enter your exam average:”);
scanf(“%f”, &average);
if (average>=64)
printf("You passed CS 11.\n");
else
printf("You failed CS 11.\n");
}
Gabud.CS11.
for loop
• The for loop is also prone to infinite
loops. This is especially true if expr2 is
omitted. Thus, the following may result
to an infinite loop:
for (;;)
{
/* some statements */
}
Gabud.CS11.
break and continue
• C provides two commands to control
how we loop:
– break – to exit from a loop or a switch
– continue – skip an iteration of the loop
Gabud.CS11.
break and continue
• Consider the following example where we
read in integer values and process them
according to the following conditions. If the
value we have read is negative, we wish to
print an error message and abandon the loop.
If the value read is greater than 100, we wish
to ignore it and continue to the next value in
the data. If the value is zero, we wish to
terminate the loop.
Gabud.CS11.
while ((scanf(“%d”, &value) == 1) &&
(value != 0))
{
if (value < 0)
{
printf(“Illegal value”);
break; /* Abandon the loop */
}
if (value > 100)
{
printf(“Invalid value”);
continue;
/* Skip to start loop again */
}
/* Process the value read */
/* Guaranteed between 1 and 100 */
}
Gabud.CS11.
Summary:
• Sequence Control Structure
• Selection Control Structure
– if
– if-else
– switch
• Repetition Control Structure
– while
– do-while
– for