Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 02 (Part II) Introduction to C++ Programming.

Similar presentations


Presentation on theme: "Chapter 02 (Part II) Introduction to C++ Programming."— Presentation transcript:

1 Chapter 02 (Part II) Introduction to C++ Programming

2 Goal Example: –How to obtain information from the user. –How to perform arithmetic calculations Introduction to computer memory.

3 Obtaining Two Integers and Summing Them Up

4 Variables We can store data in variables. –Variable must be declared with name and data type before used. For example: int integer1; int integer2; –Several variables of same type can be written in one declaration. For example, int interger1, integer2, sum; Data typeVariable name Place a space after each comma (,) to make programs more readable.

5 Principles of naming a Variable The declaration of variables (in a block) cannot be duplicate. Use identifiers of 31 characters or fewer to ensure portability. Choosing meaningful identifiers. Avoid identifiers that begin with underscores. Always place a blank line between a declaration and adjacent executable statements.

6 Capturing Input Input stream object –std::cin Usually connected to keyboard Stream extraction operator >> –Waits for user to input value until the user presses Enter. –Stores value in variable to right of the operator »Converts value to variable data type Example –std::cin >> number1; »Reads an integer typed at the keyboard »Stores the integer in variable number1

7 Adding integers Assignment operator = –Assigns value on right to variable on left (correct) x = 3; (error)3 = x; –Binary operator (two operands) lvalue = rvalue; –Example: sum = integer1 + integer2; –Adding the values of integer1 and integer2 –Storing result in sum Remember the direction of assigning a value! int x = 3; int y = x + 2;

8 Outputting the Result Stream manipulator std::endl –Outputs a newline. –Flushes output buffer. Concatenating stream insertion operations –Also called chaining or cascading. Stream insertion operator knows how to output each type of data. –Example … sum = number1 + number2; std::cout << "Sum is “; std::cout << sum; std::cout << std::endl; std::cout << "Sum is " << number1 + number2 << std::endl; Outputs "Sum is “ Then, outputs sum of number1 and number2 Then, outputs newline and flushes output buffer

9 Obtaining Two Integers and Summing Them Up

10 2.5 Memory Concept Variable names –Correspond to actual locations (addresses) in computer's memory Every variable has name, type, size and value number1 45

11 2.5 Memory Concept A compiler maintain a table that maps variable names to real addresses, and their types (memory size). 0 1 2 3 4 5 6 7 8 9 10 Memory cell number1 2 int number2 7 int 45 72

12 2.5 Memory Concept Variable names –When new value placed into variable, overwrites old value. Writing to memory is destructive. Reading variables from memory is nondestructive. –Example: sum = number1 + number2; –Value of sum is overwritten. –Values of number1 and number2 remain intact.

13 2.6 Arithmetic Arithmetic operators –% Modulus operator returns remainder –7 % 5 evaluates to 2 –Attempting to use the modulus operator ( % ) with non-integer operands is a compilation error.

14 2.6 Arithmetic (Cont.) Straight-line form –All constants, variables and operators appear in a straight line Grouping subexpressions –Parentheses are used in C++ expressions to group subexpressions –Example a * ( b + c ) –Multiple a times the quantity b + c

15 2.6 Arithmetic Rules of operator precedence –Operators in parentheses evaluated first. –Multiplication, division, modulus applied next Operators applied from left to right –Addition, subtraction applied last Operators applied from left to right

16 Exercise


Download ppt "Chapter 02 (Part II) Introduction to C++ Programming."

Similar presentations


Ads by Google