0% found this document useful (0 votes)
10 views4 pages

Control Statements

Uploaded by

Firoza
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)
10 views4 pages

Control Statements

Uploaded by

Firoza
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/ 4

1.

If-Else Statement

python

# Program to check if a number is positive, negative,


or zero

num = float(input("Enter a number: "))

if num > 0:
print("Positive number")
elif num == 0:
print("Zero")
else:
print("Negative number")

2. Nested If Statement

python

# Program to check if a number is positive, negative,


or zero, using nested if

num = float(input("Enter a number: "))

if num >= 0:
if num == 0:
print("Zero")
else:
print("Positive number")
else:
print("Negative number")

3. For Loop

python

# Program to print the first 10 natural numbers

for i in range(1, 11):


print(i)
4. While Loop

python

# Program to calculate the factorial of a number


using a while loop

num = int(input("Enter a number: "))


factorial = 1

if num < 0:
print("Factorial does not exist for negative
numbers.")
elif num == 0:
print("Factorial of 0 is 1.")
else:
while num > 0:
factorial *= num
num -= 1
print("Factorial is", factorial)

5. Break Statement

python

# Program to demonstrate break in a loop

for i in range(1, 11):


if i == 5:
break
print(i)

6. Continue Statement

python

# Program to demonstrate continue in a loop

for i in range(1, 11):


if i == 5:
continue
print(i)
7. Pass Statement

python

# Program to demonstrate the use of pass statement

for i in range(1, 6):


if i == 3:
pass # Pass does nothing, just a placeholder
print(i)

8. Else with Loop

python

# Program to show the use of else with a loop

for i in range(1, 6):


print(i)
else:
print("Loop ended.")

9. Switch Case (Using Dictionary)

Python doesn’t have a switch-case construct, but it can be mimicked using a


dictionary.

python

# Program to mimic switch-case using a dictionary

def switch_case(argument):
switcher = {
1: "January",
2: "February",
3: "March",
4: "April"
}
return switcher.get(argument, "Invalid month")

month = int(input("Enter a month number: "))


print(switch_case(month))
10. For Loop with Else

Python

# Program to demonstrate for-else loop

for i in range(1, 6):


print(i)
else:
print("The loop ended successfully")

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy