Q and A for Chapter 7 Students of CS104, and me, your benevolent professor, Victor T. Norman, PhD.

11
Q and A for Chapter 7 Students of CS104, and me, your benevolent professor, Victor T. Norman, PhD

Transcript of Q and A for Chapter 7 Students of CS104, and me, your benevolent professor, Victor T. Norman, PhD.

Page 1: Q and A for Chapter 7 Students of CS104, and me, your benevolent professor, Victor T. Norman, PhD.

Q and A for Chapter 7

Students of CS104, and me, your benevolent professor, Victor T.

Norman, PhD

Page 2: 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?A: Consider swapping two values, where i and j were assigned values previously. Then to swap:itemp = ii = jj = itemp

Page 3: Q and A for Chapter 7 Students of CS104, and me, your benevolent professor, Victor T. Norman, PhD.

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.

Page 4: Q and A for Chapter 7 Students of CS104, and me, your benevolent professor, Victor T. Norman, PhD.

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

Page 5: Q and A for Chapter 7 Students of CS104, and me, your benevolent professor, Victor T. Norman, PhD.

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.

Page 6: Q and A for Chapter 7 Students of CS104, and me, your benevolent professor, Victor T. Norman, PhD.

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.

Page 7: Q and A for Chapter 7 Students of CS104, and me, your benevolent professor, Victor T. Norman, PhD.

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.

Page 8: Q and A for Chapter 7 Students of CS104, and me, your benevolent professor, Victor T. Norman, PhD.

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.

Page 9: Q and A for Chapter 7 Students of CS104, and me, your benevolent professor, Victor T. Norman, PhD.

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.

Page 10: Q and A for Chapter 7 Students of CS104, and me, your benevolent professor, Victor T. Norman, PhD.

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.)

Page 11: Q and A for Chapter 7 Students of CS104, and me, your benevolent professor, Victor T. Norman, PhD.

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…