You are on page 1of 50

2/28/2023

LECTURE

2 Programming Basics
CSC 113
Computer Programming
Spring 2023

Abrar Ahmed
abrarahmed.buic@bahria.edu.pk

Department of Computer Science


Bahria University, Islamabad

CSC 113 – Computer Programming


Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 1

Outline

• Binary Number System • Rules of operator precedence


• Decimal to Binary conversion • Relational operators
• Binary Decimal conversion • Equality and Assignment Operators
• Representation of Binary Numbers in Memory • Logical operators
• Binary Representation of Signed Numbers • Truth table of logical operators
• Variables • Conditional operators
• Variables Declarations • Bitwise Operators
• Operators • Type conversion
• Arithmetic operators • All Operators precedence

CSC 113 – Computer Programming


Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 2

1
2/28/2023

Binary Number System

Decimal-to-Binary
Conversion

Binary-to-Decimal
Conversion

CSC 113 – Computer Programming


Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 3

Decimal (base 10) number system consists of 10 symbols or digits

0 1 2 3 4
5 6 7 8 9
CSC 113 – Computer Programming
Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 4

2
2/28/2023

We count in Base 10 (Decimal)

We count in Base 10 (Decimal)

100
101
19
18
17
16
15
99
98
97
96
95
14
13
12
11
10
24
23
22
21
20
9
8
7
6
5
4
3
2
1
0
Ran out of symbols (0-9), so increment the digit on the left by one unit.

CSC 113 – Computer Programming


Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 5

Binary (base 2) number system consists of just two

01
CSC 113 – Computer Programming
Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 6

3
2/28/2023

Computers count in Base 2 (Binary)


• Counting in Binary is the same, but with only two symbols
• On (1)
• Off (0)

10000
1111
1110
1101
1100
1011
1010
1001
1000
111
101
100
110
11
10
1
0
CSC 113 – Computer Programming
Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 7

Counting in Decimal Counting


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
4 13 23 33 11 1101 10111 100001
5 14 24 34 100 1110 11000 100010
6 15 25 35 101 1111 11001 100011
7 16 26 36 110 10000 11010 100100
8 17 27 . 111 10001 11011 .
9
18 28 . 1000 10010 11100 .
CSC 113 – Computer Programming
19
Department of Computer Sciences
Bahria University, Islamabad
29 . 1001 10011 11101 . 2. Programming Basics
Slide: 8

4
2/28/2023

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
7 6 5 4 3 2 1 0
• Images, sound, etc.
1 0 0 1 1 1 0 1

• Bit Numbering 2 7 6
2 5
2 2 4 3
2 2 2
2 1
20

• Least significant bit (LSB) is rightmost (bit 0)


• Most significant bit (MSB) is leftmost (bit 7 in an 8-bit number)

CSC 113 – Computer Programming


Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 9

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

CSC 113 – Computer Programming


Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 10

10

5
2/28/2023

Decimal Binary
conversion

CSC 113 – Computer Programming


Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 11

11

Convert 75 to Binary

2 75 remainder
2 37 1
2 18 1
2 9 0
2 4 1
2 2 0
1 0

Department of Computer Sciences


1001011 CSC 113 – Computer Programming
2. Programming Basics
Bahria University, Islamabad
Slide: 12

12

6
2/28/2023

CSC 113 – Computer Programming


Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 13

13

Check

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


0x24 + 0x25 + 1x26

= 1 + 2 + 0 + 8 + 0 + 0 + 64

= 75

CSC 113 – Computer Programming


Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 14

14

7
2/28/2023

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 CSC 113 – Computer Programming


Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 15

15

Dec → Binary : More Examples

a) 1310 = ?

b) 2210 = ?

c) 4310 = ?

d) 15810 = ?

CSC 113 – Computer Programming


Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 16

16

8
2/28/2023

Dec → Binary : More Examples

a) 1310 = ? 11012

b) 2210 = ? 101102

c) 4310 = ? 1010112

d) 15810 = ? 100111102

CSC 113 – Computer Programming


Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 17

17

Binary Decimal
conversion

CSC 113 – Computer Programming


Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 18

18

9
2/28/2023

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

7 6 5 4 3 2 1 0
1 0 0 1 1 1 0 1
7 6 5 4 3 2 1
2 2 2 2 2 2 2 20

