Title: Flow of Control Control Structures in CC
1Flow of Control Control Structures in C/C
- Flow of control
- Order in which statements are executed
- Control structures in C/C
- Sequence structure
- statements executed in order in which they are
placed in program - essentially built into C/C (comes for free)
- Selection structures
- provide ability to select which statement will be
executed next - provide decision-making capability, essentially
- Repetition (or iteration) structures
- provide ability to go back repeat execution of
set of statements - provide looping capability, essentially
2Flow of Control Control Structures in C/C
(contd)
- Overview - 7 Control Structures in C/C
- Sequence
- Order in which you place you statements in
program - Three types of selection
- if structure - single selection
- if-else structure - double (multiple when
nested) selection - switch structure - multiple selection
(restricted) - Three types of repetition
- for structure
- while structure
- do-while structure
3Flow of Control Control Structures in C/C
(contd)
- An Aside - The goto Statement
- The notorious goto statement (C/C actually
supports it!) - Allows unconditional transfer of control to some
other statement - Crimes of goto statements
- Bad program structure - spaghetti code
- Tend to complicate programs - make them more
difficult to read understand at best - Verdict for goto statements (at least for this
class) - Theoretically - goto statement is never
required - other control structures in C/C
provide sufficient flexibility to handle all
possible flow control requirements - Dont use at all cost, lest youd have lots of
explanation to do!
4Flow of Control Control Structures in C/C
(contd)
- The if Statement
- Single selection structure
- Performs an action if condition is true
- Skips the action if condition is false
- Note conditions are relational expressions
- Example in pseudocode
- If students grade is gt 60
- Print Passed
- Example in C
- if (grade gt 60)
- cout ltlt "Passed" ltlt endl
5Flow of Control Control Structures in C/C
(contd)
- The if Statement (contd)
- Flowchart Representation
Action
Condition
true
false
6Flow of Control Control Structures in C/C
(contd)
The if-else Statement
- Double selection structure
- Performs an action if condition is true
- Performs a different action if condition is false
- Can be nested (chained) to do multiple selection
- Example in pseudocode
- If students grade is gt 60
- Print Passed
- else
- Print Failed
Example in C if (grade gt 60) cout ltlt
"Passed" ltlt endl else cout ltlt "Failed" ltlt
endl
7Flow of Control Control Structures in C/C
(contd)
- The if-else Statement (contd)
- Flowchart Representation
Action 1
Action 2
Condition
true
false
8Flow of Control Control Structures in C/C
(contd)
- Seen so far - only single-statement action for
if or if-else - Multiple-statement actions - enclose statements
with - Compound statement - set of statements contained
in - Can be placed anywhere a single statement can be
placed - A compound statement containing declarations is
called a block - Example
if (grade gt 60) cout ltlt "Passed" ltlt
endl else cout ltlt "Failed" ltlt endl cout
ltlt "Must take this course again." ltlt endl
9Flow of Control Control Structures in C/C
(contd)
- Nested (Chained) if-else Statements
if (grade gt 90) cout ltlt "A" else if
(grade gt 80) cout ltlt "B" else if
(grade gt 70) cout ltlt "C" else
if (grade gt 60) cout ltlt "D"
else cout ltlt "F" cout ltlt endl
(popular alternative form) if (grade gt 90)
cout ltlt "A" else if (grade gt 80) cout ltlt
"B" else if (grade gt 70) cout ltlt "C" else
if (grade gt 60) cout ltlt "D" else cout ltlt
"F" cout ltlt endl
10Flow of Control Control Structures in C/C
(contd)
- The switch Statement
- Multiple selection structure
- Test expression can assume series of integral
values - Performs different actions for different test
expression values - Less general than nested if-else statement
- integer test expression only
- test for equality only
- General form shown on right
switch (expression ) case value1
statement11 ... break case
value2 statement21 ...
break ... case valueN
statementN1 ... break default
statementD ...
11Flow of Control Control Structures in C/C
(contd)
- The switch Statement (contd)
- Uses 4 keywords
- switch
- identifies start of switch statement
- expression in parentheses (switch expression)
evaluated its value compared to various
alternative (case) values within compound
statement that follows - switch expression must evaluate to an integer
result (compilation error otherwise)
switch (expression) case value1
statement11 ... break case
value2 statement21 ...
break ... case valueN
statementN1 ... break default
statementD ...
12Flow of Control Control Structures in C/C
(contd)
- The switch Statement (contd)
- Uses 4 keywords (contd)
- case
- labels individual values to be compared to value
of switch expression (identifies possible entry
points of switch statement) - comparison done in order in which case values are
listed - execution begins with statement immediately
following case value that matched value of switch
expression
switch (expression) case value1
statement11 ... break case
value2 statement21 ...
break ... case valueN
statementN1 ... break default
statementD ...
13Flow of Control Control Structures in C/C
(contd)
- The switch Statement (contd)
- Uses 4 keywords (contd)
- break
- identifies end of each case forces immediate
exit from switch statement - once entry point established, no further case
evaluations done all statements within compound
statement executed unless break statement is
encountered - if omitted, all statements in compound statement
following entry point (including default case)
are executed - (incidentally) also used to force similar
immediate exit in while, for do-while statements
switch (expression) case value1
statement11 ... break case
value2 statement21 ...
break ... case valueN
statementN1 ... break default
statementD ...
14Flow of Control Control Structures in C/C
(contd)
- The switch Statement (contd)
- Uses 4 keywords (contd)
- default
- identifies default action when no match between
case value switch expression value exists - optional (if switch expression value does not
match any case value, no statement is executed
unless default is encountered) - invariably placed at bottom of switch statement
(by virtue of its purpose) break statement not
needed for last switch case
switch (expression) case value1
statement11 ... break case
value2 statement21 ...
break ... case valueN
statementN1 ... break default
statementD ...
15Flow of Control Control Structures in C/C
(contd)
- // Switch statement example
- include ltiostream.hgt
- int main(void)
-
- char choice
- cout ltlt "Type a character then press enter "
ltlt endl - switch ( cin gtgt choice )
-
- case 'r' case 'R'
- cout ltlt "RED" ltlt endl break
- case 'g' case 'G'
- cout ltlt "GREEN" ltlt endl break
- case 'b' case 'B'
- cout ltlt "BLUE" ltlt endl break
- default
- cout ltlt "NON-PRIMARY" ltlt endl
-
- return(0)
16Flow of Control Control Structures in C/C
(contd)
- / How does this program (using nested if-else)
- compare with the preceding one? /
- include ltiostream.hgt
- int main(void)
-
- int choice
- cout ltlt "Type a character then press enter "
ltlt endl - cin gtgt choice
- if (choice 'r' choice 'R')
- cout ltlt "RED" ltlt endl
- else if (choice 'g' choice 'G')
- cout ltlt "GREEN" ltlt endl
- else if (choice 'b' choice 'B')
- cout ltlt "BLUE" ltlt endl
- else
- cout ltlt "NON-PRIMARY" ltlt endl
- return(0)