CONTROL STRUCTURES (MULTI-WAY SELECTION) - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

CONTROL STRUCTURES (MULTI-WAY SELECTION)

Description:

(MULTI-WAY SELECTION) MULTI-WAY SELECTION EXTENDED IF-ELSE Used to select exactly one task out of multiple tasks (or possibly none) Unlike a series of ifs ... – PowerPoint PPT presentation

Number of Views:58
Avg rating:3.0/5.0
Slides: 17
Provided by: MARKS291
Category:

less

Transcript and Presenter's Notes

Title: CONTROL STRUCTURES (MULTI-WAY SELECTION)


1
CONTROL STRUCTURES(MULTI-WAY SELECTION)
2
MULTI-WAY SELECTION
  • EXTENDED IF-ELSE
  • Used to select exactly one task out of multiple
    tasks (or possibly none)
  • Unlike a series of ifs evaluation stops once
    one relational expression evaluates as true
    (others are skipped)
  • Always does on less evaluation than a series of
    ifs
  • Well planned implementation provides for
    efficiency most common choice is listed first

3
MULTIPLE SELECTION
  • EXTENDED IF-ELSE - SIMPLE
  • if ( relational expression(s) )
  • statement
  • else
  • if ( relational expression(s) )
  • statement
  • else
  • if ( relational expression(s) )
  • statement
  • else
  • statement

4
MULTI-WAY SELECTION
  • EXTENED IF-ELSE - COMPOUND
  • if ( relational expression(s) )
  • statement
  • statement(s)
  • else
  • if ( relational expression(s) )
  • statement
  • statement(s)
  • else
  • if ( relational expression(s) )
  • statement
  • statement(s)

5
MULTI-WAY SELECTION
  • RULES
  • 1. First statement(s) will be executed if and
    only if the evaluation of the relational
    expression(s) is/are true.
  • 2. Second statement(s) will be executed if and
    only if the evaluation of the first relational
    expression(s) is/are false and if and only if
    the evaluation of the second relational
    expression(s) is/are true.
  • 3. Same progression as 1 and 2 for all levels
    that exist.
  • Note This process does not have to end with
    two, three or four selections, but can continue
    for as many selections as needed.

6
MULTIPLE SELECTION
  • EXAMPLES
  • if (numStudentslt 6)
  • salary hours CLUSTER_RATE
  • else
  • if (numStudents lt 12)
  • salary hours LIMITED_RATE
  • else
  • salary hours FULL_RATE

7
MULTIPLE SELECTION
  • if (temperature gt 85)
  • cout ltlt Go Swimming ltlt endl
  • else
  • if (temperature gt 70)
  • cout ltlt Play Tennis ltlt endl
  • cout ltlt Wear white! ltlt endl
  • else
  • if (temperature gt 32)
  • cout ltlt Play Football ltlt end
  • else
  • if (temperature gt 10)
  • cout ltlt Go Skiing ltlt endl
  • else
  • cout ltlt Go Bowling ltlt endl
  • EXAMPLES
  • if (temperature lt 10)
  • cout ltlt Go Bowling ltlt endl
  • else
  • if (temperature lt 32)
  • cout ltlt Go Skiing ltlt endl
  • else
  • if (temperature lt 70)
  • cout ltlt Play Football ltlt endl
  • else
  • if (temperature lt 85)
  • cout ltlt Play Tennis ltlt endl
  • cout ltlt Wear white! ltlt endl
  • else
  • cout ltlt Go Swimming ltlt endl

8
MULTI-WAY SELECTION
  • NESTED IF-ELSE
  • Situations where action is dependent upon two or
    more different conditions
  • Examples

if (gender M) if (age lt MAX_AGE)
cout ltlt Male and under 67. ltlt endl else
cout ltlt Male and 67 or over. ltlt endl else
if (age lt MAX_AGE) cout ltlt Female and
under 67. ltlt endl else cout ltlt
Female and 67 or over. ltlt endl
if (savings gt DOLLAR_LIMIT) if (vacation gt
VACATION_LIMIT) cout ltlt Trip to Hawaii ltlt
endl else cout ltlt Vacation at local
ski resort. ltlt endl else cout ltlt Go camping
in mountains. ltlt endl
9
MULTI-WAY SELECTION
  • SWITCH
  • Alternate form of multi-way selection
  • Decision can only be made on integral or
    enumerated data types (bool, char, int)
  • Does not work with strings or floating point
    numbers
  • Only works when testing for specific values
  • Does not work with ranges

10
MULTI-WAY SELECTION
  • SWITCH - SIMPLE
  • switch (selector)
  • case choice1 statement
  • break
  • case choice2 statement
  • break
  • case choice3 statement
  • break
  • case choice(n) statement

11
MULTI-WAY SELECTION
  • SWITCH WITH DEFAULT
  • switch (selector)
  • case choice1 statement
  • break
  • case choice2 statement
  • break
  • case choice(n) statement
  • break
  • default statement
  • statement(s)

12
MULTIPLE SELECTION
  • SWITCH COMPOUND
  • switch (selector)
  • case choice1 statement
  • statement(s)
  • break
  • case choice2 statement
  • statemant(s)
  • break
  • case choice(n) statement
  • statement(s)
  • break
  • default statement
  • statement(s)

13
MULTIPLE SELECTION
  • RULES
  • 1. Selector may only be an integral data type
    (int, char, bool, or enumerated).
  • 2. Corresponding statement(s) is/are executed if
    choice is equal to the selector.
  • 3. After corresponding statement(s) are
    executed if break is not present additional
    case statements are executed until a break is
    found or end of structure is found.
  • 4. If no choice is equal to the selector, the
    switch is exited unless a default is available.

14
MULTIPLE SELECTION
  • EXAMPLES
  • switch(category)
  • case 1 cout ltlt Category One ltlt endl
  • break
  • case 2 cout ltlt Category Two ltlt endl
  • break
  • case n cout ltlt Category n ltlt endl
  • break

15
MULTI-WAY SELECTION
  • int num1, num2, answer
  • char choice
  • cout ltlt "Enter two numbers, separated by a space
    "
  • cin gtgt num1 gtgt num2
  • cout ltlt endl ltlt endl
  • cout ltlt "Perform which operation on the Numbers?"
    ltlt endl
  • cout ltlt " a - Add" ltlt endl
  • cout ltlt " s - Subtract" ltlt endl
  • cout ltlt " m - Mulitply" ltlt endl
  • cout ltlt " d - Divide" ltlt endl
  • cout ltlt "Enter your choice from above " ltlt endl
  • cin gtgt choice
  • choice tolower (choice)
  • switch (choice)
  • case 'a answer num1 num2
  • EXAMPLE

16
MULTI-WAY SELECTION
  • EXAMPLE
  • char answer
  • cout ltlt "Would you like to continue Y or
    N?" ltlt endl
  • cin gtgt answer
  • switch (answer)
  • case 'Y'
  • case 'y cout ltlt "You chose yes" ltlt endl
  • cout ltlt "Way to go." ltlt endl
  • break
  • case 'N'
  • case 'n cout ltlt "You chose no" ltlt endl
  • cout ltlt "Not very positive." ltlt endl
  • break
  • default cout ltlt "You were suppose to choose
    Y or N." ltlt endl
Write a Comment
User Comments (0)
About PowerShow.com