Programming Basics - lms.bahria.edu.pk

50
10/25/2021 1 Department of Computer Sciences Bahria University, Islamabad CSC 113 – Computer Programming 2. Programming Basics Slide: 1 Programming Basics CSC 113 Computer Programming Spring 2021 Abrar Ahmed [email protected] Department of Computer Science Bahria University, Islamabad 2 LECTURE Department of Computer Sciences Bahria University, Islamabad CSC 113 – Computer Programming 2. Programming Basics Slide: 2 Outline Binary Number System Decimal to Binary conversion Binary Decimal conversion Representation of Binary Numbers in Memory Binary Representation of Signed Numbers Variables Variables Declarations Operators Arithmetic operators Rules of operator precedence Relational operators Equality and Assignment Operators Logical operators Truth table of logical operators Conditional operators Bitwise Operators Type conversion All Operators precedence 1 2

Transcript of Programming Basics - lms.bahria.edu.pk

Page 1: Programming Basics - lms.bahria.edu.pk

10/25/2021

1

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 1

Programming Basics

CSC 113

Computer Programming

Spring 2021

Abrar Ahmed [email protected]

Department of Computer ScienceBahria University, Islamabad

2LECTURE

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 2

Outline

• Binary Number System

• Decimal to Binary conversion

• Binary Decimal conversion

• Representation of Binary Numbers in Memory

• Binary Representation of Signed Numbers

• Variables

• Variables Declarations

• Operators

• Arithmetic operators

• Rules of operator precedence

• Relational operators

• Equality and Assignment Operators

• Logical operators

• Truth table of logical operators

• Conditional operators

• Bitwise Operators

• Type conversion

• All Operators precedence

1

2

Page 2: Programming Basics - lms.bahria.edu.pk

10/25/2021

2

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 3

Binary Number System

Binary-to-Decimal

Conversion

Decimal-to-Binary

Conversion

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 4

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

0 1 2 3 4 5 6 7 8 9

3

4

Page 3: Programming Basics - lms.bahria.edu.pk

10/25/2021

3

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 5

We count in Base 10 (Decimal)

01234567891011121314959697989910010115161718192021222324

Ran out of symbols (0-9), so increment the digit on the left by one unit.

We count in Base 10 (Decimal)

01234567891011121314959697989910010115161718192021222324

Ran out of symbols (0-9), so increment the digit on the left by one unit.

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 6

Binary (base 2) number system consists of just two

0 1

5

6

Page 4: Programming Basics - lms.bahria.edu.pk

10/25/2021

4

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 7

Computers count in Base 2 (Binary)• Counting in Binary is the same, but with only two symbols

• On (1)

• Off (0)

0110111001011111000100110101011110011011110111110000110

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 8

Counting in Decimal

0123456789

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

.

.

.

0

1

10

11

100

101

110

111

1000

1001

1010

1011

1100

1101

1110

1111

10000

10001

10010

10011

10100

10101

10110

10111

11000

11001

11010

11011

11100

11101

11110

11111

100000

100001

100010

100011

100100

.

.

.

Counting

in Binary

7

8

Page 5: Programming Basics - lms.bahria.edu.pk

10/25/2021

5

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 9

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

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

1 0 0 1 1 1 0 1

27 26 25 24 23 22 21 20

01234567

Most

Significant BitLeast

Significant Bit

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 10

• 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

1 x 4 + 0 x 2 + 1 x 1 = 5

Binary Numbers

9

10

Page 6: Programming Basics - lms.bahria.edu.pk

10/25/2021

6

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 11

Decimal Binary conversion

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 12

Convert 75 to Binary

752

37 12

18 12

9 02

4 12

2 02

1 0

1001011

remainder

11

12

Page 7: Programming Basics - lms.bahria.edu.pk

10/25/2021

7

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 13

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 14

Check

1001011 = 1x20 +1x21 + 0x22 + 1x23 +0x24 + 0x25 + 1x26

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

= 75

13

14

