0% found this document useful (0 votes)
1 views32 pages

Programming Fundamentals Lecture 2

Introduction to Variables in Programming

Uploaded by

AdeelAkram
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views32 pages

Programming Fundamentals Lecture 2

Introduction to Variables in Programming

Uploaded by

AdeelAkram
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

UNDERSTANDING

Programming Fundamentals (CS-106) VARIABLES


What are Variables? 2

 Variable is an identifier whose value can be changed


during program execution and are used to store values
Bits & Bytes 3

 Bit is the smallest unit of information that can be


stored on a computer.
 Bit consists of either 0 or 1.
 A group of 8 bits is referred as Byte.
Building Blocks of Programming 4

 The core function of a program is to store the


data and use them in constructive ways.
 If there is no way to represent such data in a
clear fashion, then programming won’t be
possible.
Variable Declaration - Syntax 5

 Syntax
 define name as type

 Examples:
 Define age as integer
 Define balance as float
6

We will see about each data type in the next few lectures
Integer Variable 7

Integer is a type of variable whose value can only be numeric


Integer Variable 8

Depending on the range of value you intend to store


and the programming language used, you can
typically define the integer variable as:

 Short (2 bytes)
 Int (4 bytes)
 long (8 bytes)
Integer Variable - Syntax 9

Syntax:

define variable as integer


Integer Variable - Examples 10

Syntax:

define age as integer


age = 25

define TotalStudents as integer


TotalStudents = 35000
Integer Variable – Right Usage 11

define age as integer


age = 45.5
INCORRECT USAGE
define TotalStudents as integer
totalStudents = 35000

define students as integer


students = totalStudents
Float Variable 12

Float is a type of variable used to store


single-precision fraction values
13

Depending on the programming language, you can


typically define the Float variable as:

 float (4 bytes)
 double (8 bytes)
Float Variable - Syntax 14

Syntax:

define variable as float


Float Variable - Examples 15

Syntax:

define CurrentBalance as float


CurrentBalance = 2045.5

define timeTakenInHours as float


timeTakenInHours = 3.5
Float Variable – Right Usage 16

define CurrentBalance as float


CurrentBalance = 2045

define timeTakenInHours as integer


timeTakenInHours = 3.5

define currentBalance as float


CurrentBalance = 2045
Boolean Variable 17

Boolean is a type of variable whose value can be


either true or false only
Boolean Variables 18

 They can’t have any other value assigned to them.


 Boolean is also referred to as Bool in some
programming languages.
 Used mostly for conditional checks. We will cover
about this in later section.
Boolean Variable - Syntax 19

Syntax:

define variable as boolean


Boolean Variable - Examples 20

define hasSucceeded as boolean


hasSucceeded = true

define isAllowed as boolean


isAllowed = false
Boolean Variable – Right Usage 21

define hasSucceeded as integer


hasSucceeded = true

define isAllowed as boolean


isAllowed = 0

define isAllowed as boolean


isAllowed = False
Character Variable 22

Characters are a single unit of alphabets, numbers,


or symbols
Character Variables 23

 They typically take 1 byte of memory.


 They are represented within single quotes.
 For example: ‘i’,’P’,’5’,’3’,’.’
 There values are case sensitive. Hence, ‘y’ is
different than ‘Y’.
Character Variable - Syntax 24

Syntax:

define variable as character


Character Variable – Right Usage 25

define delimiter as character


delimiter = ‘,.’

define prefixCharacter as character


prefixCharacter = ‘A’
prefixCharacter = ‘B’
String Variables 26

Strings are a sequence of characters


String Variables 27

 For example, “iPhone” is a string which is basically a


collection of the characters ‘i’,’P’,’h’,’o’,’n’,’e’.
 They are one of the most versatile types in a
programming language.
 Programmers also use string variables to hold values
of other data types such as integer, float, date, etc.
String Variable - Syntax 28

Syntax:

define variable as stringe


String Variable - Examples 29

define result as string


result = “Success”

define errorMessage as string


errorMessage = “Invalid parameters”
String Variable – Right Usage 30

define result as character


result = “Success”

define result as string


result = “A”

define errorMessage as string


errorMessage = “Invalid” “parameters”
References 31

 Fundamentals of Programming Languages #2 |


Understanding Variables – YouTube
(CodeWithPraveen)
 Lecture 2:(Part 2) Algorithms and FlowCharts in
C++ | Algorithms and Flowcharts | The Kacs –
YouTube (KACS Learning)
 Lecture 3: Get started with C++ in urdu | C++ |
The Kacs – YouTube (KACS Learning)
Assignment #2 32
1. Write the following decimal numbers in 8-bit Binary format:
(https://byjus.com/maths/decimal-to-binary/)
a) 0
b) 15
c) 99
d) 129
e) 255
2. Write the following 8-bit binary numbers in decimal format:
(https://byjus.com/maths/binary-to-decimal-conversion/)
a) 00010001
b) 10000001
c) 10100101
d) 11011011
e) 11111111

Email: adeel.akram@uettaxila.edu.pk with Subject: Name/Reg - PF Assignment #2

You might also like