class 9 lecture notes

15
Class 9 – Flow Control: Loops

Upload: stephen-parsons

Post on 18-Nov-2014

2.222 views

Category:

Technology


2 download

DESCRIPTION

This is the class notes presentation for Class 9 of the Programming 1026 course: Logic & Problem Solving

TRANSCRIPT

Page 1: Class 9 Lecture Notes

Class 9 – Flow Control: Loops

Page 2: Class 9 Lecture Notes

Agenda

Warm-up, Issues Controlling Flow with loops Quest: Defining and calling a Ruby

method Assignment 6 Mid-term Exam

Page 3: Class 9 Lecture Notes

Warm-up and Issues

Lateral Thinking Exercise Project Issues? Other Issues? Remaining Activities/ Work Plan

Dates

Page 4: Class 9 Lecture Notes

Flow Control: Looping

Iteration refers to the ability to repeat similar actions conditionally rather than requiring a user-entered signal to stop.

Iterations may be loops using “while-end” or “until-end” or “for-end” or may be an array method using “.each”

We will examine only loops tonight and save the “each” method for next week when we cover arrays.

Page 5: Class 9 Lecture Notes

Looping

Scenarios Score Averager – what if you don’t know

how many students there will be? A persistent converter

Page 6: Class 9 Lecture Notes

Looping Notation

Pseudocode (sample)# get the number of rounds to complete

# set a counter to value 0

# while the counter is less than the number of rounds

# do some stuff here

# increase the counter

# end the loop

Page 7: Class 9 Lecture Notes

Looping Flowchart

The Loop Decision

implicit May

evaluate true or false condition

May use a counter to advance value

start

end

Assignment name?

Set counter = 0

Counter>10?

Calculate Average

Print Average

Increment Counter

Student Grade? Ye

sNo

Page 8: Class 9 Lecture Notes

Looping Ruby Code

While-end (a condition is true at the outset)

var_type=“any wine”

while var_type!=“exit”

puts “what type of wine do you like (type exit to end)”

var_type=gets.chomp

puts ”Mmmmm. I like ” + var_type + “,too!”

end

Page 9: Class 9 Lecture Notes

Looping Ruby Code

Until-end (condition is false at the outset)

var_type=“any wine”

until var_type==“exit”

puts “what type of wine do you like (type exit to end)”

var_type=gets.chomp

puts ”Mmmmm. I like ” + var_type + “,too!”

end

Page 10: Class 9 Lecture Notes

Looping Ruby Code

For-end (uses a range of values)for i in (1..12)

puts i.to_s + “ o\’clock and all is well”

end

for x in (“man”..”max”)

puts x

end

Page 11: Class 9 Lecture Notes

Exercise

Sample: Can I do it until I wear glasses?

Instructions: Develop a loop that will ask user to tell

something about himself/herself Respond to each prompt in some way,

e.g. “Interesting! Tell me more!” Continue to loop until the user says “I

wear glasses” Then say “Bye!” and end program

Page 12: Class 9 Lecture Notes

Quest: Defining and Calling a Method What is a method?

A function or operation that may be used several times in the application

May perform a calculation on variables May return a value Must be at top of file in Ruby

Why? Shortened code, consistent results Single code block to edit

Page 13: Class 9 Lecture Notes

Ruby: Declare/ Call Method

Exampledef doFtoC (fVal)

temp=fVal*9/5 + 32

return temp.round(2)

end

puts “enter fahrenheit temp”

f_temp=gets.chomp.to_f

puts “Celsius =” + doFtoC(f_temp).to_s

Page 14: Class 9 Lecture Notes

One last example

Ruby – the wine connoisseur

Your turn: Assignment 6 From Flowchart to Flow Control

Page 15: Class 9 Lecture Notes

Wrap-up

Summary Iteration – repeating

functions/operations Loops – a kind of iteration Methods: while, until, for

Next Week Arrays, Intro Project 2 Assignment 6 due at start of class