Page 8: Programming Basics - lms.bahria.edu.pk

10/25/2021

8

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 15

Convert 100 to Binary

1002

50 02

25 02

12 12

6 02

3 02

1 1

1100100

remainder

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 16

Dec → Binary : More Examples

a) 1310 = ?

b) 2210 = ?

c) 4310 = ?

d) 15810 = ?

15

16

Page 9: Programming Basics - lms.bahria.edu.pk

10/25/2021

9

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 17

Dec → Binary : More Examples

a) 1310 = ?

b) 2210 = ?

c) 4310 = ?

d) 15810 = ?

1 1 0 1 2

1 0 1 1 0 2

1 0 1 0 1 1 2

1 0 0 1 1 1 1 0 2

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 18

Binary Decimal conversion

17

18

Page 10: Programming Basics - lms.bahria.edu.pk

10/25/2021

10

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 19

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 =

1 0 0 1 1 1 0 1

27 26 25 24 23 22 21 20

01234567

27 + 24 + 23 + 22 + 1 = 157

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 20

Converting Binary to Decimal

1 0 1 0 1 1 0 0

1248163264128

00480320128 + + + + + + +

128 + 32 + 8 + 4 = 172

19

20

Page 11: Programming Basics - lms.bahria.edu.pk

10/25/2021

11

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 21

Converting Binary to Decimal

0 1 0 1 0 0 0 11248163264128

1000160640 + + + + + + +

64 + 16 + 1 = 81

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 22

Converting Binary to Decimal

- - - ✓ - ✓ ✓ ✓

1248163264128

124016000 + + + + + + +

16 + 4 + 2 + 1 = 23

21

22

Page 12: Programming Basics - lms.bahria.edu.pk

10/25/2021

12

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 23

Converting Binary to Decimal

☺ ☺ ☺ ☺ ☺ ☺

1248163264128

124016320128 + + + + + + +

128 + 32 + 16 + 4 + 2 + 1 = 183

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 24

Binary → Dec : More Examples

a) 0110 2 = ?

b) 11010 2 = ?

c) 0110101 2 = ?

d) 11010011 2 = ?

23

24

Page 13: Programming Basics - lms.bahria.edu.pk

10/25/2021

13

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 25

Binary → Dec : More Examples

a) 0110 2 = ?

b) 11010 2 = ?

c) 0110101 2 = ?

d) 11010011 2 = ?

6 10

26 10

53 10

211 10

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 26

Representation of Binary Numbers

in Memory

25

26

Page 14: Programming Basics - lms.bahria.edu.pk

10/25/2021

14

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 27

Nibble

• 4 bits form a Nibble

• “1011” is One Nibble of Information

• Nibble Values:

• 0000

• 1111

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

0 0 0 0

23 22 21 20

0123

= 0

1 1 0 1

23 22 21 20

0123

= 13

1 1 1 1

23 22 21 20

0123

= 15

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 28

Bytes -> char in C++

• 8 bits form a single byte

• “10011101” is One Byte of Information

• Byte Values:

• 00000000

• 11111111

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

1 0 0 1 1 1 0 1

27 26 25 24 23 22 21 20

01234567

= 157

0 0 0 0 0 0 0 0

27 26 25 24 23 22 21 20

01234567

1 1 1 1 1 1 1 1

27 26 25 24 23 22 21 20

01234567

= 0

= 255

27

28

Page 15: Programming Basics - lms.bahria.edu.pk

10/25/2021

15

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 29

2 Bytes -> Short in C++

• 16 bits form a 2 bytes

• “0001110110011101” is Two Bytes of Information

• 2 Bytes Value:

• 0000000000000000

• 1111111111111111

1 0 0 1 1 1 0 1

27 26 25 24 23 22 21 20

01234567

= 7,5810 0 0 1 1 1 0 1

215 214 213 212 211 210 29 28

89101112131415

0 0 0 0 0 0 0 0

27 26 25 24 23 22 21 20

01234567

