Presentation is loading. Please wait.

Presentation is loading. Please wait.

Alternate Version of STARTING OUT WITH C++ 4th Edition

Similar presentations


Presentation on theme: "Alternate Version of STARTING OUT WITH C++ 4th Edition"— Presentation transcript:

1 Alternate Version of STARTING OUT WITH C++ 4th Edition
Chapter 2 Introduction to C++ Copyright 2004 Scott/Jones Publishing

2 Variable Assignment vs. Initialization
Initializing a variable Gives an initial value to a variable at the time it is created Can initialize some or all variables int length;  int length = 12; length = 12; See pr2-18.cpp Chapter 2 slide 2

3 2.14 Scope The scope of a variable
That part of the program where the variable can be used A variable cannot be used before it is defined int a; cin >> a; // legal cin >> b; // illegal int b; See pr2-19.cpp Chapter 2 slide 3

4 2.15 Arithmetic Operators Used for performing numeric calculations
C++ has unary, binary, and ternary operators unary (1 operand) binary (2 operands) ternary (3 operands) exp1 ? exp2 : exp3 Chapter 2 slide 4

5 Binary Arithmetic Operators
SYMBOL OPERATION EXAMPLE ans + addition ans = 7 + 3; 10 - subtraction ans = 7 - 3; 4 * multiplication ans = 7 * 3; 21 / division ans = 7 / 3; 2 % modulus ans = 7 % 3; 1 See pr2-20.cpp Chapter 2 slide 5

6 / Operator C++ division operator (/)performs integer division if both operands are integers cout << 13 / 5; // displays 2 cout << 2 / 4; // displays 0 If either operand is floating-point, the result is floating-point cout << 13 / 5.0; // displays 2.6 cout << 2.0 / 4; // displays 0.5 Chapter 2 slide 6

7 % Operator C++ modulus operator (%) computes the remainder resulting from integer division cout << 9 % 2; // displays 1 % requires integers for both operands cout << 9 % 2.0; // error Chapter 2 slide 7


Download ppt "Alternate Version of STARTING OUT WITH C++ 4th Edition"

Similar presentations


Ads by Google