XI-Conditional & Looping Constructs (1)
XI-Conditional & Looping Constructs (1)
Flow Chart
Flow Chart – It is a pictorial representation of step-by-step solution to a given problem
Examples:
Conditional Statement
Conditional Statement
if statement:
➢ "if statement" is written by using the “if” keyword.
➢ It states if condition given in program is true, perform all steps that follow.
➢ And if condition given in program is false, it won’t perform at all
➢ Python relies on indentation (whitespace at the beginning of a line) to define
scope in the code.
Output:
If-else statement:
➢ "if-else statement" is written by using the “if” and “else” keyword.
➢ In case if condition is evaluated to true, then the statement/statements
indented inside if statement are executed, otherwise statement/statements
following else statement are executed
Output:
Output:
If-elif-else statement:
➢ This statement is used to check with multiple options.
➢ As soon as one condition is ‘true’ then rest of the conditions bypassed.
Output:
Output:
Output:
Output:
Looping / Iteration Statement
Looping / Iteration
Looping/Iteration Statement
for-loop while-loop
start states the integer value at which the sequence begins, if this is not included
then start begins at 0
stop is always required and is the integer that is counted up to but not included
step sets how much to increase (or decrease in the case of negative numbers) the
next iteration, if this is omitted then step defaults to 1
• Syntax:
range ( [ start ] , step , [ stop ] )
command output
range(10) [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
range(1,10) [ 1, 2, 3, 4, 5, 6, 7, 8, 9]
range(1,11) [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
range(2, 21, 2) [ 2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
range(5, 51, 5) [ 5, 10, 15, 20, 25, 30, 35, 40, 45, 50]
range(0, -11, -1) [ 0, -1, -2, -3, -4, -5, -6, -7, -8, -9, -10]
Break statement
• Terminates the loop containing it and resumes the statement just after the end of
that loop
• If break statement appears in nested loop, then break will terminate the innermost
loop. (loop within ‘break’ statement written)
Continue statement
• It is used to skip rest of statements in loop for current iteration
• If continue statement encounters in loop, control jumps to the beginning of the
loop for next iteration (skipping statements inside loop) without terminating the
loop
Pass statement
• It is used when a statement is required syntactically but you do not want any
command or code to execute.
• The pass statement is a null operation; nothing happens when it executes.
The pass is also useful in places where your code will eventually go, but has not been
written yet
For loop
• A for loop is used for iterating over a sequence (that is either a list, a tuple, a
dictionary, a set, or a string)
Loop output
for a in 'PYTHON': P
Y
print (a) T
H
O
N
for a in '123456789': 1
2
print (a) 3
4
5
6
7
8
9
for a in range (5,51,5): 5
10
print (a) 15
20
25
30
35
40
45
50
First, let’s only pass the stop argument, so that our sequence set up is range(stop):
for i in range(6):
print(i)
In the program above, the stop argument is 6, so the code will iterate from 0-6 (exclusive
of 6):
Output
0
1
2
3
4
5
Next, we’ll look at range(start, stop), with values passed for when the iteration should
start and for when it should stop:
for i in range(20,25):
print(i)
Here, the range goes from 20 (inclusive) to 25 (exclusive), so the output looks like this:
Output
20
21
22
23
24
The step argument of range() is similar to specifying stride while slicing strings in that it
can be used to skip values within the sequence.
With all three arguments, step comes in the final position: range(start, stop, step). First,
let’s use a step with a positive value:
for i in range(0,15,3):
print(i)
In this case, the for loop is set up so that the numbers from 0 to 15 print out, but at
a step of 3, so that only every third number is printed, like so:
Output
0
3
6
9
12
We can also use a negative value for our step argument to iterate backwards, but we’ll
have to adjust our start and stop arguments accordingly:
for i in range(100,0,-10):
print(i)
Here, 100 is the start value, 0 is the stop value, and -10 is the range, so the loop begins at
100 and ends at 0, decreasing by 10 with each iteration. We can see this occur in the
output:
Output
100
90
80
70
60
50
40
30
20
10
Output:
Output:
For loop - with break Statement
With the break statement we can stop the loop before it has looped through all the
items:
Output:
Output:
For loop - with continue Statement
With the continue statement we can stop the current iteration of the loop, and
continue with the next:
Output:
Here, key point of the while loop is that the loop might not ever run. When the
condition is tested and the result is false, the loop body will be skipped and the first
statement after the while loop will be executed.
Output:
Output:
Using else Statement with Loops
Python supports to have an else statement associated with a loop statement.
• If the else statement is used with a for loop, the else statement is executed when
the loop has exhausted iterating the list.
• If the else statement is used with a while loop, the else statement is executed when
the condition becomes false.
Output:
Nested Loop (Loops Inside Loops):
• A nested loop is a loop inside a loop.
• The "inner loop" will be executed one time for each iteration of the "outer loop":
Output:
Solved Questions:
Define Flow Charts?
It is a pictorial representation of step-by-step solution to a given problem
What is a statement?
It is a logical instruction which is executed while running a program.
What are different ways of flow of control in Python?
Flow of control in a program can be in 3 ways:
1. Sequential
2. Conditional
3. Iteration
How many kinds of statements Python supports?
Python supports 3 types of statements:
1. Empty statement
2. Simple statement
3. Compound statement
What is the significance of an empty statement?
An empty statement does nothing. It is a pass statement in Python.
Differentiate simple and compound statement.
Single executable statement forms simple statement where as compound statement has
a header and an indented body below the header. For example-if and while statement.
What is the difference between ‘break’ and ‘continue’ statement?
Both are ‘jump’ statements and are used to change the flow of loop.
Break statement Terminates the loop containing it and resumes the statement just after
the end of that loop.
If continue statement encounters in loop, control jumps to the beginning of the loop for
next iteration (skipping statements inside loop) without terminating the loop.
What is the use of range ()?
Range () is used in looping constructs to generate a sequence of list types.
What is the use of ‘else’ in a loop?
Looping statement can have an optional ‘else’ block.
If else statement is used with a for loop, the else statement is executed when the loop has
exhausted iterating the list.
If the else statement is used with a while loop, the else statement is executed when the
condition becomes false.
Differentiate ‘for’ and ‘while’ loop constructs.
For loop While loop
It is called ‘definite loop’ It is called ‘indefinite loop’
It is known in advance how many times the loop is Number of iterations are not exactly known
going to execute.
In 'for' loop iteration statement is written at top, In 'while' loop, the iteration statement can be written
hence, executes only after all statements in loop are anywhere in the loop.
executed.
Define nested loop.
A loop contained within another loop is called nested loop.
What do you mean by an infinite loop? How it occurs in looping construct and how to
solve this issue?
A loop becomes infinite loop if a condition never becomes FALSE.
You must use caution when using while loops because of the possibility that this condition
never resolves to a FALSE value.
This results in a loop that never ends. Such a loop is called an infinite loop.