02 Flow Control
02 Flow Control
02 Flow Control
num = 3
if num > 0:
print(num, "is a positive number.")
print("This is always printed.")
num = -1
if num > 0:
print(num, "is a positive number.")
print("This is also always printed.")
Python if...else Statement
num = 3
if num >= 0:
# num is >= 0
print("Positive or Zero")
else:
# num is < 0
print("Negative number")
Python if...else Statement
'''In this program, we check if the number is positive or
negative or zero and display an appropriate message'''
num = 3.4
if num > 0:
print("Positive number")
elif num == 0:
print("Zero")
else:
print("Negative number")
Python Nested if Example
'''In this program, we input a number check if the number is
positive or negative or zero and display an appropriate
message This time we use nested if statement'''
num = 0 num = 0
num = -1 num = -1
# List of numbers
numbers = [6, 5, 3, 8, 4, 2, 5, 4, 11]
The break keyword can be used to stop a for loop. In such cases, the else part is ignored.
n = 10
while i <= n:
sum = sum + i
i = i+1 # update counter
counter = 0
Output:
Inside loop
Inside loop
Inside loop
Inside else
Python break and continue