0% found this document useful (0 votes)
1 views26 pages

Control STMT

The document provides examples and explanations of control flow statements in Python, including if-else statements, loops with else, break, continue, and pass statements. It illustrates how these statements can be used in both for and while loops with sample code. The document emphasizes the functionality and purpose of each statement in managing the flow of execution in a program.

Uploaded by

r.balamurugan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views26 pages

Control STMT

The document provides examples and explanations of control flow statements in Python, including if-else statements, loops with else, break, continue, and pass statements. It illustrates how these statements can be used in both for and while loops with sample code. The document emphasizes the functionality and purpose of each statement in managing the flow of execution in a program.

Uploaded by

r.balamurugan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

num = int(input(“Enter a value”))

if num >= 0:
if num == 0:
print("Zero")
else:
print("Positive number")
else:
print("Negative number")
prolanguages = [“c”, “Python”, “C++”, “Java”]

# access items of a list using for loop


for language in prolanguages:
print(language)

Output
C
Python
C++
Java
The else Statement Used with Loops:
• 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.

• For loop example


Output
for x in range(6): 0
if x == 10: 1
break 2
print(x) 3
else:
4
print("Finally finished!")
5
Finally finished!
The else Statement Used with Loops:

• while loop example

Output
x=0 0
while x<=5: 1
if x == 3:
2
break
print(x)
x+=1
else:
print("Finally finished!")
The break Statement:
• The break statement in Python terminates the current loop and resumes execution at the
next statement.
• The most common use for break is when some external condition is triggered requiring a
hasty exit from a loop. The break statement can be used in both while and for loops.
s=input("Enter a string:\n")
# Using for loop
for letter in s:
if letter == 'r' or letter == 'o':
break
print(letter) Output
print("Out of for loop") Enter a string:
print() software
s
# Using while loop
i=0
Out of for loop
while i<len(s):
if s[i] == 'r' or s[i] == 'o': s
break Out of while loop
print(s[i])
i += 1
print("Out of while loop")
The continue Statement:
• continue statement in Python returns the control to the beginning of the while loop. The
continue statement rejects all the remaining statements in the current iteration of the loop
and moves the control back to the top of the loop.
• continue statement can be used in both while and for loops.
s=input("Enter a string:\n")
# Using for loop
for letter in s:
if letter == 'r' or letter == 'o':
continue
print(letter) Output
print("Out of for loop") Enter a string:
print() software
s
# Using while loop
i=0
f
while i<len(s): t
if s[i] == 'r' or s[i] == 'o': w
continue a
print(s[i]) e
i += 1 Out of for loop
print("Out of while loop")
s
The pass Statement:
• pass statement in Python 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 (e.g., in
stubs for example):
Output
Enter a string:
software
s=input("Enter a string:\n") s
# Using for loop o
for letter in s: f
if letter == 'r' or letter == 'o':
t
pass
print(letter) w
print("Out of for loop") a
print() r
e
# Using while loop Out of for loop
i=0
while i<len(s): s
if s[i] == 'r' or s[i] == 'o': o
pass
f
print(s[i])
i += 1 t
print("Out of while loop") w
a
r
e
Out of while loop

You might also like