CSC 113 – Computer Programming


Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 19

19

Converting Binary to Decimal

1 0 1 0 1 1 0 0
128 64 32 16 8 4 2 1

128 + 0 + 32 + 0 + 8 + 4 + 0 + 0
128 + 32 + 8 + 4 = 172
CSC 113 – Computer Programming
Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 20

20

10
2/28/2023

Converting Binary to Decimal

0 1 0 1 0 0 0 1
128 64 32 16 8 4 2 1

0 + 64 + 0 + 16 + 0 + 0 + 0 + 1

64 + 16 + 1 = 81
CSC 113 – Computer Programming
Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 21

21

Converting Binary to Decimal

- - - ✓ - ✓ ✓ ✓
128 64 32 16 8 4 2 1

0 + 0 + 0 + 16 + 0 + 4 + 2 + 1

16 + 4 + 2 + 1 = 23
CSC 113 – Computer Programming
Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 22

22

11
2/28/2023

Converting Binary to Decimal

☺  ☺ ☺  ☺ ☺ ☺
128 64 32 16 8 4 2 1

128 + 0 + 32 + 16 + 0 + 4 + 2 + 1

128 + 32 + 16 + 4 + 2 + 1 = 183
CSC 113 – Computer Programming
Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 23

23

Binary → Dec : More Examples

a) 0110 2 = ?

b) 11010 2 = ?

c) 0110101 2 = ?

d) 11010011 2 = ?

CSC 113 – Computer Programming


Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 24

24

12
2/28/2023

Binary → Dec : More Examples

a) 0110 2 = ? 6 10

b) 11010 2 = ? 26 10

c) 0110101 2 = ? 53 10

d) 11010011 2 = ? 211 10

CSC 113 – Computer Programming


Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 25

25

Representation of
Binary Numbers
in Memory
CSC 113 – Computer Programming
Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 26

26

13
2/28/2023

Nibble
• 4 bits form a Nibble
3 2 1 0
• “1011” is One Nibble of Information 1 1 0 1 = 13
23 22 21 20

• Nibble Values: 3 2 1 0

• 0000 0 0 0 0 =0
3 2 1
2 2 2 20

3 2 1 0

• 1111 1 1 1 1 = 15
23 22 21 20

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

CSC 113 – Computer Programming


Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 27

27

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


7 6 5 4 3 2 1
2 2 2 2 2 2 2 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

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

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

CSC 113 – Computer Programming


Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 28

28

14
2/28/2023

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: 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0

• 0000000000000000 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 =0
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 20

15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0

• 1111111111111111 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 = 65,535
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 20

CSC 113 – Computer Programming


Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 29

29

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
23 22 . . . . . 8 7 6 5 4 3 2 1
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 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
CSC 113 – Computer Programming
Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 30

30

15
2/28/2023

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
231 230 2. 2. 2. 2. 2. 28 27 26 25 24 23 22 21 20
• 4 Byte Values:
• 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
CSC 113 – Computer Programming
Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 31

31

8 Bytes -> Double in C++


• 64 bits form 8 bytes
• “00000……….0000001110110011101” is 8 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
31 30 . . . . . 8 7 6 5 4 3 2 1
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 20
• 4 Byte Values:
• 000000000000000…………00000000000000000
63 62 . . . . . 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
63 62 . . . . . 8 7 6 5 4 3 2 1
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 20

• 11111111111111…………111111111111111111
63 62 . . . . . 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 = 18,446,744,073,709,551,616
263 262 2. 2. 2. 2. 2. 28 27 26 25 24 23 22 21 20
=?
CSC 113 – Computer Programming
Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 32

32

16
2/28/2023

Binary
Representation of
Signed Numbers
CSC 113 – Computer Programming
Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 33

33

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
6 5 4 3 2 1 0 6 5 4 3 2 1
2 2 2 2 2 2 2 2 2 2 2 2 2 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
6 5 4 3 2 1
2 2 2 2 2 2 20

CSC 113 – Computer Programming


Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 34

34

17
2/28/2023

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;
}
CSC 113 – Computer Programming
Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 35

35

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 from
cin >> confusionLevel;
cout << "Dont worry, you are " << 100 - confusionLevel << "% confident";

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

