Chapter 05 Part III - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

Chapter 05 Part III

Description:

A character constant is enclosed using single quotation marks. ... A string constant is enclosed using double quotation marks. Integer Data Types ... – PowerPoint PPT presentation

Number of Views:49
Avg rating:3.0/5.0
Slides: 20
Provided by: tcu3
Category:
Tags: iii | chapter | part | quotation

less

Transcript and Presenter's Notes

Title: Chapter 05 Part III


1
Chapter 05 (Part III)
  • Control Statements Part II

2
Objectives
  • In this part you will learn
  • To understand multiple selection using the switch
    selection statement.
  • To use the break and continue program control
    statements to alter the flow of control.

3
5.6 switch Multiple-Selection Statement
  • switch statement
  • Used for multiple selections
  • Tests a variable or expression

4
5.6 switch Multiple-Selection Statement
  • switch statement
  • Controlling expression
  • Expression in parentheses after keyword switch
  • case labels
  • Compared with the controlling expression
  • Statements following the matching case label are
    executed
  • Braces are not necessary around multiple
    statements in a case label

5
The Use of switch Statement
  • int option
  • cin gtgt option
  • switch (option)
  • case 0
  • break
  • case 1
  • break
  • default
  • break

6
5.6 switch Multiple-Selection Statement
  • default case
  • Executes if no matching case label is found
  • This case is optional
  • If no match and no default case
  • Control simply continues after the switch
  • A break statements causes execution to proceed
    with the first statement after the switch
  • Without a break statement, execution will fall
    through to the next case label

Note Forgetting a break statement when one is
needed in a switch statement is a logic error.
7
5.6 switch Multiple-Selection Statement
  • The default clause does not require a break
    statement. Some programmers include break for
    clarity and for symmetry with other cases.
  • Specifying an expression in the case label is a
    syntax error.
  • Incorrect example
  • switch (option)
  • case ab
  • break

8
Fig. 5.12 switch multiple-selection statement
with break statements.
9
Comparison
int option cin gtgt option if (option 1)
result a b else if (option 2)
result a b else cout ltlt Incorrect
option! ltlt endl
int option cin gtgt option switch (option)
case 1 result a b break case
2 result a b break default
cout ltlt Incorrect option! ltlt endl break
10
Calculating number of letters
Examples of character constant A, b, C,
\n
Note Do not confuse string constant with
character constant. A string is a sequence of
characters. String A Character A
11
Reading character input
  • Function cin.get()
  • Reads one character from the keyboard
  • EOF
  • End-of-file.
  • Defined in ltiostreamgt
  • ltctrlgt d in UNIX/Linux
  • ltctrlgt z in Windows and then press Enter.
  • while ((letter cin.get()) ! EOF)
  • Read one character from keyboard and assign it to
    letter.
  • Compare letter with EOF
  • If letter ! EOF (End-of-file), enter the loop.
  • Otherwise (letter EOF), leave the loop.

12
switch Multiple-Selection Statement
13
Output Results
Character constant
String constant
  • Note
  • A character constant is enclosed using single
    quotation marks.
  • We can also represent a character constant using
    integer type.
  • A string constant is enclosed using double
    quotation marks.

14
Integer Data Types
  • Integer data types
  • short
  • Abbreviation of short int
  • Minimum range is -32,768 to 32,767
  • long
  • Abbreviation of long int
  • Minimum range is -2,147,483,648 to 2,147,483,647
  • int
  • Equivalent to either short or long on most
    computers
  • char
  • Can be used to represent small integers

Note Integer data types can vary in size between
systems
15
5.7 break and continue Statements
  • break/continue statements
  • Alter flow of control
  • break statement
  • Causes immediate exit from control structure
  • Used in while, for, dowhile or switch statements
  • Example
  • int option 0
  • while (option ! -1)
  • cin gtgt option
  • if (option -1)
  • break //force immediate exit

16
5.7 break and continue Statements
  • continue statement
  • Skips remaining statements in loop body
  • Proceeds to increment and condition test in for
    loops
  • Proceeds to condition test in while/dowhile
    loops
  • Then performs next iteration (if not terminating)
  • Used in while, for or dowhile statements

17
Loop 10 times
Exit for statement (with a break) when count
equals 5
18
Loop 10 times
Skip line 14 and proceed to line 9 when count
equals 5
19
Performance Tip 5.5
  • The break and continue statements, when used
    properly, perform faster than do the
    corresponding structured techniques.
Write a Comment
User Comments (0)
About PowerShow.com