CS112: Lab 3 :: Precedence, Color and Plotting

Part 0. Pair Programming Tips

  1. Recognize that pairing is hard
  2. Pairing is an intense relationship, summoning all your powers of ingenuity, communication, humour, and code. If you find it hard, or even just tiring, it's because it is hard and tiring sometimes. It takes effort to be kind, patient and to help steer someone in the right direction. Respect your partner and be kind. Your job is to help each other learn.
  3. Be mindful of your body language
    • Is your back to your partner?
    • Is your computer screen angled away?
    • Do you make eye contact?
    You want to create an atmosphere of inclusion so your pair feels respected and encouraged to comment.
  4. Listen: Is it too quiet between you and your partner?
  5. If there is a lot of silence, that is often a sign that someone feels left behind. Did you take over the keyboard and write a bunch of lines in silence? Yikes! Explain what you type to your partner. Or perhaps you are confused but feel uncomfortable speaking up. Try: "I don't understand this line, can you explain what it does?"

    [some content from https://www.fiddlerscode.com/blog/1\ 0-tips-for-great-pair-programming]

Part 1. Precedence

Here are MATLAB's rules*:
*The table below is adapted from Mastering MATLAB 7 by Hanselman and Littlefield.

Operator Precedence (from highest to lowest)

  1. Parentheses ()
  2. Transpose (.'), conjugate transpose ('), power (.^), matrix power (^)
  3. Unary plus (+), unary minus (-), negation (~)
  4. Multiplication (.*), matrix multiplication (*), right division (. /), left division (.\), matrix right division (/), matrix left division (\)
  5. Addition (+), subtraction (-), logical negation (~)
  6. Colon operator (:)
  7. Less than (<), less than or equal to (<=), greater than (>), greater than or equal to (>=), equal to (==), not equal to (~=)
  8. Element-wise logical AND (&)
  9. Element-wise logical OR (|)

Reminders:

  • Operators of the same precedence are evaluated from left to right
  • Precedence can be overridden with parentheses
  • Please Excuse My Dear Aunt Sally

I. Practice with Precedence

Work with your partner to evaluate these expressions (by hand!):
  1. 2 ^ 2 * 4
  2. 5 + 4 - 3 ^ 2 * 1
  3. (3 > 4) | (2 < 10) == 1
  4. alex = (10 == (8 - 6 ) * 5)
  5. 1 : 4 * 2
  6. sox = 1 < 1
  7. sox = 0 < 0.5 < 1
  8. sox = 2 == 2
  9. rem(10,4)
  10. rem(10,3)
  11. rem(10,2)
Now run each command in MATLAB to verify your answers.