What is the output of the following code? x = 7 if x > 10: print(“Greater than 10”) else: print(“10 or less”)
a) Greater than 10
b) 10 or less
c) Error
d) None
Answer:
b) 10 or less
Explanation:
In this code, the variable x
is assigned the value 7. The if
statement checks whether x
is greater than 10. Since 7 is not greater than 10, the else
block is executed, printing “10 or less”.
x = 7
if x > 10:
print("Greater than 10")
else:
print("10 or less") # This line is executed
This example demonstrates how if-else
statements allow you to control the flow of your program based on conditions, ensuring that the appropriate block of code is executed depending on the values being tested.
Using if-else
statements is essential for creating responsive and adaptable programs that can make decisions based on varying inputs or states.