3 1
3 1
• Special symbols
+ ?
- ,
* <=
/ !=
. ==
; >=
3
Reserved Words (Keywords)
4
Problem with integer datatype
5
Data Types
6
Simple Data Types
7
Simple Data Types (continued)
8
Simple Data Types (continued)
9
int Data Type
• 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
10
bool Data Type
• 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
11
char Data Type
12
Floating-Point Data Types
14
Arithmetic Operators
• C++ arithmetic operators:
− + addition
− - subtraction
− * multiplication
− / division
− % modulus operator
• +, -, *, and / can be used with integral and floating-
point data types (and they sometimes behave
differently with different types!)
• Operators can be unary (one operand) or binary (two
operands)
15
#include <iostream>
What is the output?
using namespace std;
void main()
{
cout << "-2 + 5 = " << -2 + 5 << endl;
cout << "10 - 20 = " << 10 - 20 << endl;
cout << "2 * 7 = " << 2 * 7 << endl;
cout << "5 / 2 = " << 5 / 2 << endl;
cout << "5.0 / 2 = " << 5.0 / 2 << endl;
cout << "5 / 2.0 = " << 5 / 2.0 << endl;
cout << "34 % 5 = " << 34 % 5 << endl;
cout << "4 % 6 = " << 4 % 6 << endl;
getch();
}
Operator, operands and operations
18
Expressions
20
Mixed Expressions
• 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
21
Mixed Expressions (continued)
• 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
22
What is the output?
#include <iostream>
#include<conio.h>
using namespace std;
void main()
{
cout << "3 / 2 + 5.5 = " << 3 / 2 + 5.5 << endl;
cout << "10.6 / 2 + 5 = " << 10.6 / 2 + 5 <<
endl;
cout << "4 + 5 / 2.0 = " << 4 + 5 / 2.0 << endl;
cout << "4 * 3 + 7 / 5 - 12.5 = "
<< 4 * 3 + 7 / 5 - 12.5
<< endl;
getch();
}
Identifiers
24
Identifiers (continued)
25
Prompt Lines