0% found this document useful (0 votes)
5 views

conditional loop

Chapter 3 of 'Problem Solving and Programming with Python' by Reema Thareja discusses control flow statements in Python, including sequential, selection, and iterative controls. It covers various types of selection statements such as if, if-else, nested if, and if-elif-else, as well as iterative statements like while and for loops, including the use of the range() function. Additionally, it explains control statements such as break, continue, pass, and the use of else with loops.
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)
5 views

conditional loop

Chapter 3 of 'Problem Solving and Programming with Python' by Reema Thareja discusses control flow statements in Python, including sequential, selection, and iterative controls. It covers various types of selection statements such as if, if-else, nested if, and if-elif-else, as well as iterative statements like while and for loops, including the use of the range() function. Additionally, it explains control statements such as break, continue, pass, and the use of else with loops.
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/ 25

Problem Solving and

Programming with

Python
Reema Thareja
Chapter 3 : Control
Flow Statements
.

© Oxford University Press 2019. All rights reserved.


Control Statements

• A control statement is a statement that determines the control flow of a set of instructions,
i.e., it decides the sequence in which the instructions in a program are to be executed.

• Types of Control Statements —

• Sequential Control: A Python program is executed sequentially from the first line of the
program to its last line.

• Selection Control: To execute only a selected set of statements.

• Iterative Control: To execute a set of statements repeatedly.


© Oxford University Press 2019. All rights reserved. 3
Selection or conditional branching statements
1.If Statement

Example

© Oxford University Press 2019. All rights reserved. 4


2.if-else Statement

Example

© Oxford University Press 2019. All rights reserved. 5


3.Nested if Statements

• A statement that contains other statements is called a compound statement. To perform more complex
checks,
• if statements can be nested, that is, can be placed one inside the other.
• In such a case, the inner if statement is the statement part of the outer one.
• Nested if statements are used to check if more than one conditions are satisfied.
• Example:

© Oxford University Press 2019. All rights reserved. 6


4.if-elif-else Statement

• Python supports if-elif-else statements to test additional conditions apart from the initial test expression. The if-elif-
else construct works in the same way as a usual if-else statement. If-elif-else construct is also known as nested-if
construct.
• Example:

© Oxford University Press 2019. All rights reserved. 7


Use the AND/OR operators to form a compound relation expression. In python, the
following expression is invalid

if(60<=marks<=75):

The correct way to write is:


if((mark>=60) and (mark<=75)):

© Oxford University Press 2018. All rights reserved. 8


3.3Basic Loop structures/Iterative
statements
• Iterative statements are decision control statements that are used to repeat
the execution of a list of statements.
• Two types of iterative statements-
• while loop
• for loop

© Oxford University Press 2018. All rights reserved. 9


3.3.1while Loop

Example

© Oxford University Press 2019. All rights reserved. 10


3.3.2 for Loop

• For loop provides a mechanism to repeat a task until a particular condition is True. It is usually known as a determinate or definite
loop because the programmer knows exactly how many times the loop will repeat.

• The for...in statement is a looping statement used in Python to iterate over a sequence of objects.

© Oxford University Press 2019. All rights reserved. 11


for Loop and range() Function

• The range() function is a built-in function in Python that is used to iterate over a sequence of numbers. The syntax of range() is
range(beg, end, [step]).

• The range() produces a sequence of numbers starting with beg (inclusive) and ending with one less than the number end. The step
argument is option (that is why it is placed in brackets). By default, every number in the range is incremented by 1 but we can
specify a different increment using step. It can be both negative and positive, but not zero.

• Examples

© Oxford University Press 2019. All rights reserved. 12


range() Function

• If range() function is given a single argument, it produces an object with values from 0 to argument-1. For example:
range(10) is equal to writing range(0, 10).

• If range() is called with two arguments, it produces values from the first to the second. For example, range(0,10).

• If range() has three arguments then the third argument specifies the interval of the sequence produced. In this case, the
third argument must be an integer. For example, range(1,20,3).
• Examples

