Conditional statements-
In python, decision making is performed by the following statements.
If-statement - The if statement is used to test a specific condition. If the condition is true, a block of code(If-block) will be executed.
IF-else Statement - The if-else statement is similar to if statement except the fact that, if also provides the block of the code for the false case of the condition to be checked. If the condition provided in the if statement is false, then the else statement will be executed.
Nested if statement - Nested if statements enable us to use if ? else statement inside an outer if statement.
If Statement
The if statement is used to test a particular condition and if the condition is true, it executes a block of code known as If-block. The condition of if statement can be any valid logical expression which can be either evaluated to true of false.
Syntax
statement
Example 1:
if num%2==0
print("Number is even")
Output:
Number is even
Example 2:
b= int(input("Enter b value"))
c= int(input("Enter c value"))
if a>b and a>c:
print(" a is largest");
if b>a and b>c:
print(" b is largest");
if c>a and c>b:
print("c is largest");
Output:
Enter b value 2000
Enter c value 3000
c is largest
If-Else Statement :
The If -else statement provides an else block combined with the if statement which is executed in the false case of the condition. If the condition is true, then the if-block is executed, otherwise, the else-block is executed.
Syntax -
#block of statements
else
#another block of statements(else-block)
Example - Program to check whether a person is eligible to vote or not.
age=int(input("Enter your age"))
if age>=18:
print("you are eligible to vote")
else
print("sorry you have to wait")
Output :
you are eligible to vote
Example : Program to check whether a number is even or not.
if num%2==0
print("Number is even..")
else
print("Number is odd..")
Output -
Number is even
elif Statement :
The elif statement enables us to check multiple conditions and execute the specific block of statements depending upon the true condition among them. We can have any number of elif statements in out program depending upon our need. However, using elif is optional.
The elif statement works like an if-else-if ladder statement in C. It must be succeeded by an if statement.
Syntax :
#block of statements
elif expression 2:
#block of statements
elif expression 3:
#block of statements
else:
#block of statements
Example 1:
if number == 10:
print("number is equals to 10")
elif number == 50
print("number is equal to 50")
elif number == 100
print("number is equal to 100")
else
print("number is not equal to 10, 50 or 100")
Output:
number is not equal to 10, 50 or 100
Example 2:
if marks > 85 and marks <= 100:
print("scored grade A")
elif marks > 60 and marks <= 85:
print("Score grade B+")
elif marks > 40 and marks <= 60:
print("Scored grade B")
print("scored grade C")
else:
print("fail")
No comments:
Post a Comment
Note: only a member of this blog may post a comment.