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

If-Else: 1. Illustrate The Different Types of Control Flow Statements Available in Python With Flowcharts

The document discusses 6 different types of flow control statements in Python: 1) if-else, 2) nested if-else, 3) for loops, 4) while loops, 5) break statements, and 6) continue statements. Each statement is explained with its syntax and a flowchart example. Additionally, the PASS statement is discussed as a placeholder with no functionality.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5K views

If-Else: 1. Illustrate The Different Types of Control Flow Statements Available in Python With Flowcharts

The document discusses 6 different types of flow control statements in Python: 1) if-else, 2) nested if-else, 3) for loops, 4) while loops, 5) break statements, and 6) continue statements. Each statement is explained with its syntax and a flowchart example. Additionally, the PASS statement is discussed as a placeholder with no functionality.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

1.

Illustrate the different types of control flow statements available in Python with
flowcharts.

There are 6 different types of flow control statements available in Python:

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. ...

Understand this statement with a flow chart.


Example

Check In[3] and In[4] and also In[5] and In[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. ...

Flow chart of Nested-if else

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

Check In[3] and In[8] and also In[9] and In[10].


for Statement
The for loop statement has variable iteration in a sequence(list, tuple or string) and
executes statements until the loop does not reach the false condition.

Syntax

1. for value in sequence:


2. ...body statement of for

Flow chart of for statement


Example

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..

Flow chart of while loop


Example

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

II. with for loop

1. for value in sequence:


2. ...body statement of for
3. if(<condition>):
4. break
5. ...body statement of for loop
6.
7. ...body statement outside of for loop

III. with a while loop


1. while(<condition>):
2. statement 1...
3. if(<condition>):
4. break
5. ...Statement of while loop
6.
7. ....Statements outside while loop

Break statement Flow Chart

Example
Continue Statement
A continue statement won’t continue the loop, it executes statements until the
condition matches True.

Syntax

I. continue

II. with for loop

1. for value in sequence:


2. ...body statement of for
3. if(<condition>):
4. continue
5. ...body statement of for loop
6.
7. ...body statement outside of for loop

III. with the while loop

1. while(<condition>):
2. statement 1...
3. if(<condition>):
4. continue
5. ...Statement of while loop
6.
7. ...Statements outside while loop

Continue statement Flow Chart

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.

1. # take input from the user


2. nterms = int(input("How many terms? "))
3.
4. # first two terms
5. n1 = 0
6. n2 = 1
7. count = 2
8.
9. # check if the number of terms is valid
10. if nterms <= 0:
11. print("Plese enter a positive integer")
12. elif nterms == 1:
13. print("Fibonacci sequence:")
14. print(n1)
15. else:
16. print("Fibonacci sequence:")
17. print(n1,",",n2,end=', ')
18. while count < nterms:
19. nth = n1 + n2
20. print(nth,end=' , ')
21. # update values
22. n1 = n2
23. n2 = nth
24. count += 1

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"

print "Maximum number is ", largest


print "Minimum number is ", smallest
o/p:

#Enter 7,2,bob,10,4 u will get desired output

You might also like