Hello friends how are you, today in this blog i will teach you what is if statement, syntax of if statement , how if statement works and many programs using if statement in a very simple way.
Let's start
If statement
Syntax:
if condition : body
- It is used to test the condition and the result is based on the condition.
- If the condition is true its body will execute otherwise does not execute.
no=int(input("Enter any number:"))
if no>5:
print("Number is greater than 5")
/*
Output
Enter any number:10
Number is greater than 5
*/
Here in this program we will use single variable for number , After taking user input we will apply condition on it. if the given number is greater than 0 then it will definitely positive , if the given number is less than 0 we will print negative and at last if the number is equal to 0 then we will print Number is Zero.
no=int(input("Enter any number:"))
if no > 0:
print("Number is positive")
if no < 0:
print("Number is negative")
if no == 0:
print("Number is Zero")
"""
###Output###
Run 1
Enter any number:5
Number is positive
Run 2
Enter any number:-9
Number is negative
"""
Example 3: Find greatest value in three number
#taking user input for numbers
no1=int(input("Enter first number:"))
no2=int(input("Enter second number:"))
no3=int(input("Enter third number:"))
#applying condition on numbers
if no1>no2 and no1>no3:
print(no1," is greatest value")
if no2>no1 and no2>no3:
print(no2," is greatest value")
if no3>no1 and no3>no2:
print(no3," is greatest value")
"""
###Output###
Enter first number:89
Enter second number:85
Enter third number:72
89 is greatest value
"""
Request:-If you found this post helpful then let me know by your comment and share it with your friend.
If you want to ask a question or want to suggest then type your question or suggestion in comment box so that we could do something new for you all.
If you have not subscribed my website then please subscribe my website.
Try to learn something new and teach something new to other. Thanks.
0 Comments