Table of contents | |
Decision Making Statement | |
If Statement | |
If…else statement | |
if-elif ladder | |
Nested if statements | |
For Loop | |
While Statement |
In programming languages, decision-making statements determine the program’s execution flow. Python has the following decision-making statements:
The if statement is used to test a condition: if the condition is true, a set of statements is executed (called the if-block).
Syntax -
test expression:
statement(s)
# Check if the number is positive, we print an appropriate message
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 always printed”)
The if/else statement is a control flow statement that allows you to run a block of code only if a set of conditions are satisfied.
Syntax -
if test expression:
Body of if
else:
Body of else
# A program to check if a person can vote
age = input(“Enter Your Age”)
if age >= 18:
print(“You are eligible to vote”)
else:
print(“You are not eligible to vote”)
# Write Python program to find the greatest number among two numbers
num1 = int(input(“Enter Number”));
num2 = int(input(“Enter Number”));
if num1 >= num2:
if num1 == num2:
print("Both numbers are equal.")
else:
print("Fisrt number is greater than the second number.")
else:
print("Second number is greater than the First number.")
# Write python program to check the number is even or odd
num = int(input("Enter a number: "))
if (num % 2) == 0:
print("{0} is Even".format(num))
else:
print("{0} is Odd".format(num))
Elif stands for “else if.” It enables us to check for several expressions at the same time. If the if condition is False, the next elif block’s condition is checked, and so on. The body of else is executed if all of the conditions are False.
Syntax -
if test expression:
Body of if
elif test expression:
Body of elif
else: Body of else
# To check the grade of a student
Marks = 60
if marks > 75:
print("You get an A grade")
elif marks > 60:
print("You get a B grade")
else:
print("You get a C grade")
An if…elif…else sentence can be nestled inside another if…elif…else statement. In computer programming, this is referred to as nesting.
# Write a program to check weather number is zero, positive or negative
num = float(input("Enter a number: "))
if num >= 0:
if num == 0:
print("Zero")
else:
print("Positive number")
else:
print("Negative number")
The for statement allows you to specify how many times a statement or compound statement should be repeated. A for statement’s body is executed one or more times until an optional condition is met.
Syntax -
for val in sequence:
Body of for
# Program to find the sum of all numbers stored in a list
numbers = [6, 5, 3, 8, 4, 2, 5, 4, 11]
sum = 0
for val in numbers:
sum = sum+val
print("The sum is", sum)
The while statement allows you to repeatedly execute a block of statements as long as a condition is true. A while statement is an example of what is called a looping statement. A while statement can have an optional else clause.
Syntax -
while test_expression:
Body of while
# Program to add natural
n = int(input("Enter n: "))
sum = 0
i = 1
while i <= n:
sum = sum + i
i = i+1
print("The sum is", sum)
40 videos|35 docs|6 tests
|
1. What is the purpose of a decision-making statement in programming? |
2. What is the difference between an if statement and an if-else statement? |
3. How does a nested if statement work in programming? |
4. What is the difference between a for loop and a while loop in programming? |
5. How does a switch statement differ from an if-else statement in programming? |
|
Explore Courses for Class 10 exam
|