_getch();
return 0;
}
CSC 113 – Computer Programming
Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 36

36

18
2/28/2023

Variables

CSC 113 – Computer Programming


Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 37

37

What is 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.

CSC 113 – Computer Programming


Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 38

38

19
2/28/2023

Declaration
• Declarations
• Variable Declarations
• Constant Declarations

• Variable Declarations
• Syntax
<type> <identifier> = <expression>;
• Example
int confidenceLevel = 100;

CSC 113 – Computer Programming


Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 39

39

Declaration
• Constant Declarations
• Syntax
const <type> <identifier> = <expression>;

• Example
• const double PI = 3.1459;

CSC 113 – Computer Programming


Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 40

40

20
2/28/2023

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

CSC 113 – Computer Programming


Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 41

41

Declarations
• 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.

CSC 113 – Computer Programming


Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 42

42

21
2/28/2023

Declarations

• 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

CSC 113 – Computer Programming


Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 43

43

Variable Declaration
• All variables must declared before use.
• At the top of the program
• Just before use.
• Commas are used to separate identifiers of the same type.
int count, age;
or
int count;
int age;
• Variables can be initialized to a starting value when they are declared
int count = 0;
int age;

CSC 113 – Computer Programming


Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 44

44

22
2/28/2023

What is an Expression 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

CSC 113 – Computer Programming


Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 45

45

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.

CSC 113 – Computer Programming


Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 46

46

23
2/28/2023

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 coercion occurs after evaluation but before the value is stored if the types differ for
Expression and Variable

CSC 113 – Computer Programming


Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 47

47

Variables in C++

• Symbol represents a place to store information Computer memory


• Name
• Value
• Memory space
• Example: somebody’s Confidence Level
• An integer variable confidenceLevel;
• confidenceLevel = 80;
confidenceLevel 80

CSC 113 – Computer Programming


Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 48

48

24
2/28/2023

C++ Data Types

➢Integer Data types


✓ char
✓ short
✓ int
✓ long
✓bool
➢Floating Point Data types
✓ float
✓ double
✓ long double

CSC 113 – Computer Programming


Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 49

49

C++ Data types sizeof ( )


returns the size in bytes
a = sizeof (char)

Data Type No. of Bytes No. of Bits


char 1 8
short 2 16
int 4 32
long 4 32
float 4 32
double 8 64
long double 10 80
bool 1 8

CSC 113 – Computer Programming


Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 50

50

25
2/28/2023

Back to C++
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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;
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

CSC 113 – Computer Programming


Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 51

51

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

CSC 113 – Computer Programming


Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 52

52

26
2/28/2023

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

CSC 113 – Computer Programming


Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 53

53

CSC 113 – Computer Programming


Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 54

54

27
2/28/2023

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

7 6 5 4 3 2 1 0
• Byte Values:
0 0 0 0 0 0 0 0 =0
• 00000000 27 26 25 24 23 22 21 20

7 6 5 4 3 2 1 0
1 1 1 1 1 1 1 1 = 255
• 11111111
27 26 25 24 23 22 21 20

CSC 113 – Computer Programming


Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 55

55

CSC 113 – Computer Programming


Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 56

56

28
2/28/2023

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

CSC 113 – Computer Programming


Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 57

57

Lvalues and Rvalues

• Lvalues
• Expressions that can appear on left side of equation
• Can be changed (i.e., variables)
• x = 4;
• Rvalues
• Only appear on right side of equation
• Constants, such as numbers
• Cannot write 4 = x;

Lvalues can be used as Rvalues, but not vice


versa

CSC 113 – Computer Programming


Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 58

58

29
2/28/2023

Input value
• Input Source?
• Console
• Data File
• Console Input
• cin >>
• In the header file “iostream”

CSC 113 – Computer Programming


Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 59

59

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
CSC 113 – Computer Programming
Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 60

60

30
2/28/2023

Good programming tips

Some programmers prefer to declare each variable on a


separate line. This format allows for easy insertion of a
descriptive comment next to each declaration.

CSC 113 – Computer Programming


Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 61

61

Good programming tips

Choosing meaningful identifiers helps make a program self-


documenting—a person can understand the program simply
by reading it rather than having to refer to manuals or
comments.

Avoid using abbreviations in identifiers. This promotes


