Class 9 Lecture Notes


Published on

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

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Class 9 Lecture Notes

  1. 1. Class 9 – Flow Control: Loops
  2. 2. Agenda <ul><li>Warm-up, Issues </li></ul><ul><li>Controlling Flow with loops </li></ul><ul><li>Quest: Defining and calling a Ruby method </li></ul><ul><li>Assignment 6 </li></ul><ul><li>Mid-term Exam </li></ul>
  3. 3. Warm-up and Issues <ul><li>Lateral Thinking Exercise </li></ul><ul><li>Project Issues? </li></ul><ul><li>Other Issues? </li></ul><ul><li>Remaining Activities/ Work Plan </li></ul><ul><ul><li>Dates </li></ul></ul>
  4. 4. Flow Control: Looping <ul><li>Iteration refers to the ability to repeat similar actions conditionally rather than requiring a user-entered signal to stop. </li></ul><ul><li>Iterations may be loops using “while-end” or “until-end” or “for-end” or may be an array method using “.each” </li></ul><ul><li>We will examine only loops tonight and save the “each” method for next week when we cover arrays. </li></ul>
  5. 5. Looping <ul><li>Scenarios </li></ul><ul><ul><li>Score Averager – what if you don’t know how many students there will be? </li></ul></ul><ul><ul><li>A persistent converter </li></ul></ul>
  6. 6. Looping Notation <ul><li>Pseudocode (sample) </li></ul><ul><li># get the number of rounds to complete </li></ul><ul><li># set a counter to value 0 </li></ul><ul><li># while the counter is less than the number of rounds </li></ul><ul><li># do some stuff here </li></ul><ul><li># increase the counter </li></ul><ul><li># end the loop </li></ul>
  7. 7. Looping Flowchart <ul><li>The Loop </li></ul><ul><ul><li>Decision implicit </li></ul></ul><ul><ul><li>May evaluate true or false condition </li></ul></ul><ul><ul><li>May use a counter to advance value </li></ul></ul>start end Assignment name? Set counter = 0 Counter>10? Calculate Average Print Average Increment Counter Student Grade? Yes No
  8. 8. Looping Ruby Code <ul><li>While-end (a condition is true at the outset) </li></ul><ul><li>var_type=“any wine” </li></ul><ul><li>while var_type!=“exit” </li></ul><ul><li>puts “what type of wine do you like (type exit to end)” </li></ul><ul><li>var_type=gets.chomp </li></ul><ul><li>puts ”Mmmmm. I like ” + var_type + “,too!” </li></ul><ul><li>end </li></ul>
  9. 9. Looping Ruby Code <ul><li>Until-end (condition is false at the outset) </li></ul><ul><li>var_type=“any wine” </li></ul><ul><li>until var_type==“exit” </li></ul><ul><li>puts “what type of wine do you like (type exit to end)” </li></ul><ul><li>var_type=gets.chomp </li></ul><ul><li>puts ”Mmmmm. I like ” + var_type + “,too!” </li></ul><ul><li>end </li></ul>
  10. 10. Looping Ruby Code <ul><li>For-end (uses a range of values) </li></ul><ul><li>for i in (1..12) </li></ul><ul><li>puts i.to_s + “ o’clock and all is well” </li></ul><ul><li>end </li></ul><ul><li>for x in (“man”..”max”) </li></ul><ul><li>puts x </li></ul><ul><li>end </li></ul>
  11. 11. Exercise <ul><li>Sample: Can I do it until I wear glasses? </li></ul><ul><li>Instructions: </li></ul><ul><ul><li>Develop a loop that will ask user to tell something about himself/herself </li></ul></ul><ul><ul><li>Respond to each prompt in some way, e.g. “Interesting! Tell me more!” </li></ul></ul><ul><ul><li>Continue to loop until the user says “I wear glasses” </li></ul></ul><ul><ul><li>Then say “Bye!” and end program </li></ul></ul>
  12. 12. Quest: Defining and Calling a Method <ul><li>What is a method? </li></ul><ul><ul><li>A function or operation that may be used several times in the application </li></ul></ul><ul><ul><li>May perform a calculation on variables </li></ul></ul><ul><ul><li>May return a value </li></ul></ul><ul><ul><li>Must be at top of file in Ruby </li></ul></ul><ul><li>Why? </li></ul><ul><ul><li>Shortened code, consistent results </li></ul></ul><ul><ul><li>Single code block to edit </li></ul></ul>
  13. 13. Ruby: Declare/ Call Method <ul><li>Example </li></ul><ul><li>def doFtoC (fVal) </li></ul><ul><li>temp=fVal*9/5 + 32 </li></ul><ul><li>return temp.round(2) </li></ul><ul><li>end </li></ul><ul><li>puts “enter fahrenheit temp” </li></ul><ul><li>f_temp=gets.chomp.to_f </li></ul><ul><li>puts “Celsius =” + doFtoC(f_temp).to_s </li></ul>
  14. 14. One last example <ul><li>Ruby – the wine connoisseur </li></ul><ul><li>Your turn: Assignment 6 </li></ul><ul><ul><li>From Flowchart to Flow Control </li></ul></ul>
  15. 15. Wrap-up <ul><li>Summary </li></ul><ul><ul><li>Iteration – repeating functions/operations </li></ul></ul><ul><ul><li>Loops – a kind of iteration </li></ul></ul><ul><ul><li>Methods: while, until, for </li></ul></ul><ul><li>Next Week </li></ul><ul><ul><li>Arrays, Intro Project 2 </li></ul></ul><ul><ul><li>Assignment 6 due at start of class </li></ul></ul>