= 00 0 0 0 0 0 0 0

215 214 213 212 211 210 29 28

89101112131415

1 1 1 1 1 1 1 1

27 26 25 24 23 22 21 20

01234567

= 65,5351 1 1 1 1 1 1 1

215 214 213 212 211 210 29 28

89101112131415

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 30

3 Bytes

• 24 bits form 3 bytes

• “000000000001110110011101” is Three Byte of Information

• 3 Byte Values:

• 000000000000000000000000

• 111111111111111111111111

1 0 0 1 1 1 0 1

27 26 25 24 23 22 21 20

01234567

= 7,5810 0 0 1 1 1 0 1

223 222 2. 2. 2. 2. 2. 28

8.....2223

0 0 0 0 0 0 0 0

27 26 25 24 23 22 21 20

01234567

= 00 0 0 0 0 0 0 0

223 222 2. 2. 2. 2. 2. 28

8.....2223

1 1 1 1 1 1 1 1

27 26 25 24 23 22 21 20

01234567

= 16,777,2161 1 1 1 1 1 1 1

223 222 2. 2. 2. 2. 2. 28

8.....2223

= 16.77 Millions

29

30

Page 16: Programming Basics - lms.bahria.edu.pk

10/25/2021

16

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 31

4 Bytes -> int and long in C++

• 32 bits form 4 bytes

• “000000000001110110011101” is Four Byte of Information

• 4 Byte Values:

• 00000000000000000000000000000000

• 11111111111111111111111111111111

1 0 0 1 1 1 0 1

27 26 25 24 23 22 21 20

01234567

= 7,5810 0 0 1 1 1 0 1

231 230 2. 2. 2. 2. 2. 28

8.....3031

0 0 0 0 0 0 0 0

27 26 25 24 23 22 21 20

01234567

= 00 0 0 0 0 0 0 0

231 230 2. 2. 2. 2. 2. 28

8.....3031

1 1 1 1 1 1 1 1

27 26 25 24 23 22 21 20

01234567

= 4,294,967,2961 1 1 1 1 1 1 1

231 230 2. 2. 2. 2. 2. 28

8.....3031

= 4.29 Billions

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 32

8 Bytes -> Double in C++

• 64 bits form 8 bytes

• “00000……….0000001110110011101” is 8 Byte of Information

• 4 Byte Values:

• 000000000000000…………00000000000000000

• 11111111111111…………111111111111111111

1 0 0 1 1 1 0 1

27 26 25 24 23 22 21 20

01234567

= 7,5810 0 0 1 1 1 0 1

231 230 2. 2. 2. 2. 2. 28

8.....3031

0 0 0 0 0 0 0 0

27 26 25 24 23 22 21 20

01234567

= 00 0 0 0 0 0 0 0

263 262 2. 2. 2. 2. 2. 28

8.....6263

1 1 1 1 1 1 1 1

27 26 25 24 23 22 21 20

01234567

= 18,446,744,073,709,551,6161 1 1 1 1 1 1 1

263 262 2. 2. 2. 2. 2. 28

8.....6263

= ?

31

32

Page 17: Programming Basics - lms.bahria.edu.pk

10/25/2021

17

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 33

Binary Representation of Signed Numbers

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 34

Signed Binary Representation• MSB: if 0, positive; if 1, negative

• Minimum value

• Maximum value

1 0 0 1 1 1 0 1

26 25 24 23 22 21 20

0123456

= -290 0 0 1 1 1 0 1

26 25 24 23 22 21 20

0123456

= 29

1 1 1 1 1 1 1 1

26 25 24 23 22 21 20

0123456

= -128

0 1 1 1 1 1 1 1

26 25 24 23 22 21 20

0123456

= +127

33

34

Page 18: Programming Basics - lms.bahria.edu.pk

10/25/2021

18

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 35

Fixed Part of the Program

//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

confusion level out of 100

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++ Example

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 36

Data Type

Memory is Requested from

//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

confusion level out of 100

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++

