Chapter 2
Chapter 2
Chapter 2
− Multiple line
/*
You can include comments that can
occupy several lines.
*/
• Special symbols
+ ?
- ,
* <=
/ !=
. ==
; >=
• Examples:
-6728
0
78
+763
• Positive integers do not need a + sign
• No commas are used within an integer
− Commas are used for separating items in a list
• bool type
− Two values: true and false
− Manipulate logical (Boolean) expressions
• true and false are called logical values
• bool, true, and false are reserved words
• Mixed expression:
− Has operands of different data types
− Contains integers and floating-point
• Examples of mixed expressions:
2 + 3.5
6 / 4 + 3.9
5.4 * 2 – 13.6 + 18 / 2
• Evaluation rules:
− If operator has same types of operands
• Evaluated according to the type of the operands
− If operator has both types of operands
• Integer is changed to floating-point
• Operator is evaluated
• Result is floating-point
− Entire expression is evaluated according to
precedence rules
• For example:
#include <iostream>
y = w + x; //Line 4: error
• Named Constant
const double CENTIMETERS_PER_INCH = 2.54;
const int INCHES_PER_FOOT = 12;
return 0;
}
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 70
Programming Example: Writing a
Complete Program
• Begin the program with comments for
documentation
• Include header files
• Declare named constants, if any
• Write the definition of the function main
The numbers you entered are 15 for feet and 7 for inches.
The total number of inches = 187
The number of centimeters = 474.98