program readability.

CSC 113 – Computer Programming


Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 62

62

31
2/28/2023

Portability Tip

C++ allows identifiers of any length, but your C++


implementation may impose some restrictions on the length
of identifiers. Use identifiers of 31 characters or fewer to
ensure portability.

CSC 113 – Computer Programming


Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 63

63

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

CSC 113 – Computer Programming


Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 64

64

32
2/28/2023

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

CSC 113 – Computer Programming


Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 65

65

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

CSC 113 – Computer Programming


Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 66

66

33
2/28/2023

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

CSC 113 – Computer Programming


Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 67

67

Arithmetic operators
• Arithmetic Assignment Operators
• a = a + b;
• a += b;
int main()
{
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
int number = 15;
number += 10;
cout << number << endl;
number -= 7; 25
cout << number << endl;
number *= 2; 18
cout << number << endl;
number %= 2; 36
cout << number << endl;
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0
_getch();
return 0;
}

CSC 113 – Computer Programming


Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 68

68

34
2/28/2023

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

CSC 113 – Computer Programming


Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 69

69

Arithmetic operators
• Decrement Operators (Unary Operator)
◼ count = count - 1;
◼ count -=1;
◼ count--; OR --count;

postfix prefix

CSC 113 – Computer Programming


Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 70

70

35
2/28/2023

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
}

CSC 113 – Computer Programming


Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 71

71

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

cout << a;
cout << endl;
cout << b;
CSC 113 – Computer Programming
Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 72

72

36
2/28/2023

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

CSC 113 – Computer Programming


Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 73

73

Relational operators
• Examples
▪ (7 == 5) would return false
▪ (3 != 2) would return true
▪ (6 >= 6) would return true
• If a=2, b=3 and c=6
▪ (a*b >= c) would return true since it is (2*3 >= 6)
▪ (b+4 > a*c) would return false since it is (3+4 > 2*6)
▪ ((b=2) == a) would return true

CSC 113 – Computer Programming


Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 74

74

37
2/28/2023

Relational operators - characters


• C++ allows character comparison using relational and equality operators.

• During comparison alphabetical order is followed. (ASCII: American Standard Code for Information
Interchange ).

• Examples
• ‘a’ < ‘e’ // True

CSC 113 – Computer Programming


Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 75

75

Equality (==) and Assg (=) Operators


• Common error : Does not cause syntax errors
• Example
if ( payCode == 4 )
cout << "You get a bonus!“;

• If == was replaced with =


if ( payCode = 4 )
cout << "You get a bonus!“;

• PayCode set to 4 (no matter what it was before)


• Statement is true (since 4 is non-zero)
• Bonus given in every case

CSC 113 – Computer Programming


Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 76

76

38
2/28/2023

Logical operators
• Logical expressions - expressions that use conditional statements and logical operators.
• && (And)
• A && B is true if and only if both A and B are true
• || (Or)
• A || B is true if either A or B are true
• ! (Not)
• !(condition) is true if condition is false, and false if condition is true
• This is called the logical complement or negation
• Examples
• (salary < 10000) || (dependants > 5)
• (temperature > 40.0) && (humidity > 90)
• !(temperature > 90.0)

CSC 113 – Computer Programming


Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 77

77

Logical operators - Exercises

• NOT, AND, OR : ( !, &&, || )


• Operator ! is equivalent to Boolean operation NOT
• ! (5 == 5) returns false
• ! (6 <= 4) returns true
• ! true returns false.
• ! false returns true.

• ((5 == 5) && (3 > 6)) returns false (true && false)


• ((5 == 5) || (3 > 6)) returns true ( true || false ).

CSC 113 – Computer Programming


Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 78

78

39
2/28/2023

Truth table (AND)

CSC 113 – Computer Programming


Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 79

79

Truth table (OR)

CSC 113 – Computer Programming


Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 80

80

40
2/28/2023

Truth table (NOT)

CSC 113 – Computer Programming


Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 81

81

Remember

• && operator yields a true result only when both its operands are true.

• || operator yields a false result only when both its operands are false.

CSC 113 – Computer Programming


Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 82

82

41
2/28/2023

