[go: up one dir, main page]

0% found this document useful (0 votes)
5 views33 pages

Programming Fundamentals Lecture 4

Conditional Statements

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)
5 views33 pages

Programming Fundamentals Lecture 4

Conditional Statements

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

UNDERSTANDING

CONDITIONAL
Programming Fundamentals (CS-106) STATEMENTS
Overview of Program Flow 2

A Program Flow refers to the sequence of execution of


instructions in a program
What is Program Flow? 3

Block 1 Block 3 Block 2


What is Program Flow? 4

Block 1

Block 2

Block 3
Why Program Flow constructs are required? 5

Block 1
Block 1 Block 1

Block 2

Block 2 Block 2

Block 2

Block 3 Block 3
Block 3
6

Two broad categories of program flow constructs:


Conditional Statements & Loops
Conditional Statements 7

Conditional Statements instructs the compiler to perform


different action based on a Boolean condition
Conditional Statements – Program Flow 8

Block 1

true

false Block 2

Block 3
Conditional Statements 9

 Most power language feature.

 Very simple to use. We can write entire program just with


conditional statements!

 They are also referred to as just “conditions”.


Conditional Statements 10

Type of Conditional Statements:


 if
 if…else
 nested if…else
 switch case
11

if statement is a conditional statement for one way selection


if statement – Program Flow 12

if condition

true

false Block 1

Block 2
if Statement - Syntax 13

Syntax:

if condition then
statement to execute
end if
if Statement – Example 1 14

if iPhoneModel == 10 then
print “I got iPhone 10”
end if

print “I will get executed anyways”


if Statement – Example 2 15

if iPhoneModel == 10 || iPhoneModel == 11 then


print “I got a latest iPhone”
end if
if Statement 16

Note:

The word ’if’ is a Keyword, hence it can not be used as a


variable name.
if-else Statements 17

if else statements are conditional statements for two-way


selection
if-else Statements – Program Flow 18
if condition
true

Block 1

else
false

Block 2

Block 3
if-else Statements - Syntax 19

Syntax:

if condition then
success statement to execute
else
failure statement to execute
end if
if-else Statements - Example 20

if iPhoneModel == 11 then
print “I got iPhone 11”
else
print “I got another model of iPhone”
end if
if-else Statements 21

Note:

The word ’else’ is a Keyword, hence it can not be used as a


variable name.
Nested if-else Statements 22

Nested if-else statements are conditional statements for


multiple way selection
Nested if-else Statements – Program Flow 23
if condition1
true

Block 1

else if condition2
true

Block 2

else

Block n
Nested if-else Statements - Syntax 24

Syntax 1:

if condition1 then
success statement to execute
else if condition2 then
different success statement to execute
else
failure statement to execute
end if
Nested if-else Statements – Example1 25

if iPhoneModel == 10 then
print “I got iPhone 10”
else if iPhoneModel == 11 then
print “I got iPhone 11”
else
print “I got an older iPhone”
end if
Nested if-else Statements - Syntax 26

Syntax 2:

if condition1 then
success statement to execute
if statement block

else
failure statement to execute
end if
Nested if-else Statements – Example2 27

if model == “iPhone” then


if iPhoneModel == 11 then
print “I got iPhone 11”
endif
else
print “I got an Android Phone”
endif
Switch Statements 28

Switch statements are conditional statements for multiple


way selection
Switch Statements – Program Flow 29
switch variable
case value1:

Block 1

case value2:

Block 2


default:

Block n
Switch Statements - Syntax 30

switch variable
case value1:
success statements 1 to execute
case value2:
success statements 2 to execute
default:
default statements to execute
end if
Switch Statements - Examples 31

switch iPhoneModel
case 10:
print “I got iPhone 10”
case 11:
print “I got iPhone 11”
default:
print “I got an older iPhone”
References 32

 Fundamentals of Programming Languages #4 |


Understanding Conditional Statements – YouTube
(CodeWithPraveen)
 Lecture 9 (Part-1): Decision making "If Statement in C++" in
Urdu | Programming Fundamentals – YouTube
(KACS Learning)
 Lecture 9: (Part -2) if-else statements in C++ in Urdu - YouTube
(KACS Learning)
 Lecture 9: (Part 3) Nested-if in C++ in Urdu | Programming
Fundamentals C++ |YouTube (KACS Learning)
 Lecture 9: (Part-4) if else-if ladder in C++ in Urdu |
Programming fundamentals | YouTube(KACS Learning)
Assignment #4 33

 In Dev C++ create 3 separate programs that use


if-then-else, nested if-then-elseif and switch-case
statements (as given in examples) in this lecture:
1. Share source code of your programs
2. Draw Flowchart of your programs
3. Share output of your programs

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

You might also like