CS 101-Introductinon To Computing: Zawar Hussain
CS 101-Introductinon To Computing: Zawar Hussain
CS 101-Introductinon To Computing: Zawar Hussain
Outline
CS 101- Introductinon to
Computing
Zawar Hussain
Variables
• Variable names
– Correspond to locations in the computer's memory
– Every variable has a name, a type, a size and a value
• int myVariable = 10; 4 bytes
– Whenever a new value is placed into a variable, it replaces
the previous value - it is destroyed Value Size ?
Type Name
– Reading variables from memory does not change them
• A visual representation
integer1 45
Variables
integer1 45
integer1 45
integer2 72
integer2 72
sum 117
Variables
• Variables
– Location in memory where a value can be stored for use by a
program
– Must be declared with a name and a data type before they
can be used
– Some common data types are:
• int - integer numbers
• char - characters
• double - floating point numbers
– Example: int myvariable;
• Declares a variable named myvariable of type int
– Example: int variable1, variable2;
• Declares two variables, each of type int
Program Output
1.22 Arithmetic
• Arithmetic calculations
– Use * for multiplication and / for division
– Integer division truncates remainder
• 7 / 5 evaluates to 1
– Modulus operator returns the remainder
• 7 % 5 evaluates to 2
• Operator precedence
– Some arithmetic operators act before others (i.e.,
multiplication before addition)
• Be sure to use parenthesis when needed
– Example: Find the average of three variables a, b and c
• Do not use: a + b + c / 3
• Use: (a + b + c ) / 3
1.22 Arithmetic
• Arithmetic operators:
C++ op era tion Arithmetic Alg eb ra ic C++ exp ression
op era tor exp ression
Addition + f+7 f + 7
Subtraction - p–c p - c
Multiplication * bm b * m
Division / x/y x / y
Modulus % r mod s r % s
Arithmetic Example
Step 1. y = 2 * 5 * 5 + 3 * 5 + 7; (Leftmost multiplication)
2 * 5 is 10
50 + 15 is 65
Relational operators
> > x > y x is greater than y
< < x < y x is less than y
Equality operators
= == x == y x is equal to y
!= x != y x is not equal to y
If <condition> then
operations for the then-part
Else
operations for the else-part
E1 and E2
true if both are true; false otherwise
E.g. 3 > 2 and 2 > 3 is false
not E
true if E is false, false if E is true