class 9 lecture notes

Post on 18-Nov-2014

2.222 Views

Category:

Technology

2 Downloads

Preview:

Click to see full reader

DESCRIPTION

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

TRANSCRIPT

Class 9 – Flow Control: Loops

Agenda

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

method Assignment 6 Mid-term Exam

Warm-up and Issues

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

Dates

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.

Looping

Scenarios Score Averager – what if you don’t know

how many students there will be? A persistent converter

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

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

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

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

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

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

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

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

One last example

Ruby – the wine connoisseur

Your turn: Assignment 6 From Flowchart to Flow Control

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

top related