Title: Ch 3 Control Structures
1Ch 3 Control Structures
- 3.1 Three Essential Structures
- 3.2 Conditions
- 3.3 Selection and Repetition
- 3.4 Nested and Multiple-Alternative Selection
Structures - 3.5 The switch statement for Multiple Alternatives
23.1 Three Essential Structures
- Sequential
- code executes in sequence (line 1 to line n)
- do deviations
- Selection
- chooses one of many alternative paths
- only one path is chosen and executed
- Repetition
- same code is executed repeatedly
33.2 ConditionsIf statement
- Decisions are made through the use of the if
statement. - Syntax
- if (expression) statements
if (age gt 21) cout ltlt "Cheers!"
43.2 ConditionsIf...else statement
- Syntax
- if (expression) statements
-
- else statements
- if (age gt 21)
- cout ltlt "Cheers!"
-
- else
- cout ltlt "Soda anyone?"
53.2 ConditionsRelational Operators
- Use relational operators in your expressions
- gt greater than
- lt less than
- gt greater than or equal
- lt less than or equal
- equal to
- ! not equal
- A Boolean expression must evaluate to either true
or false.
3 4 gt 5 2 evaluates to true (2 3 1) lt
4 2 / 9 evaluates to false 2 / 3 3
0 evaluates to true
63.2 ConditionsLogical Operators
- Use logical operators in your expressions
- AND (true if both arguments are true)
- OR (true if either argument is true)
- ! NOT (true if single argument is false)
- Logical expressions must evaluate to true or
false.
1 3 evaluates to true 1 lt 3 4 gt
6 evaluates to true 5 1 gt 2 !0 evaluates
to true
73.2 Conditionsif example
include ltiostreamgt using namespace std int
main() char achar cout ltlt "Enter a
alphabetic character " cin gtgt achar if
(achar 'a' achar 'A' achar
'e' achar 'E' achar 'i'
achar 'I' achar 'o' achar
'O' achar 'u' achar 'U')
cout ltlt "You entered a vowel. Thanks!" ltlt endl
ltlt endl
83.2 Conditionsif example Continued
else cout ltlt "You entered a consonant.
Thanks!" ltlt endl ltlt endl
return (0)
93.2 ConditionsDemo Examples
- Show examples of if, if...else
- ex 1. Given a test score, what is the letter
grade equivalent - ex 2. Given a character, display it in the
opposite case - ex 3. Given a character, display the preceding
and following characters
103.3 Selection and Repetition
- Single branch
- if (expression) statements
-
- Two-way branch
- if (expression)
- statements
-
- else
- statements
-
113.4 Nested and Multi-branch Selectionsif...else
if...else
- if statements can and often are nested.
- Syntax
- if (expression1) statements
-
- else
- if (expression2)
- statements
-
- else
- statements
-
-
123.4 Nested and Multi-branch Selectionsif...else
if...else Continued
- Rewrite the following if using a nested if
if (age gt 21 wantDrink 'Y') //
serve drink else // serve soda or
nothing
if (age gt 21) if (wantDrink 'Y') //
serve drink else // do not serve drink
else // serve soda
133.4 Nested and Multi-branch Selectionsif...else
if...else Continued
- if (expression1)
- statements
- else if (expression2)
- statements
- else
- if (expression3)
- statements
- else
- if (expression4)
- statements
-
Notice the diagonal effect. Not cool!
143.4 Nested and Multi-branch Selectionsif...else
if...else Continued
- Regain your coolness with an if...else if...else
statement
- if (expression1) statements
- else if (expression2) statements else
if (expressionN) - statements
-
- else statements
153.4 Nested and Multi-branch Selectionsif...else
if...else Continued
- Show examples
- If I were a rich man.... (NOT!)
- ex 1. Display menu of choices.
- ex 2. Display final course grade based on current
class average. - ex 3. Determine if given number is between three
separate ranges. Take different action for each
range.
163.5 The switch Statement
- To switch or not to switch? That is the question.
- switch (expression) case constant_1
statements breakcase constant_n
statements breakdefault statements
break
173.5 The switch StatementExamples
- Show examples
- ex 1. Display a menu of choices (const, enum)
- ex 2. Determine if entered character is a vowel
or consonant