Page 112 - 2611_SmartGPT Pro V(5.0) C-7
P. 112
After the if condition, on the next line, write an indented block of statements. When the condition
evaluates to true, the block of statements given below it is executed; otherwise, it is skipped and
control flows to the statement after if.
Here, if the age of the user is more than or equal to 18, ‘Eligible’ will be printed, otherwise nothing
will be printed.
Program 1. To demonstrate the use of the ‘if’ statement.
Program1.py
File Edit Format Run Options Window Help
age=int(input("Enter Age: "))
if(age>=18):
print("You are eligible to cast a vote")
Output
Enter Age: 20
You are eligible to cast a vote
Enter Age: 16
Program 2. To greet the user based on the time entered.
Program2.py
File Edit Format Run Options Window Help
time=int(input("Enter Time in 24-hour format: "))
if (time<12):
print("Good Morning")
Output
Enter Time in 24-hour format: 6 Write a Python program to enter a
Good Morning number. If it is less than 0, display:
‘Negative numbers are
Enter Time in 24-hour format: 18
not allowed’.
IF … ELSE … STATEMENT
The if…else… statement lets your program choose between two options. Python checks the
condition; if it’s true, it executes statement block 1 after the if statement—otherwise, it executes
statement block 2 after the else statement.
110 Computer Science (V5.0)-VII

