Introductory Programming Fall 2006 For today, you should have: 1) Finished Homework 1. 2) Prepared for an evaluation. Today: 1) Evaluation 1 2) updating variables 3) if and for statements For next Tuesday you should: 1) Read Chapter 2 Updating variables ------------------ What do these lines of code do? x = 5 x = x + 1 The second one looks funny, because in math it would be an equation with no solutions. But as an assignment statement, it makes perfect sense. Remember that the order of events is 1) evaluate the right hand side. 2) create the new variable if necessary. 3) assign the value of the RHS to the variable. So what happens if you try this y = y + 1 Simultaneous updates -------------------- Suppose we have two variables, a and b, and we want to update them both. For example, imagine two car lots, with a cars on Lot A and b cars on Lot B. Each day, 3% of the cars from Lot A go to Lot B and 5% of the cars from Lot B go to Lot A Write a few lines of code that compute the new values of a and b based on the old values. Store your code in a file named update.m Now start with the initial values a = 200, b = 100 And call your script 10-20 times (up-arrow, return) What happens to the values of a and b? if statements ------------- n = n + 1 if n < 10 update end for loops --------- for i=1:10 update end fprintf ------- Formatted printing (ugly feature!) fprintf('%f\t%f\n', a, b)