Flow Control II - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

Flow Control II

Description:

while (speed is less than 100mph) push the ... Speed is less than 100mph ... statements in the body must be able to change the boolean expression value, from ... – PowerPoint PPT presentation

Number of Views:32
Avg rating:3.0/5.0
Slides: 21
Provided by: qizh
Category:
Tags: control | flow | lessable

less

Transcript and Presenter's Notes

Title: Flow Control II


1
Flow Control II
Qi ZhangSept 8
COMP 110 Introduction to Programming
2
Last Class
  • Review of Ch.2
  • Branching Statement Part I
  • The if-else statement
  • Boolean expressions
  • Nested statements and compound statements

3
Todays Lecture
  • Branching Statement Part II
  • The switch statements
  • Loop Statements
  • while statement

4
Announcements
  • Lectures on next Mon Wed
  • Lecturer Feng Pan
  • Assignment Submission
  • Time change
  • If assigned on Wed, due on the Wed after two
    weeks, right before class.
  • Also bring a printed copy!
  • Remember to submit all your files, otherwise, ask
    me to clear your attempt and resubmit
  • Talk about Lab 1 on Fri

5
Branching Statement Switch
  • Multiple Branches

Start
Prompt User for int
What is the integer?
inputInt 0
inputInt is other value
inputInt 1
Print how may I help you
Print hello
Print how are you
6
Switch Statements
  • switch (inputInt)
  • case 1
  • System.out.print(how are you)
  • break
  • case 0
  • System.out.print(hello)
  • break
  • default
  • System.out.print(how may I help you)
  • break

Controlling expression integer or char ONLY!
Case label
Break statement
Default all other values
6
7
Switch Statements
  • Syntax
  • switch (controlling expression)
  • case case label
  • statements
  • break
  • case case label
  • statements
  • break
  • default
  • statements
  • break

7
8
In-Class Exercise Practice with Switch statements
  • Write a switch statement that takes as
    controlling expression your year in college (as
    int) and outputs your year in college as
    freshman, sophomore, junior, senior, or super
    senior

9
Solution
  • switch (year)
  • case 1
  • System.out.print(freshman)
  • break
  • case 2
  • System.out.print(sophomore)
  • break
  • case 3
  • System.out.print(junior)
  • break
  • case 4
  • System.out.print(senior)
  • break
  • default
  • System.out.print(super senior)
  • break

9
10
Loop
  • How to speedup a car to 100mph?

11
Loops
  • Loop - part of program that repeats
  • Body - statements being repeated
  • Iteration - each repetition of body
  • Stopping condition

12
Types of Loops
  • while
  • Safest choice
  • Not always most elegant
  • do-while
  • Loop iterates at least ONCE
  • for
  • Numeric computation changes by equal amount

12
13
while loop
Push the acceleration paddle while (speed is
less than 100mph) push the acceleration
paddle Release the paddle
14
while loop
  • Syntax
  • while (boolean expression)
  • statements
  • .

Evaluate Boolean Expression
true
false
Execute Body
End loop
15
while loop
  • The stop condition is a boolean expression. The
    body is repeated if boolean expression is true.
  • Speed is less than 100mph
  • The statements in the body must be able to change
    the boolean expression value, from true to false.

16
while loop
If the Body can not affect the boolean expression
Endless loop !!
17
In-Class exercise
  • Use while loop to describe how to output integer
    number from 1 to 10.
  • Draw flow chart
  • Write pseudocode
  • n1
  • output n
  • nn1
  • output n
  • nn1
  • output n
  • ..
  • until n10

18
In-Class exercise
19
In-Class exercise
  • int n 1
  • while (nlt 10)
  • Output n
  • n n1

int n 1 while (nlt10) System.out.println(n)
nn1
19
20
Assignment 2
  • Due time 9AM Sept 24
  • Submission
  • through Blackboard
  • Print out and turn in during class
Write a Comment
User Comments (0)
About PowerShow.com