Class # 3 Topics:
Summary from previous class January 7, 2001 Introduction to C++ Programming Some Common Escape Sequence (cont) An Addition Program (using cin) Memory Concepts Arithmetic Operation Precedence of Arithmetic Operators Zeki Aliyazicioglu Decision Making: Equality and relational Operators Electrical & Computer Eng. Cal Poly
A simple Program: Printing a Line of Text Some Common Escape Sequence // Fig. 1.2: fig01_02.cpp // indicates that the remainder of each line is a command // A first program in C++ \n New line. Goes to beginning of the next line #include
Example: Example: 1 // Fig. 1.4: fig01_04.cpp 2 // Printing a line with multiple statements 1 // Fig. 1.5: fig01_05.cpp 3 #include
1 Example: An Addition Program Fig 1-6 (cont)
1 // Fig. 1.6: fig01_06.cpp 11 std::cout << "Enter second integer\n"; // prompt 2 // Addition program 12 std::cin >> integer2; // read an integer 3 #include
Notes: (Continue) Notes: The other data types l double (used for specifying real number) Line int integer1, integer2, sum; is a declaration. l char (for specifying character data. May hold only a single integer1, integer2, and sum are the names of letter. variables. A variable name is any valid identifier, which Line 10 l series of character consisting of letter, digits, and underscore std::cin >> integer1 (_) that does not begin with a digit. l Uses the input stream object cin and the stream l Case sensitive extraction operator >> to obtain value from the l Use max 31 characters keyboard. l No reserved or keyword l It waits for user to enter a value for integere1 The declaration specifies the data types int of l User responds by typing an integer number then variables. These variables will hold integer values press the Enter key to send the number the All variables must be declared with name and data computer type before using in the program l This number will be integer1 value.
Notes: (Continue) Memory Concepts Line 13 sum = integer1 + integer2; Every variable has a name, a type, a size and Calculates sum of the variables integre1 and intger2 and assign the result to variable sum using a value. assignment operator =. Each variable is placed into a memory location. Line 14 Whenever a value is placed in a memory std::cout << "Sum is " << sum << endl; location, the value replaces the previous Prints character string Sum is followed by the value in that location. The previous value is numerical value of variable sum. lost. std::endl manipulator outputs a new-line then “flushes the output buffer”
2 Arithmetic Operation Precedence of Arithmetic Operators
C+++ Arithmetic Algebraic C++ Operation Operation Operations Expression
Addition + f + 7 f + 7 Operators Operations Order of Evaluation
Subtraction - p - c p - c Evaluate first. If there are ( ) Parentheses several, evaluate left to right
Multiplication * bm b * m Multiplication Evaluate second. If there are *,/,or % Division several evaluate left to right Division / x / y x / y Modulus Addition Evaluate last. If there are Modulus % r mod s r % s + or - Subtraction several, evaluate left to right
3