Ch 3 Control Structures - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

Ch 3 Control Structures

Description:

{ char achar; cout 'Enter a alphabetic character: '; cin achar; if (achar == 'a' || achar == 'A' || achar == 'e' || achar == 'E' ... – PowerPoint PPT presentation

Number of Views:26
Avg rating:3.0/5.0
Slides: 18
Provided by: socratist
Category:

less

Transcript and Presenter's Notes

Title: Ch 3 Control Structures


1
Ch 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

2
3.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

3
3.2 ConditionsIf statement
  • Decisions are made through the use of the if
    statement.
  • Syntax
  • if (expression) statements

if (age gt 21) cout ltlt "Cheers!"
4
3.2 ConditionsIf...else statement
  • Syntax
  • if (expression) statements
  • else statements
  • if (age gt 21)
  • cout ltlt "Cheers!"
  • else
  • cout ltlt "Soda anyone?"

5
3.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
6
3.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
7
3.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
8
3.2 Conditionsif example Continued
else cout ltlt "You entered a consonant.
Thanks!" ltlt endl ltlt endl
return (0)
9
3.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

10
3.3 Selection and Repetition
  • Single branch
  • if (expression) statements
  • Two-way branch
  • if (expression)
  • statements
  • else
  • statements

11
3.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

12
3.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
13
3.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!
14
3.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

15
3.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.

16
3.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

17
3.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
Write a Comment
User Comments (0)
About PowerShow.com