0% found this document useful (0 votes)
6 views39 pages

Lecture 3 Conditional Statements and Loops

Uploaded by

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

Lecture 3 Conditional Statements and Loops

Uploaded by

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

Application Development (CSF 510)

Lecture 3: Conditional Statements and Loops


Lecture Contents
• Control Structures
• Sequence Structures
• Decision Structures
• if Statement
• if else Statement
• elif Statement ( Chained Conditionals)
• Nested Conditionals
• Repetition Structures
• Loops
• for loop
• while loop

Conditional Statements and Loops 2


Decision Structures /
Conditional Statements

Conditional Statements and Loops 3


Control Structure
• A control structure controls the order in which a set of statements execute
• Sequence Structure
• All statements are executed in a sequential order
• Sequence structure cannot handle all types of real word scenarios
• Decision Structure / Conditional Statements
• Allow to execute different statements based on a condition
• If condition is True execute statement 1 otherwise execute statement 2
• Examples / Types
• if Statement
• if-else Statement
• elif Statement
• Repetition Structures / Loops
• Execute a block of code specified number of times
• for loop and while loop

Conditional Statements and Loops 4


Decision Structure : Conditional
Statements
• Conditional Statement
• A line of code that includes a Boolean expression
• Evaluates to True or False
• Includes Relational Operators

Conditional Statements and Loops 5


if Statement
• if statement is used to execute a block of code based on a Boolean expression
• Syntax:

• Things to Remember :
• if is in lower case
• There is a colon after the Boolean expression
• Body of if should be indented
• Can contain a single or multiple statements in the body

Conditional Statements and Loops 6


if Statement Example / Flow Chart

Conditional Statements and Loops 7


if else Statement
• Sometimes we need to execute a specified block of code when condition is
true and another block of code when condition is false

Conditional Statements and Loops 8


Chained Conditionals – elif
• Sometimes there are more than two possibilities .

Conditional Statements and Loops 9


Nested Conditional Statements
• An if statement or an if-else statement can be nested within another if or else
block

Conditional Statements and Loops 10


Using / Checking Multiple Conditions
• Logical Operators can be used to combine multiple conditions

Conditional Statements and Loops 11


Multiple Conditions – Examples

Conditional Statements and Loops 12


Switch Case Statement
• Languages like C , C++ , Java and Others have Switch–Case statement that can
be used as an alternative to elif statement to check multiple conditions.
• Earlier versions of python do no have any switch-case functionality
• The same can be implemented with the help of dictionaries and classes and functions
• Python version 3.10 and later have match-case functionality

Conditional Statements and Loops 13


Switch-Case functionality using
functions

Conditional Statements and Loops 14


match – case ( Syntax)

Conditional Statements and Loops 15


match-case ( Example)

Conditional Statements and Loops 16


More about Switch Case in Python
• https://www.mygreatlearning.com/blog/python-switch-case/

Conditional Statements and Loops 17


Which is more efficient ? switch case or
elif
• https://programmerbay.com/difference-between-if-statement-and-switch-sta
tement/

Conditional Statements and Loops 18


Repetition Structures / Loops

Conditional Statements and Loops 19


Repetition Structure
• A repetition structure causes a statement or group of statements to execute
repeatedly
• Two Types
• Counter Controlled Loops
• Number of iterations is known
• A Counter variable is used to count the number of iterations
• for loop is a counter-controlled loop
• Example: Print first 10 odd numbers
• Condition Controlled Loops
• Number of iterations is not known in advanced
• Before each iteration a condition is checked
• while loop is a condition-controlled loop
• For example : play a game until choice ==‘No’

Conditional Statements and Loops 20


for loop
• A counter controlled loop
• Can be used to process each element in a sequence
• Strings
• List
• Dictionaries
• Sets and Tuples
• Syntax

Conditional Statements and Loops 21


for loop – Examples

Conditional Statements and Loops 22


Nested Loops

Conditional Statements and Loops 23


Nested for Loops

Conditional Statements and Loops 24


HCF of Two Numbers

Conditional Statements and Loops 25


Print this pattern using for loops

Conditional Statements and Loops 26


while loop
• While loop is used when number of iterations is not known
• Number of iterations depends on some condition
• Syntax:

• Example

Conditional Statements and Loops 27


while Loop – Examples

Conditional Statements and Loops 28


Game: Guess the number
• Requirements
• A secret number
• Ask the user to guess the number
• Check if the number matches
• If number is greater – display “ You are too high”
• If the number is less – display “ You are too low”
• Terminate when the number matches with a congratulations message

Conditional Statements and Loops 29


Which one is better? for or while loop
• Understandability
• for loop is easy to understand and implement as compared to for loop
• Potential for infinite looping
• Less chances in for loop
• Can run into infinite iterations based on the condition
• Execution time
• Both have initialization , test condition and increment expression
• Actual execution time depends on the logic to be implemented
• Any for loop task can be implemented using a while loop but not vice versa

Conditional Statements and Loops 30


Loop Control Statements
• Loop control statements change execution from its normal sequence.

Conditional Statements and Loops 31


Continue Statement – Examples

Conditional Statements and Loops 32


Continue Statement – Examples

Conditional Statements and Loops 33


Pass Statement
• The pass statement in Python is a null statement. It does not do anything. It is
used as a placeholder for future code.
• For example, you might use the pass statement to define a function that you
have not yet implemented, or to create a loop that you will fill in later.
• The pass statement is also used to avoid errors in situations where a
statement is required syntactically, but you do not want any code to execute.
• For example, you might use the pass statement in an if statement where you
do not want to take any action if the condition is met.

Conditional Statements and Loops 34


pass Statement - Example

The pass statement is a useful tool for avoiding errors and for creating
placeholders for future code.

Conditional Statements and Loops 35


break statement-examples

Conditional Statements and Loops 36


do while loop
• There is no do while loop in Python
• The same functionality can be implemented with while True loop
• The loop will run at least once
• A break statement is included to quit the loop based on a condition

Conditional Statements and Loops 37


Python Literals
• A fixed value that is represented directly in python source code
• Literals are constants that are self-explanatory and don’t need to be
computed or evaluated.
• They are used to provide values to variable or to directly utilize them in
expressions

Conditional Statements and Loops 38


References / Resources/
Acknowledgments
• Starting out with Python , 3rd Edition
• Chapter 3 Decision Structure and Boolean Logic
• Chapter 4 Repetition Structures
• https://code4coding.com
• https://www.geeksforgeeks.org/python-while-loop/

Conditional Statements and Loops 39

You might also like