python

Control Flow : Conditional Statement


Control Flow 

  • It describes the order in which statements will be executed at runtime.
  • Pay attention to the indentation.

 

 

Conditional Statements (if)

one line approach for single line:

If condition evaluates to true then the statement will be executed.

syntax:

if condtions : statement


one line approach for Multiple Statement:

syntax:

if conditons : statement1; statement


Multiple line approach:

syntax:

if conditions:
    statements

 

if-else:

If condition evaluates to true then the action1 will be executed otherwise action2 will be executed.

syntax:

if conditions:
 action1
else:
 action2

 

if-elif-else:

if conditions1 evaluated to true action1 will be executed otherwise it will check for next condition i.e condtion2, and if condition2 evaluates to true action2 will be executed. If none of the condition evaluates to true else block will be executed.

   

https://cdn.programiz.com/sites/tutorial2program/files/Python_if_elif_else_statement.jpg

 

syntax:

if conditions1:
 action1
elif condition2:
 action2
else:
 default action

python