If-Else: 1. Illustrate The Different Types of Control Flow Statements Available in Python With Flowcharts
If-Else: 1. Illustrate The Different Types of Control Flow Statements Available in Python With Flowcharts
Illustrate the different types of control flow statements available in Python with
flowcharts.
1. if-else
2. Nested if-else
3. for
4. while
5. break
6. Continue
if-else
If-else is used for decision making; when code statements satisfy the condition, then
it will return non-zero values as true, or else zero or NONE value as false, by the
Python interpreter.
Syntax
1. if(<condition>):
2. Statement 1
3. ...
4. else:
5. Statement 2
6. ...
Nested if-else
With if...elif...else, elif is a shortened form of else if. It works the same as 'if'
statements, where the if block condition is false then it checks to elif blocks. If all
blocks are false, then it executes an else statement. There are multiple elif blocks
possible for a Nested if...else.
Syntax
1. if (<condition 1>):
2. Statement 1
3. ...
4. elif (<condition 2>):
5. Statement 2
6. ...
7. else
8. Statement 3
9. ...
Remember there is no condition statement associated with else part of these flow
control statements. It will execute ig statements only in the case that of all conditions
are false.
Example
Syntax
Check In[14] and In[16]. The continue statement is used to stop for loop, in case
there is an else block missed.
while loop
A while loop is used in python to iterate over the block of expression which matches
to true. Non-zero values are True and zero and negative values are False, as
interpreted in Python.
Syntax
1. while(<condition>):
2. statement 1..
Check In[4] and In[7]. In[7]. If the user wants to add a number of his choice, then
use: n = int(input("Enter number: ")) instead of n=20. Also, check-in In[3] for a
while..else loop.
Break statement
The Python Break statement is used to break a loop in a certain condition. It
terminates the loop. Also, we can use it inside the loop then it will first terminate the
innermost loop.
Syntax
I. break
Example
Continue Statement
A continue statement won’t continue the loop, it executes statements until the
condition matches True.
Syntax
I. continue
1. while(<condition>):
2. statement 1...
3. if(<condition>):
4. continue
5. ...Statement of while loop
6.
7. ...Statements outside while loop
Example
One more additional Flow statement is PASS.
PASS
In Python, pass, and comment both are quite similar. The pass contains a Null value.
The Python interpreter ignores the comment statement, but it’s not possible to ignore
a pass statement. There is nothing is going to execute when a pass is used. It is
used as a Place Holder in a loop or a function.
Example
It executes nothing.
2. Write a Program to Prompt for a Score between 0.0 and 1.0. If the Score is out of range, print an
error. If the score is between 0.0 and 1.0, print a grade using the following table
score =
input("Enter
Score: ")
s = float(score)
x = 'Error'
if s >= 0.9:
x = 'A'
elif s >=0.8:
x='B'
elif s >=0.7:
x='C'
elif s >= 0.6:
x='D'
elif s < .6:
x ='F'
else:
x ="Out of Range"
print (x)
3. Write a program to display the fibonacci sequences up to nth term where n is provided by the
user.
3. Write a
program
to
repeatedly
check for
the largest
number
until the
user
enters
“done”
largest =
None
smallest = None
while True:
try:
num = raw_input("Enter a number: ")
if num == 'done':
break;
n = int(num)
largest = num if largest < num or largest == None else
largest
smallest = num if smallest > num or smallest == None else
smallest
except:
print "Invalid input"