0% found this document useful (0 votes)
3 views

Lecture 2 Variables and Operators

The document introduces computer programming concepts, covering topics such as computer programs, software types, programming languages, and paradigms. It explains binary and decimal number systems, conversion methods, and the representation of binary numbers in memory. Additionally, it discusses the significance of bits, bytes, and their applications in programming.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Lecture 2 Variables and Operators

The document introduces computer programming concepts, covering topics such as computer programs, software types, programming languages, and paradigms. It explains binary and decimal number systems, conversion methods, and the representation of binary numbers in memory. Additionally, it discusses the significance of bits, bytes, and their applications in programming.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 57

Computer Programming

Introduction

Week 01 Sp 25

Acknowledgement – Ms. Nabia Khalid worked on the slides


Hello!
I am Dr.Arif Ur
Rahman
Office: Near Faculty Cafeteria – Cubical F
Time: Everyday after 2PM
You can reach me at arif.buic@bahria.edu.pk

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

• Software Development activities - IDEs

• 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!

1001011 = 1x20 + 1x21 + 0x22 + 1x23 +


0x24 + 0x25 + 1x26

= 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

• Each bit represents a power of 2


• Every binary number is a sum of powers of 2
• Decimal Value = (dn-1  2n-1) + ... + (d1  21) + (d0 
20)
• Binary (10011101)2 = 27 + 24 + 23 + 22 + 1 = 157
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

As a result, binary numbers are mostly written as a full Nibble (0001)


Bytes -> char in C++
8 bits form a single byte
7 6 5 4 3 2 1 0
“10011101” is One Byte of Information 1 0 0 1 1 1 0 1 = 157
27 26 25 24 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

As a result, binary numbers are mostly written as a full byte (00000001)


2 Bytes -> Short in C++
16 bits form a 2 bytes
“0001110110011101” is Two Bytes of Information
15 14 13 12 11 10 9 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
215 214 213 212 211 210 29 28 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!

• A variable is a memory address where data can be stored and changed.

• 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

• Variables are not automatically initialized. For example, after declaration


int sum;
the value of the variable sum can be anything (garbage).

• Thus, it is good practice to initialize variables when they are declared.


Once a value has been placed in a variable it stays there until the program
deliberately alters it.
Declaration!!

• Constants and variables must be declared before they can be used


• When you declare a constant or a variable, the compiler:

Reserves a memory location in which to store the value of the constant or
variable.

Associates the name of the constant or variable with the memory location.

int integer1 = 45; integer1 45


Expression or Equation in C++!
• An expression is a valid arrangement of variables, constants, and
operators.
• In C++, each expression can be evaluated to compute a value of a
given type

• In C++, an expression can be:


• A variable or a constant (count, 100)
• An operation (a + b, a * 2)
• Function call (getRectangleArea(2, 4)) coming up later in this course
Assignment Operator!
• An operator to give (assign) a value to a variable.
• Denote as ‘=‘
• Only variable can be on the left side.
• An expression is on the right side.
• Variables keep their assigned values until changed by another
assignment statement or by reading in a new value.
Assignment Operator Syntax

• Variable = Expression
• First, expression on right is evaluated.
• Then the resulting value is stored in the memory location of Variable on left.

NOTE: An automatic type correction occurs after evaluation but before


the value is stored if the types differ for Expression and Variable
Variables in C++
• Symbol represents a place to store Computer memory

information
• Name
• Value
• Memory space
• Example: somebody’s Confidence
Level
confidenceLevel 80
• An integer variable confidenceLevel;
• confidenceLevel = 80;
C++ Datatypes!

Data Type No. of Bytes No. of Bits


char 1 8
short 2 16
int 4 32
sizeof ( )
long 4 32
returns the size in bytes
float 4 32 a = sizeof (char)
double 8 64
long double 10 80
bool 1 8
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
cout << "Size of char : \t\t\t" << sizeof(char)
<< " byte" << endl;
cout << "Size of int : \t\t\t" << sizeof(int)
<< " bytes" << endl;
cout << "Size of short int : \t\t" << sizeof(short int)
<< " bytes" << endl;
cout << "Size of long int : \t\t" << sizeof(long int)
<< " bytes" << endl;
cout << "Size of signed long int : \t" << sizeof(signed long int)
<< " bytes" << endl;
cout << "Size of unsigned long int:\t " << sizeof(unsigned long int)
<< " bytes" << endl;
cout << "Size of float : \t\t" << sizeof(float)
<< " bytes" << endl;
cout << "Size of double : \t\t" << sizeof(double)
<< " bytes" << endl;
cout << "Size of long double : \t\t" << sizeof(long double)
<< " bytes" << endl;
cout << "Size of bool : \t\t\t" << sizeof(bool)
<< " bytes" << endl;
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
C++ Data types
• Integer Data Types
• Range
• Unsigned and Signed Data Types

Type Sign Size Min Value Max Value


short signed 16 bits -32768 32767
unsigned 0 65535
int signed 32 bits -2,147,483,648 2,147,483,647
unsigned 0 4,294,967,295
long signed 32 bits -2,147,483,648 2,147,483,647
unsigned 0 4,294,967,295
bool - 8 bits false/0 true/1
C++ Data types
• Floating Data Types
• Range
• Unsigned and Signed Data Types

Type Sign Size Min Value Max Value

float signed 32 bits -2,147,483,648 2,147,483,647

double signed 64 bits -9,223,372,036,854,775,808 9,223,372,036,854,775,807

long double signed 80 bits -604,462,909,807,314,587,353,088 604,462,909,807,314,587,353,087


Character Data Types

• Represent single characters


• declared as char
• Stored by ASCII values
• ASCII (American Standard Code for Information Interchange)
• 1 byte for each char

• 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

• Decrement Operators (Unary Operator)



count = count - 1;

count -=1;

count--; OR --count;

postfix prefix
Arithmetic operators
void main()
{
int count = 10;

cout << count << endl; 10


cout << ++count << endl; 11
cout << count << endl; 11
cout << count++ << endl; 11
cout << count << endl; 12
}
Postfix and Prefix !

int a = 1;
int b = a++ + ++a;

cout << a;
cout << endl;
cout << b;
Relational operators
• To evaluate comparison between two expressions
• Result : True / False

Standard algebraic C++ equality Example Meaning of


equality operator or or relational of C++ C++ condition
relational operator operator condition
Relational operators
> > x > y x is greater than y
< < x < y x is less than y

 >= x >= y x is greater than or equal to y

 <= x <= y x is less than or equal to y

Equality operators
= == x == y x is equal to y

 != x != y x is not equal to y

You might also like