A 05 Conditional Statements and Loops
A 05 Conditional Statements and Loops
ABC is an e-commerce organization that deals with the listing of online products
based on their purchase, reviews, and availability. The users filter these products
based on multiple factors. The products are listed only if it matches a few criteria,
else an error is displayed.
The organization is supposed to develop this module with the help of control
structure, implement range functions to filter based on the ranges, and
implement looping control statements.
Start
Condition
Expression
Statement in Statement in
ifAction/ta
branch else branch
sk
Stop
Decision Control Structures: Scenario
Start
Rain
Rains
● Based on weather conditions, I can choose Condition tomorrow
my activities throughout the day. morning?
Stop
Nested if Statements
No
Raining
Start Go swimming
tomorrow?
Yes
Tidy up the cellar and
Clean room and do paint the walls
chores
Swimming
Some
time left?No
Some time Stop
left?
Yes
If-else
If statement statement
Control
structures in
Python
Nested-if If-elif-else
statement statement
If Statement
Python uses the if statement to change the flow of control in the program.
The indentation is used to mark the block of code.
Syntax
if condition: • A colon marks the beginning of the block of code.
statement • The block of code is indented, usually at four spaces.
statement • Each statement in the block of code should be at the same
……. indentation.
If Statement: Example
Example
If-Else Statement
The if-else statement evaluates the condition and executes the body of if only when the
test condition is True. Otherwise, the body of the else block will be executed.
Syntax
if condition:
statement 1
statement 2
else:
statement 3
statement 4
Example
If-Elif-Else Statement
The if-elif-else statement allows checking for multiple conditions. If the condition
for if is False, it checks the condition of the next elif block and so on.
Syntax
if condition 1:
statement
elif condition 2:
statement
elif condition 3:
statement
else:
statement
Only one block among the several if-elif-else blocks is executed according to the condition.
If all the conditions are False, the body of else is executed.
If-Elif-Else Statement: Example
Example
Nested-If
Syntax
if (condition 1):
statement
• This format is called nesting in programming.
# Executes when condition 1 is True
if (condition 2): • The level of nesting is defined by using
# Executes when condition 2 is also True indentation.
# inner if Block ends here
# outer if Block ends here
Nested-If: Example
Example
True or False
A loop statement allows the execution of a statement or group of statements multiple times.
Start
Condition End
Swimming
Some
time left?
True
Execute Statement
Do tax declaration
Types of Loops
Syntax
A loop will be repeated until a given condition changes True to False or False to
True, depending on the type of loop.
Syntax
Syntax
Start Start
No True
Execute Execute
Statement Do tax Statement Do tax
declaration declaration
The for loop is used to iterate over a The while loop is used to iterate over a
sequence list, tuple, string, or other block of code if the test expression is
objects. Its syntax is: true. Its syntax is:
The following examples illustrate the use of for and while loops.
Syntax
The inner and outer loops can be of the different or the same type.
Nested Loops: Flowchart
Yes Yes
Is this the last
element of
outer loop?
End of outer No
loop
No
No
Inner loop
statements
Nested Loops: Example
Syntax
• The break statement breaks the
innermost enclosing of for or while
break loop.
• It terminates the nearest enclosing loop
and skips the optional else.
• If a loop is terminated by a break, the
loop variable keeps its current value.
Break: Example
Example
Loops Control Statements: Continue
Syntax
• The continue statement skips the
current iteration and continues with the
next iteration.
continue • It does not terminate the loop; it just
moves the control to the next iteration.
• Since the loop does not terminate
unexpectedly, it executes the optional
else block of the loop.
Continue: Example
Example
Loop Control Statements
The loop else statement is not executed when the loop is terminated because
of a break statement.
Start Start
True True
Example
While Else Statement: Example
Example
Range Function
The built-in function range can be used with loops to iterate over a sequence of numbers. It
generates an iterator of arithmetic progressions.
range(n) range()
range(start, stop)
syntax
It generates a sequence of n integer numbers starting from 0 and ending with (n-1).
Example
Range(start, stop) Function
It generates a sequence of integer numbers starting with the start value and ending
with (stop-1).
Example
Step in Range
The range() function has an additional optional step argument that specifies the increment of the
sequence.
Syntax
The default increment value is 1. The increment value can be positive or negative but
not zero.
Step in Range: Example
Positive step value creates a forward Negative step value creates a reverse
sequence. sequence.
Example Example
Key Takeaways
The break and continue statements are used to skip some statements
inside the loop or terminate the loop immediately without checking the
condition.
Python supports the else clause with for and while loops.
A. for loop
B. while loop
C. do-while loop
A. for loop
B. while loop
C. do-while loop
A. if a>=2 :
B. if (a>=2)
C. if a>=22
D. if a=>22
Knowledge
Check
Which one of the following is a valid Python if statement?
2
A. if a>=2 :
B. if (a>=2)
C. if a>=22
D. if a=>22
A. while loop
B. for loop
C. do-while loop
D. Both A and B
Knowledge
Check
Where can the continue statement be used?
3
A. while loop
B. for loop
C. do-while loop
D. Both A and B
The continue statement can be used in both while and for loops.