35

36

Page 19: Programming Basics - lms.bahria.edu.pk

10/25/2021

19

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 37

Variables

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 38

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.

37

38

Page 20: Programming Basics - lms.bahria.edu.pk

10/25/2021

20

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 39

Declaration

• Declarations

• Variable Declarations

• Constant Declarations

• Variable Declarations

• Syntax

<type> <identifier> = <expression>;

• Exampleint confidenceLevel = 100;

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 40

Declaration

• Constant Declarations

• Syntax

const <type> <identifier> = <expression>;

• Example• const double PI = 3.1459;

39

40

Page 21: Programming Basics - lms.bahria.edu.pk

10/25/2021

21

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 41

Declaration

• Variable Declarations

• Variables are used to store values that can be changed during the program execution

• Syntax:

• Examples:

< type > < identifier >;

< type > < identifier > = < expression>;

int sum;int total = 3445;char answer = 'y';double temperature = -3.14

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 42

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.

41

42

Page 22: Programming Basics - lms.bahria.edu.pk

10/25/2021

22

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 43

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.

integer1 45int integer1 = 45;

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 44

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;

43

44

Page 23: Programming Basics - lms.bahria.edu.pk

10/25/2021

23

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 45

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

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 46

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.

45

46

Page 24: Programming Basics - lms.bahria.edu.pk

10/25/2021

24

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 47

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

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 48

Variables in C++

• Symbol represents a place to store information

• Name

• Value

• Memory space

• Example: somebody’s Confidence Level

• An integer variable confidenceLevel;

• confidenceLevel = 80;

Computer memory

confidenceLevel 80

47

48

Page 25: Programming Basics - lms.bahria.edu.pk

10/25/2021

25

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 49

C++ Data Types

➢Integer Data types

✓ char✓ short✓ int✓ long✓bool

➢Floating Point Data types

✓ float✓ double✓ long double

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 50

C++ Data types

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

sizeof ( )

returns the size in bytesa = sizeof (char)

49

50

Page 26: Programming Basics - lms.bahria.edu.pk

10/25/2021

26

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 51

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

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;

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

Back to C++

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 52

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

51

52

Page 27: Programming Basics - lms.bahria.edu.pk

10/25/2021

27

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 53

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

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 54

53

54

Page 28: Programming Basics - lms.bahria.edu.pk

10/25/2021

28

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 55

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:

• 00000000

• 11111111

0 0 0 0 0 0 0 0

27 26 25 24 23 22 21 20

01234567

1 1 1 1 1 1 1 1

27 26 25 24 23 22 21 20

01234567

= 0

= 255

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 56

55

56

Page 29: Programming Basics - lms.bahria.edu.pk

10/25/2021

29

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 57

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

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 58

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

57

58

Page 30: Programming Basics - lms.bahria.edu.pk

10/25/2021

30

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 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

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 60

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.

59

60

Page 31: Programming Basics - lms.bahria.edu.pk

10/25/2021

31

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 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.

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 62

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.

61

62

Page 32: Programming Basics - lms.bahria.edu.pk

10/25/2021

32

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 63

Input value

• Input Source?

• Console

• Data File

• Console Input

• cin >>

• In the header file “iostream”

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 64

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

63

64

Page 33: Programming Basics - lms.bahria.edu.pk

10/25/2021

33

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 65

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 Division Modulus

Evaluated second. If there are several, they re evaluated left to right.

+ or - Addition Subtraction

Evaluated third. If there are several, they are evaluated left to right.

= Assignment Evaluated last, right to left

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 66

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

65

66

Page 34: Programming Basics - lms.bahria.edu.pk

10/25/2021

34

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 67

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

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 68

Arithmetic operators

• Arithmetic Assignment Operators

• a = a + b;

• a += b;

25

18

36

0

int main()

{

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

int number = 15;number += 10;cout << number << endl;number -= 7;cout << number << endl;number *= 2;cout << number << endl;number %= 2;cout << number << endl;

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

_getch();

return 0;

}