© Oxford University Press 2019. All rights reserved. 13


for loop-arguments

© Oxford University Press 2018. All rights reserved. 14


3.3.3 selecting an appropriate loop
• Loop can be of different types such as
• -entry –controlled (also known as pre-test)

• -exit-controlled (also known as post-test)


• -Counter –controlled and conditional controlled (or sentinel
controlled) loop

© Oxford University Press 2018. All rights reserved. 15


Pre-test and post-test loops
• In entry –controlled loop---- condition is tested before the loop starts.
• In exit -controlled loop---- condition is tested after the loop is executed.

© Oxford University Press 2018. All rights reserved. 16


• We know in advance the number of times the loop should be executed–
counter controlled loop.
• The counter is a variable that must be initialized, tested and updated for
performing the loop operations.
• Such a counter-controlled loop in which the counter is assigned a constant or
a value is also known as a definite repetition loop.
• When we do not know in advance the number of times the loop will be
executed, we use conditional controlled or sentinel-controlled or indefinite
loop.
• In such a loop, a spl value called the sentinel value is used to change the loop control
expression from True to False.

© Oxford University Press 2018. All rights reserved. 17


Condition-controlled and Counter-controlled Loops

i=1
for i in range(1, 6): # Counter-controlled with range while i <= 5: # Condition-controlled with while
print(i) print(i)
i += 1

A sentinel value denotes the end of a data set, but it is not part of the data. A loop that uses a
sentinel value is called a sentinel-controlled loop. ​​
© Oxford University Press 2019. All rights reserved. 18
Nested for Loops

• Python allows its users to have nested loops, that is, loops that can be placed inside other loops. Although this feature will
work with any loop like while loop as well as for loop.

• A for loop can be used to control the number of times a particular set of statements will be executed. Another outer loop
could be used to control the number of times that a whole loop is repeated.

• Loops should be properly indented to identify which statements are contained within each for statement.

• Example:

© Oxford University Press 2019. All rights reserved. 19


The break Statement

• The break statement is used to terminate the execution of the nearest enclosing loop in which it appears. The break
statement is widely used with for loop and while loop. When compiler encounters a break statement, the control
passes to the statement that follows the loop in which the break statement appears.
• Example

© Oxford University Press 2019. All rights reserved. 20


The continue Statement

• Like the break statement, the continue statement can only appear in the body of a loop.
• When the compiler encounters a continue statement then the rest of the statements in the loop are skipped and the
control is unconditionally transferred to the loop-continuation portion of the nearest enclosing loop.
• Example:

© Oxford University Press 2019. All rights reserved. 21


• The continue statement is somewhat opposite to the break statement.
• The continue statement is usually used to restart a statement sequence
when an error occurs.
• i.e ---It is used to stop the current iteration of the loop and continues with the next one.

© Oxford University Press 2018. All rights reserved. 22


The pass Statement

• Pass statement is used when a statement is required syntactically (a way that relates to the grammatical
arrangement of words in a sentence)but no command or code has to be executed. It specified a null operation
or simply No Operation (NOP) statement. Nothing happens when the pass statement is executed.

• Difference between comment and pass statements In Python programming, pass is a null statement. The
difference between a comment and pass statement is that while the interpreter ignores a comment entirely, pass
is not ignored. Comment is not executed but pass statement is executed but nothing happens.

Example:

© Oxford University Press 2019. All rights reserved. 23


Use of pass statements
• This control statement in Python does not terminate or skip the execution,
• it simply passes to the next iteration.
• A loop cannot be left empty otherwise the interpreter will throw an error and
to avoid this, a programmer can use the pass statement.

© Oxford University Press 2018. All rights reserved. 24


The else Statement Used With Loops

• Unlike C and C++, in Python you can have the else statement associated with a loop statements. If the else statement
is used with a for loop, the else statement is executed when the loop has completed iterating.
• But when used with the while loop, the else statement is executed when the condition becomes false.
• Examples:

© Oxford University Press 2019. All rights reserved. 25

You might also like