Computer Science E50a Wednesday Section . 7:30 . Emerson 106 TF: Alix Marchandise-Franquet October 28, 1998 C++, the basics Variables A variable consists of 3 things: - a type (ex: int, char, float...) - a name (ex: x, foo, blah...) - a value. This value can change throughout the program (that's actually the whole point of variables...) Things you can do with variables: - declare them (mandatory before you do anything else with a given variable) Just like you used to declare robots at the beginning of a task, you have to declare variables before you use them in a given function. The declaration is as follows: ; -at declaration time, you also have the option of initializing the variable, i.e. give it an initial value. This is usually a good idea, because otherwise the value of your variable will be whatever was in that part of memory before it was allocated to you. You can run in trouble if you use a variable but never gave it a value. Here is an example of variable declaration with initialization: int i = 5; // this creates a variable of type integer, called i, with initial value 5 Where should you declare/initialize variable? Officially, at the top of your functions, but in fact you can do it anywhere. However, be careful: - do not declare a variable after you use it (will not compile) - when you declare a variable in a given block ( a piece of code surrounded by curly braces) it will only exist in that given block. What is shadowing? You can declare a variable (for ex: int i=5; ) at the top of main(), and then declare the same variable in a block inside of the main block (for ex: int i=0 declared in a for loop). Inside of the inner block, i will start with value 0, but when you exit the block (at the closing curly brace) i is back to its original value in main (i.e. 5 if it hasn't been changed since its initialization). this is called shadowing. Beware of it for now, you might run into trouble... Where should you assign a value to a variable? As we discussed above, you can do this at declaration time. You can also do it at any point AFTER the declaration. Simlpy use the assignment operator = ex: int x, y; int z = 3; // z has value 3 x = 2; // x has value 2 y = x + z; // y has value 5 x += z; // x now has value x+z = 5 Constants The difference between constants and variables is that you have to declare and initialize a constanbt at once. After you have assigned a value to a constant, the value cannot be changed in the program. Use constants to avoid "magic numbers" in your programs. ex: const int MIN_IN_HOUR = 60; // constants are in caps IF You are already familiar with the concept of "if" from Karel++. The difference is that in C++ we have variables, and therefore we can test for more things. - we can still test for the return value of a boolean function (remember if (frontIsClear()) ?) - we can also test for things such as the value of a variable ex: x < 2, x > 2, x == 2, x != 2 Note: there are still two boolean values in C++: true and false. True has value 1 (well, actually, anything that is not 0) and false has value 0. IF... ELSE IF... ELSE Again, you are familiar with else. Else if is used when you want to test for several possibilities. if (A) // do X else if (B) // do Y else // do Z You will always only do X, Y or Z. In Karel, we would have written this as follows: if (A) {// do X} else { if (B) { do Y} else { do Z} } FOR You can think of the for loop as a replacement for the "loop" instruction in Karel++, since you will iterate a given number of times through the body of the for loop (including possibly an infinite number of times). for (initial value; condition; increment) { // do something } WHILE Remember while loops from Karel++ ? initialize; while (condition) { // do something increment; } Note: while and for loops can be used interchangeably. Practice changing a for loop into a while loop and vice versa. INCLUDE At the top of your source file, you might need to include header files. Which files you need to include depends on what you do in your program... For now, you should be fine only with iostream.h #include MAIN Main is the function that corresponds to task in Karel++. MAN Learn how to use "man" pages on UNIX. At the prompt, just type man to get a page with explanations.