Control STMT
Control STMT
if num >= 0:
if num == 0:
print("Zero")
else:
print("Positive number")
else:
print("Negative number")
prolanguages = [“c”, “Python”, “C++”, “Java”]
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.
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