Conditional operators
• condition ? result1 : result2
▪ if condition is true the expression will return result1, if not it will return result2
• Examples
▪ 7==5 ? 4 : 3 returns 3 since 7 is not equal to 5.
▪ 7==5+2 ? 4 : 3 returns 4 since 7 is equal to 5+2
▪ a>b ? a : b returns the greater one, a or b

▪int res = a>b ? a : b;

CSC 113 – Computer Programming


Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 83

83

Bitwise Operators
• Bitwise Operators ( &, |, ^, ~, <<, >> )

& AND Logical AND


| OR Logical OR
^ XOR Logical exclusive OR
~ NOT Complement to one (bit inversion)
<< SHL Shift Left
>> SHR Shift Right

CSC 113 – Computer Programming


Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 84

84

42
2/28/2023

0 0 1 0 1 0
1 0 1 0 0 0

CSC 113 – Computer Programming


Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 85

85

XOR

CSC 113 – Computer Programming


Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 86

86

43
2/28/2023

Type conversion

• Automatic Type Conversion


• Casting
Automatic Type Conversion

void main(void)
{
int number = 2;
float factor = 1.5;
double result = number * factor;
cout << "Result is : " << result;
}

void main(void)
{
short x = 2;
int y = x;
}

CSC 113 – Computer Programming


Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 87

87

Type conversion

Casting

void main(void)
{
short number = 30000;//-32768 to 32767
short result = (number * 10) / 10;
cout << "Result is : " << result;//Result Incorrect
number = 30000;
result = (long(number) * 10) / 10; //Casting
cout << "Result is : " << result;
}

CSC 113 – Computer Programming


Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 88

88

44
2/28/2023

Operator precedence

CSC 113 – Computer Programming


Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 89

89

Example

int x = 1, y = 2;
int result = y++ + ++x;
cout << result << endl;
cout << x << endl;
cout << y << endl;

• Unary operator has higher precedence and is evaluated right to left


• x gets the value 2.
• y++ is postfix form so y is incremented after the execution of this
statement
• 2+2 = 4

CSC 113 – Computer Programming


Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 90

90

45
2/28/2023

Escape Sequences
1. \n (New line) – We use it to shift the cursor control to the new line
2. \t (Horizontal tab) – We use it to shift the cursor to a couple of spaces to the right in the same line.
3. \a (Audible bell) – A beep is generated indicating the execution of the program to alert the user.
4. \r (Carriage Return) – We use it to position the cursor to the beginning of the current line.
5. \\ (Backslash) – We use it to display the backslash character.
6. \’ (Apostrophe or single quotation mark) – We use it to display the single-quotation mark.
7. \” (Double quotation mark)- We use it to display the double-quotation mark.
8. \0 (Null character) – We use it to represent the termination of the string.
9. \? (Question mark) – We use it to display the question mark. (?)

CSC 113 – Computer Programming


Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 91

91

Arithmetic Expression Evaluation

• Evaluate
• Y = 2*5*5+3*5+7;

• What is your answer?

CSC 113 – Computer Programming


Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 92

92

46
2/28/2023

CSC 113 – Computer Programming


Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 93

93

Activity 2

• Input three integer values from keyboard into variables a, b and c.

CSC 113 – Computer Programming


Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 94

94

47
2/28/2023

Activity 3

• Assign the product of variables b and c to a as entered by the user.

CSC 113 – Computer Programming


Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 95

95

Activity 4

What prints when each of the following c++ statement is executed? Assume
x=2 and y=3.

1. cout<<x;

2. cout<<x+x;

3. cout<<“x=“;

4. cout<<“x=“<<x;

5. cout<<x+y<<“=“<<y+x;

CSC 113 – Computer Programming


Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 96

96

48
2/28/2023

Activity 5
• Write a program that calculates the square of a number entered by the user and displays output as below.
Value Square
4 16

CSC 113 – Computer Programming


Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 97

97

Activity 6
• What would be the output
int i=6, j=2;
cout<<i<<endl;
cout<<i++<<endl;
cout<<++i<<endl;
cout<<j++<<endl;
cout<<++i-j<<endl;
cout<<i-j--<<endl;

CSC 113 – Computer Programming


Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 98

98

49
2/28/2023

Output
6
6
8
2
6
6

CSC 113 – Computer Programming


Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 99

99

CSC 113 – Computer Programming


Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 100

100

50

You might also like