Documente Academic
Documente Profesional
Documente Cultură
CS 101- Introductinon to
Computing
Zawar Hussain
Description
Size
Range
char
Character or small
integer
1 byte
Short Integer
2 bytes
Int
Integer
4 bytes
Long integer
4 bytes
bool
1 byte
true or false
float
4 bytes
double
Double precision
floating point number
8 bytes
long double
16 bytes
= (assignment operator)
Assigns value to a variable
Binary operator (has two operands)
Example:
// Addition program
#include <iostream>
Outline
1. Load <iostream>
4
5
int main()
2. main
2.1 Initialize variables
integer1,
prompt
integer2,
Notice how std::cin is used
to get userand sum
// declaration
//
10
// read an integer
11
12
// read an integer
13
// assignment of sum
14
std::cout << "Sum is " << sum << std::endl; // print sum
8
9
15
16
return 0;
//
17 }
input.
Program Output
2000 Prentice Hall, Inc. All rights reserved.
4 bytes
A visual representation
integer1
45
integer1
integer2
integer1
45
integer2
72
45
72
sum
117
1.22
Arithmetic
Arithmetic calculations
Use * for multiplication and / for division
Integer division truncates remainder
7 / 5 evaluates to 1
Operator precedence
Some arithmetic operators act before others (i.e.,
multiplication before addition)
Be sure to use parenthesis when needed
Do not use: a + b + c / 3
Use: (a + b + c ) / 3
2000 Prentice Hall, Inc. All rights reserved.
1.22
Arithmetic
Arithmetic operators:
C++ operation Arithmetic
operator
+
Addition
Algebraic
expression
f+7
C++ expression
Subtraction
Multiplication
pc
bm
p - c
b * m
Division
x/y
x / y
Modulus
rmods
r % s
f + 7
Operation(s)
()
Parentheses
*, /, or %
+ or -
Addition
Subtraction
10
Arithmetic Example
Step 1.
y = 2 * 5 * 5 + 3 * 5 + 7;
(Leftmost multiplication)
2 * 5 is 10
Step 2.
y = 10 * 5 + 3 * 5 + 7;
(Leftmost multiplication)
10 * 5 is 50
Step 3.
y = 50 + 3 * 5 + 7;
3 * 5 is 15
Step 4.
y = 50 + 15 + 7;
(Leftmost addition)
50 + 15 is 65
Step 5.
y = 65 + 7;
(Last addition)
65 + 7 is 72
Step 6.
y = 72;
(Last operationplace 72 in y )
using std::cout;
using std::cin;
using std::endl;
11
12
C++ equality
or relational
operator
Example
of C++
condition
Meaning of
C++ condition
Relationaloperators
>
>
x>y
xisgreaterthany
<
<
x<y
xislessthany
>=
x>=y
xisgreaterthanorequaltoy
<=
x<=y
xislessthanorequaltoy
Equalityoperators
==
x==y
xisequaltoy
!=
x!=y
xisnotequaltoy
13
14
2.6
grade >= 60
print Failed
C++ code
if ( grade >= 60 )
cout << "Passed";
else
cout << "Failed";
true
print Passed
15
2.6
grade >= 60
print Failed
true
print Passed