Conditional Statements
Conditional Statements
Syntax:
if(test-expression):
statement-block(s)
statement-x
• The if..else statement evaluates test expression and will execute the body
of if only when the test condition is True.
• If the condition is False, the body of else is executed. Indentation is used to
separate the blocks.
if(test-expression):
true-block-statement(s)
else:
false-block-statement(s)
statement-x
Datec Learning Centers
Example Program
Program 1:
else:
else:
print(”Transaction Successful”)
Datec Learning Centers
elif statement (chained conditional)
• The elif is short for else if. It allows us to check for multiple expressions.
• If the condition for if is False, it checks the condition of the next elif block
and so on.
• If all the conditions are False, the body of else is executed.
• Only one block among the several if...elif...else blocks is executed
according to the condition.
• The if block can have only one else block. But it can have
multiple elif blocks. Datec Learning Centers
elif statement (chained conditional)
Same as else if in C/C++
• We canOne conditional
write can also
an if statement be nested
inside within
another another This is called
if statement.
nesting in computer programming.
Syntax
if(test-condition-1):
if(test-condition-2):
statement-1
else:
statement-2
else:
statement-3
statement-x