CS100A, Fall 1997 This lecture covers the declaration of variables, assignment, and the if statement. While discussing the declaration of variables, it will be important to mention that variables have meaning and that writing down that meaning at the point of declaration is extremely important for developing correct programs and for understanding programs. At this point in the course, the only types the students have seen is int, float, long, and the like; they don’t know what a class is. We will mention the modifier final, which is used for constants. The debugger will be used in the lecture to demonstrate execution of the assignment. For example, after slide 5 is discussed, we’ll illustrate execution of that program within the debugger, showing carefully how variable timeOfDay changes during execution. The debugger will be used to illustrate assignment, declaration of a final variable (a constant), and the if statement. CS100A, Fall 1997. Lecture 2 1 CS100A, Fall 1997 Concepts for this lecture: • Variables and constants • Declaration of a variable • Assignment to variable • Declaration of a constant • The conditional (if) statement Reading: Lewis/Loftus 75-82, Lewis/Loftus 92-93, 97-100 CS100A, Fall 1997. Lecture 2 2 Variable: a placeholder for a value; a box into which a value can be placed. Examples of declarations int x; int timeOfDay; x -49 TimeOfDay 1115 CS100A, Fall 1997. Lecture 2 3 Variable names (identifiers). • Begin with a letter, _, or $ • Contain letters, digits, _ and $ Generally, we don’t use _ or $ Convention for variable names: first letter small; capitalize words within the identifier. timeOfDay f12 f0 restOfInput CS100A, Fall 1997. Lecture 2 4 New statement (or command): Assignment to a variable. int timeOfDay = 3; // timeOfDay 3 timeOfDay= 1200; // timeOfDay 1200 timeOfDay= 3 + timeOfDay + 2; // timeOfDay 1205 CS100A, Fall 1997. Lecture 2 5 Form of assignment: <variable> = <expression> ; Here, = does not mean equality! In Java, Read x= y; as “Assign y to x” or “x becomes y” Read “x==y as “x equals y” x==y (yields true if x and y contain the same value and false otherwise) CS100A, Fall 1997. Lecture 2 6 = introduced as a sign for equality by Robert Recorde (1510-1558). He chose = because nothing could be more equal than two parallel lines. = for equality has been a worldwide convention for over 100 years. The use of = for assignment was started in FORTRAN in the early 1950s. C (about 1970) and then C++ (1980s) continued to use = for assignment and introduced == for equality; thus bringing great confusion. This use of = for assignment and requirement of == for equality has caused more confusion and waste of time in the whole world than any other notational convention. Algol 60 used := for assignment, = for equality CS100A, Fall 1997. Lecture 2 7 • Short names make program shorter, perhaps more manageable. • Long names can be used to convey something about the meaning of a variable. • However, a name can never (almost) give the full, precise meaning, so it is best to give a comment at the declaration of a variable to give its full meaning. You’ll see many examples of this as the course progresses. CS100A, Fall 1997. Lecture 2 8 The conditional statement (if statement). Allows a choice of command based on some condition. // Store the maximum of x,y in z z= x; if (y>x) z:= y; syntax: if ( <boolean expression> ) statement CS100A, Fall 1997. Lecture 2 9 Using a block statement: // if x != y, store 0 in x and store y in z if (y!=x) {x= 0; z= y;} or // if x != y, store 0 in x and store y in z if (y!=x) { x= 0; z= y; } CS100A, Fall 1997. Lecture 2 10 The block statement {<statement 0> <statement 1> <statement 2> … } { and } are used to delimit a sequence of statements that are to act together as a single statement, in the same way that parentheses are used to aggregate in arithmetic expressions. CS100A, Fall 1997. Lecture 2 11 Second form of if statement // store maximum of x and y in z if (x > =y) {z= x;} else {z= y;} The rest of the lecture will demonstrate the use of the assignment and if statements in Codewarrior on the a Macintosh. CS100A, Fall 1997. Lecture 2 12