Download presentation
Presentation is loading. Please wait.
Published byByron Adams Modified over 9 years ago
1
Q and A for Chapter 7 Students of CS104, and me, your benevolent professor, Victor T. Norman, PhD
2
Multiple Assignment Q: How would using multiple assignment be helpful? If you want to store two different numbers, why not just use two different variables? A: Consider swapping two values, where i and j were assigned values previously. Then to swap: itemp = i i = j j = itemp
3
Updating variables Q: Please explain the updating variables: if you update it, then is the original value gone forever then? A: Yes, the original value is gone. You can tell this by drawing the picture of what x = x + 1 does. Note: if you use x = x + 1 or x += 1, you have to declare x and initialize it to a value beforehand. E.g., you have to write x = 0 beforehand.
4
Augmented Assignment Q: Is there an augmented assignment like -= to decrement a value? A: Yes! There is also *=, /=, **=, %=, etc., etc. Lots of them. If you see var = var you can convert to var =
5
Before we tackle while loops So far our code has been sequential – interpreter runs one line and then next line and then next line… Or, conditional (if statements) – interpreter runs one set of line or another set of lines depending on boolean expression. Or, we jump to a function call. – which then runs sequentially or with conditions in it.
6
while loops Our first iterative statement. – Code “jumps” back up to previous statements. Pattern: while : Notice similarity to if statement: if : but, while executes the body repeatedly until the boolean expression is false.
7
while loops (2) Q: (Prof’s question) Suppose you have a while loop like this: while var1 var2: Is it good/bad/ugly to alter the value of var1 or var2 in the body of the loop? A: You almost *always* alter the value of var1 or var2 in the body, or you would have an infinite loop.
8
while loops (3) Q: (Prof’s question) Is the body of a while loop always executed? A: No: if the boolean expression is False the first time, the body is not run at all. Q: How often is the body of a while loop executed? A: Until the boolean expression is False.
9
Usefulness of infinite loop? Q: Are there interesting applications of infinite loop? Are infinite loops ever used? A: Infinite loops are used a lot, actually. Your beloved cellphone does this, basically: while True: wait for event (button pushed, etc.) figure out what user wants to do do what user asked.
10
break Q: Could we go over what a break is a little more? When and how to use this? Why would you want to get out of the loop? Can it make a loop skip a portion of code? A: break can be very useful. As the book says, you can use it to test when to get out of a while loop in the middle of the body. (Let’s rewrite the loop in 7.4 without the break.)
11
Algorithms Q: The idea of making algorithms is still vague to me. Can you give more examples? A: Algorithm for cleaning a whiteboard. Algorithm for cutting a pan of brownies into equal-sized pieces. Algorithm for deciding how to dry yourself when you get out of the shower. Algorithm for making a scribbler find its way out of a maze…
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.