Lecture 2 Variables and Operators
Lecture 2 Variables and Operators
Introduction
Week 01 Sp 25
2
Acknowledgement – Ms. Nabia Khalid worked on the slides
Summary from previous lecture!
• Computer Program & Computer Software
• Application Software & System Software
• Computer Languages
• Machine, Assembly, High level languages
• Programming paradigms
• Procedural & Object Oriented
• Programming Bugs
• Compiler, linker, run-time, conceptual errors
Scope!
• Binary Number System • Rules of operator precedence
• Decimal to Binary conversion
• Relational operators
• Equality and Assignment Operators
• Binary to Decimal conversion
• Logical operators
• Representation of Binary Numbers in • Truth table of logical operators
Memory
• Conditional operators
• Binary Representation of Signed Numbers • Bitwise Operators
• Variables • Type conversion
• Variables Declarations • All Operators precedence
• Operators
• Arithmetic operators
Decimal (base 10) number
system consists of 10 symbols
or digits
0123456789
Binary (base 2) number system
consists of just two
01
Binary Numbers
• Each binary digit (called a bit) is either 1 or 0
• Bits have no inherent meaning, they can represent …
• Unsigned and signed integers
• Fractions Most Least
Significant Bit Significant Bit
• Characters
• Images, sound, etc.
• Bit Numbering
• Least significant bit (LSB) is rightmost (bit 0)
• Most significant bit (MSB) is leftmost (bit 7 in an 8-bit
number)
Counting Counting
in Decimal in Binary
0 10 20 30 0 1010 10100 11110
1 11 21 31 1 1011 10101 11111
2 12 22 32 10 1100 10110 100000
3 13 23 33 11 1101 10111 100001
4
14 24 34 100 1110 11000 100010
5
15 25 35 101 1111 11001 100011
6
16 26 36 110 10000 11010 100100
7
8
17 27 . 111 10001 11011 .
9 18 28 . 1000 10010 11100 .
19 29 . 1001 10011 11101 .
Binary Numbers
Coefficient have two possible values 0 and 1
Strings of binary digits (“bits”)
n bits can store numbers from 0 to 2n -1
n bits can store 2n distinct combinations of 1’s and 0’s
Each coefficient aj is multiplied by 2j
So 101 binary is
1 x 22 + 0 x 21 + 1 x 20
or
1x4 + 0x2 + 1x1=5
Decimal to Binary Conversion
Convert 75 to binary!
2 75 remainder
2 37 1
2 18 1
2 9 0
2 4 1
2 2 0
1 0
1001011
Binary to Decimal!
= 1 + 2 + 0 + 8 + 0 + 0 + 64
= 75
Convert 100 to Binary
2 100 remainder
2 50 0
2 25 0
2 12 1
2 6 0
2 3 0
1 1
1100100
Convert the following?
a) 1310 = ?
b) 2210 = ?
c) 4310 = ?
d) 15810 = ?
Convert the following? …Solution
a) 1310 = ? 11012
b) 2210 = ? 101102
c) 4310 = ? 1010112
d) 15810 = ? 100111102
Converting Binary to Decimal
1 0 1 0 1 1 0
0
128 + 32 + 8 + 4 = 172
Converting Binary to Decimal
- - - -
12 64 32 16 8 4 2 1
8
0 + 0 + 0 + 16 + 0 + 4 + 2 + 1
16 + 4 + 2 + 1 = 23
Binary → Dec : More Examples
a) 0110 2 = ? 6 10
b) 11010 2 = ? 26 10
c) 0110101 2 = ? 53 10
d) 11010011 2 = ? 211 10
Decimal Fraction to Binary and Vice
Versa!
(0.75)10 = (?)2 • 0.110
• 0*20. (1*2-1)+(1*2-2)+(0*2-3)
0/2=0
• 0*20. (1*½)+(1*1/4)+(0*1/8)
• 0.75*2-> 1.50->1
• 0.(1/2)+(1/4)+0
• 0.50*2->1.00->1
• 0.(3/4)+0
• 0.00*2->0.00->0 • 0.75+0
• 0.110 • 0.75
Representation of Binary Numbers in Memory
Nibble
4 bits form a Nibble
“1011” is One Nibble of Information 3 2 1 0
1 1 0 1 = 13
23 22 21 20
Nibble Values: 3 2 1 0
0000 0 0 0 0 =0
23 22 21 20
1111 3 2 1 0
1 1 1 1 = 15
23 22 21 20
Byte Values: 7 6 5 4 3 2 1 0
00000000
0 0 0 0 0 0 0 0 =0
27 26 25 24 23 22 21 20
11111111 7 6 5 4 3 2 1 0
1 1 1 1 1 1 1 1 = 255
27 26 25 24 23 22 21 20
2 Bytes Value:
0000000000000000
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 =0
215 214 213 212 211 210 29 28 27 26 25 24 23 22 21 20
1111111111111111
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 = 65,535
215 214 213 212 211 210 29 28 27 26 25 24 23 22 21 20
3 Bytes
• 24 bits form 3 bytes
• “000000000001110110011101” is Three Byte of Information
23 22 . . . . . 8 7 6 5 4 3 2 1 0
0 0 0 1 1 1 0 1 1 0 0 1 1 1 0 1 = 7,581
223 222 2. 2. 2. 2. 2. 28 27 26 25 24 23 22 21 20
• 3 Byte Values:
• 000000000000000000000000
23 22 . . . . . 8 7 6 5 4 3 2 1 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 =0
223 222 2. 2. 2. 2. 2. 28 27 26 25 24 23 22 21 20
• 111111111111111111111111
23 22 . . . . . 8 7 6 5 4 3 2 1 0
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 = 16,777,216
223 222 2. 2. 2. 2. 2. 28 27 26 25 24 23 22 21 20
= 16.77 Millions
4 Bytes -> int and long in C++
• 32 bits form 4 bytes
• “000000000001110110011101” is Four Byte of Information
31 30 . . . . . 8 7 6 5 4 3 2 1 0
0 0 0 1 1 1 0 1 1 0 0 1 1 1 0 1 = 7,581
• 4 Byte Values: 231 230 2. 2. 2. 2. 2. 28 27 26 25 24 23 22 21 20
• 00000000000000000000000000000000
31 30 . . . . . 8 7 6 5 4 3 2 1 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 =0
231 230 2. 2. 2. 2. 2. 28 27 26 25 24 23 22 21 20
• 11111111111111111111111111111111
31 30 . . . . . 8 7 6 5 4 3 2 1 0
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 = 4,294,967,296
231 230 2. 2. 2. 2. 2. 28 27 26 25 24 23 22 21 20
= 4.29 Billions
Signed Binary Representation
• MSB: if 0, positive; if 1, negative
6 5 4 3 2 1 0 6 5 4 3 2 1 0
0 0 0 1 1 1 0 1 = 29 1 0 0 1 1 1 0 1 = -29
26 25 24 23 22 21 20 26 25 24 23 22 21 20
• Minimum value
6 5 4 3 2 1 0
1 1 1 1 1 1 1 1 = -128
26 25 24 23 22 21 20
• Maximum value
6 5 4 3 2 1 0
0 1 1 1 1 1 1 1 = +127
26 25 24 23 22 21 20
Back to C++ Example
//Preprocessor Directives
#include<iostream>
#include<conio.h>
using namespace std;
//-----------------------------------
// Main Function
int main()
{
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
/* Wirte a program that takes a value
from user as his/her
Fixed Part of
confusion level out of 100 the Program
and prints his/her confidence level*/
int confusionLevel;
cout << "Enter your confusion level out of 100% : ";
cin >> confusionLevel;
cout << "Dont worry, you are " << 100 - confusionLevel << "%
confident";
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
_getch();
return 0;
}
Back to C++
//Preprocessor Directives
#include<iostream>
#include<conio.h>
using namespace std;
//-----------------------------------
// Main Function
int main()
{
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
/* Wirte a program that takes a value
from user as his/her
Data Type
confusion level out of 100
and prints his/her confidence level*/
int confusionLevel;
Memory is
cout << "Enter your confusion level out of 100% : "; Requested
cin >> confusionLevel;
cout << "Dont worry, you are " << 100 - confusionLevel << "% confident";
from compiler
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
_getch();
return 0;
}
Declaring a variable!
• Declaring a variable means specifying both its name and its data type.
• Declarations
• Variable Declarations
• Constant Declarations
• Variable Declarations
• Syntax
<type> <identifier> = <expression>;
• Example
int confidenceLevel = 100;
Declaring a Constant!
• Constant Declarations
• Syntax
const <type> <identifier> = <expression>;
• Example
• const double PI = 3.1459;
Declaration!!
• Variable Declarations
• Variables are used to store values that can be changed during the program execution
• Syntax:
< type > < identifier >;
< type > < identifier > = < expression>;
• Examples:
int sum;
int total = 3445;
char answer = 'y';
double temperature = -
3.14
Declaration!!
• A variable has a type and it can contain only values of that type. For
example, a variable of the type int can only hold integer values
• Variable = Expression
• First, expression on right is evaluated.
• Then the resulting value is stored in the memory location of Variable on left.
information
• Name
• Value
• Memory space
• Example: somebody’s Confidence
Level
confidenceLevel 80
• An integer variable confidenceLevel;
• confidenceLevel = 80;
C++ Datatypes!
• Byte Values: 7 6 5 4 3 2 1 0
• 00000000 0 0 0 0 0 0 0 0 =0
7 6 5 4 3 2 1 0
2 2 2 2 2 2 2 2
7 6 5 4 3 2 1 0
• 11111111 1 1 1 1 1 1 1 1 = 255
27 26 25 24 23 22 21 20
Identifiers
• Series of Characters (letters, digits, underscores)
• Must NOT start with a digit (0 – 9)
• Must not be a C++ keyword
• Case Sensitive
• Exercise
• Which of these are valid identifiers
• floating
• int
• Int
• main3
• 4yi
Example (Add two numbers)
1 // Example
2 // Addition program.
3 #include <iostream>
4 using namespace std;
5 // function main begins program execution
6 void main()
7 {
8 int integer1; // first number to be input by user
9 int integer2; // second number to be input by user
10 int sum; // variable in which sum will be stored
11
12 cout << "Enter first integer:\n"; // prompt
13 cin >> integer1; // read an integer
14
15 cout << "Enter second integer:\n"; // prompt
16 cin >> integer2; // read an integer
17
18 sum = integer1 + integer2; // assign result to sum
19
20 cout << "Sum is " << sum << endl; // print sum
21
22
23
24 } // end function main
Input value
• Input Source?
• Console
• Data File
• Console Input
• cin >>
• In the header file “iostream”
Arithmetic operators
• Arithmetic calculations
•*
Multiplication
•/
Division
Integer division truncates remainder
7 / 5 evaluates to 1
•%
Modulus operator returns remainder
7 % 5 evaluates to 2
• + and –
Addition and Subtraction
Arithmetic operators
• Rules of operator precedence
Operator(s) Operation(s) Order of evaluation (precedence)
() Parentheses Evaluated first. If the parentheses are nested, the
expression in the innermost pair is evaluated
first. If there are several pairs of parentheses
“on the same level” (i.e., not nested), they are
evaluated left to right.
*, /, or % Multiplication Evaluated second. If there are several, they re
Division evaluated left to right.
Modulus
+ or - Addition Evaluated third. If there are several, they are
Subtraction evaluated left to right.
= Assignment Evaluated last, right to left
Arithmetic operators
• Priority of operators
• a = 5 + 7 % 2;
• we may doubt if it really means:
• a = 5 + (7 % 2) with result 6 or
• a = (5 + 7) % 2 with result 0
• Parentheses are included when one is not sure
Arithmetic operators
• Given integer variables a, b, c, d, and e, where a =
1, b = 2, c = 3, d = 4, Evaluate the following
expressions:
a + b - c + d
a * b / c
1 + a * b % c
a + d % b - c
e = b = d + c / b - a
Arithmetic operators
• Arithmetic Assignment Operators
• a = a + b;
• a += b; int main()
{
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
int number = 15;
number += 10; 25
cout << number << endl;
number -= 7; 18
cout << number << endl;
number *= 2; 36
cout << number << endl;
number %= 2;
0
cout << number << endl;
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
_getch();
return 0;
}
Arithmetic operators
• Increment Operators (Unary Operator)
count = count + 1;
count +=1;
count++; OR ++count;
int a = 5; int a = 5;
int b = 10; int b = 10;
int c = a * b++; int c = a * +
+b;
50 55
Arithmetic operators
postfix prefix
Arithmetic operators
void main()
{
int count = 10;
int a = 1;
int b = a++ + ++a;
cout << a;
cout << endl;
cout << b;
Relational operators
• To evaluate comparison between two expressions
• Result : True / False
Equality operators
= == x == y x is equal to y
!= x != y x is not equal to y