1 / 11

Q and A for Chapter 7

Q and A for Chapter 7. Students of CS104, and me, your benevolent professor, Victor T. Norman, PhD. 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?

lundy
Télécharger la présentation

Q and A for Chapter 7

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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<op> <value> you can convert to var<op>= <value>

  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 <boolean expression>: <body of loop> • Notice similarity to if statement:if <boolean expression>: <body of 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 <op> var2: <body> 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…

More Related