67

68

Page 35: Programming Basics - lms.bahria.edu.pk

10/25/2021

35

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 69

Arithmetic operators

• Increment Operators (Unary Operator)

◼ count = count + 1;

◼ count +=1;

◼ count++; OR ++count;

int a = 5;

int b = 10;

int c = a * b++;

int a = 5;

int b = 10;

int c = a * ++b;

50 55

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 70

Arithmetic operators

• Decrement Operators (Unary Operator)

◼ count = count - 1;

◼ count -=1;

◼ count--; OR --count;

postfix prefix

69

70

Page 36: Programming Basics - lms.bahria.edu.pk

10/25/2021

36

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 71

Arithmetic operators

1011111112

void main(){

int count = 10;

cout << count << endl;cout << ++count << endl;cout << count << endl;cout << count++ << endl;cout << count << endl;

}

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 72

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

cout << a;cout << endl;cout << b;

71

72

Page 37: Programming Basics - lms.bahria.edu.pk

10/25/2021

37

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 73

Relational operators• To evaluate comparison between two expressions

• Result : True / False

Standard algebraic equality operator or relational operator

C++ equality or relational operator

Example of C++ condition

Meaning of C++ 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

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 74

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

73

74

Page 38: Programming Basics - lms.bahria.edu.pk

10/25/2021

38

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 75

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

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 76

Equality (==) and Assg (=) Operators

• Common error : Does not cause syntax errors

• Example

• If == was replaced with =

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

• Statement is true (since 4 is non-zero)

• Bonus given in every case

if ( payCode == 4 )

cout << "You get a bonus!“;

if ( payCode = 4 )

cout << "You get a bonus!“;

75

76

Page 39: Programming Basics - lms.bahria.edu.pk

10/25/2021

39

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 77

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)

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 78

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 ).

77

78

Page 40: Programming Basics - lms.bahria.edu.pk

10/25/2021

40

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 79

Truth table (AND)

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 80

Truth table (OR)

79

80

Page 41: Programming Basics - lms.bahria.edu.pk

10/25/2021

41

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 81

Truth table (NOT)

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 82

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.

81

82

Page 42: Programming Basics - lms.bahria.edu.pk

10/25/2021

42

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 83

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;

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 84

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

83

84

Page 43: Programming Basics - lms.bahria.edu.pk

10/25/2021

43

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 85

0 0 1 0 1 0

1 0 1 0 0 0

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 86

XOR

0

85

86

Page 44: Programming Basics - lms.bahria.edu.pk

10/25/2021

44

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 87

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;

}

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 88

Type conversion

Casting

void main(void){

short number = 30000;//-32768 to 32767short result = (number * 10) / 10;cout << "Result is : " << result;//Result Incorrectnumber = 30000;result = (long(number) * 10) / 10; //Castingcout << "Result is : " << result;

}

87

88

Page 45: Programming Basics - lms.bahria.edu.pk

10/25/2021

45

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 89

Operator precedence

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 90

Example

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

• 2+2 = 4

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

89

90

Page 46: Programming Basics - lms.bahria.edu.pk

10/25/2021

46

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 91

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. (?)

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 92

Arithmetic Expression Evaluation

• Evaluate

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

• What is your answer?

91

92

Page 47: Programming Basics - lms.bahria.edu.pk

10/25/2021

47

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 93

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 94

Activity 2

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

93

94

Page 48: Programming Basics - lms.bahria.edu.pk

10/25/2021

48

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 95

Activity 3

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

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 96

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;

95

96

Page 49: Programming Basics - lms.bahria.edu.pk

10/25/2021

49

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 97

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

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 98

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;

97

98

Page 50: Programming Basics - lms.bahria.edu.pk

10/25/2021

50

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 99

Output

6

6

8

2

6

6

Department of Computer SciencesBahria University, Islamabad

CSC 113 – Computer Programming2. Programming Basics

Slide: 100

99

100