Overview - PowerPoint PPT Presentation

About This Presentation
Title:

Overview

Description:

Step3: If condition is true repeat Step1 through 3, otherwise ... Will not be flagged by compiler but won't execute the way you intend. Grandma's Attic ... – PowerPoint PPT presentation

Number of Views:24
Avg rating:3.0/5.0
Slides: 16
Provided by: engi47
Learn more at: http://web.cs.wpi.edu
Category:
Tags: flagged | overview

less

Transcript and Presenter's Notes

Title: Overview


1
Overview
  • do/while loop
  • break statement
  • continue statement
  • switch statement

2
do/while loop
do statement while (condition) Step1
Execute statements inside braces Step2 Evaluate
condition. Step3 If condition is true repeat
Step1 through 3, otherwise continue execution at
next statement following the do/while
statement Usually use even when not
necessary Useful when you want the statements to
execute at least once. i.e reading input.
3
do while example
do cout ltlt Enter a number between 1 and
10 cin gtgt number while (number lt 0
number gt 10)
4
do/while example
What will the output of the following program
sequence be? int j 4 do j j - 2
cout ltlt Hi while (j gt 0) Walk
through this example showing how j changes.
5
Loop Exercises
1) How many times does the statement inside the
do/while body execute? What gets displayed?
int i 0, x 2 do x x i while
( i lt 3) cout ltlt x ltlt x ltlt endl 2) How
many times does the cout statement execute? for
(int i 1 i lt 4 i) for (int j 1 j lt
3 j) cout ltlt Hello ltlt endl
6
continue statement
  • skip the rest of this iteration and continue with
    the next iteration of the loop
  • int x 1
  • while ( x lt 10)
  • if (5 x)
  • x
  • continue
  • cout ltlt x ltlt endl
  • x
  • condition (x lt 10) is next statement executed
    after the continue statement.

7
continue statement
for (int x 1 x lt 10 x) if (5
x) continue cout ltlt x ltlt endl
next statements executed after continue are x
x lt 10
8
break statement
Breaks out of the loop. Next statement executed
after a break statement is the statement
immediately following the loop. for (int x 1
x lt 10 x) if (5 x)
break cout ltlt x ltlt endl Next
statement executed after the break statement is
the statement immediately following the for
loop.
9
else if example
if (A lettergrade) gpa 4.0 else if
(B lettergrade) gpa 3.0 else if
(C lettergrade) gpa 2.0 else if
(D lettergrade) gpa 1.0 else if
(F lettergrade) gpa 0.0 else
cout ltlt Illegal letter grade ltlt endl
10
switch statement
switch (lettergrade) case A
gpa 4.0 break case B
gpa 3.0 break case C
gpa 2.0 break case D
gpa 1.0 break case
F gpa 1.0 break
default cout ltlt Illegal letter
grade ltlt endl
11
switch statement syntax
switch (controlling expression) case
label1 statement(s)
break case label2 statement(s)
break .
. . case labeln
statement(s)
break default statement(s)
12
switch statement rules
  • controlling expression must be of type int or
    char
  • labels must be constants that match the type of
    the controlling expression
  • default statement can be anywhere. More intuitive
    at bottom.
  • can have multiple case labels sharing the same
    code
  • case A case a
  • statements
  • break

13
else if that cannot be implemented as a switch
int score cout ltlt Enter score cin gtgt
score cout ltlt Your grade is   if (score lt
68) cout ltlt NR ltlt endl else if (score lt
78) cout ltlt C ltlt endl else if (score lt
88) cout ltlt B ltlt endl else cout
ltlt A ltlt endl
14
common programming errors
  • Forgetting the break statement. Statements for
    next case get executed by mistake.
  • Forgetting the space between the word case and
    the label. Will not be flagged by compiler but
    wont execute the way you intend.

15
Grandmas Attic
for (char letter 'a' letter lt i' letter)
cout ltlt "I'm going to Grandma's attic and
I'm going to get " switch (letter)
case 'i' cout ltlt "an igloo, "
case 'h' cout ltlt "a hat, " case
'g' cout ltlt "a goat, " case 'f'
cout ltlt "a fox, " case 'e' cout ltlt
"an elephant, " case 'd' cout ltlt
"a dinosaur, " case 'c' cout ltlt "a
cookie, " case 'b' cout ltlt "a ball
and " case 'a' cout ltlt "an apple"
ltlt endl default break
Write a Comment
User Comments (0)
About PowerShow.com