0% found this document useful (0 votes)
8 views2 pages

011 Condition

Selection statements alter the normal flow of control in a program based on conditions, with the 'if' statement being the most important in Python. The 'elif' statement allows for multiple conditions to be evaluated without excessive indentation, while the 'else' statement executes if all previous conditions are false. Together, these form the if-elif-else ladder for decision-making in code.

Uploaded by

Iulian Rusu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views2 pages

011 Condition

Selection statements alter the normal flow of control in a program based on conditions, with the 'if' statement being the most important in Python. The 'elif' statement allows for multiple conditions to be evaluated without excessive indentation, while the 'else' statement executes if all previous conditions are false. Together, these form the if-elif-else ladder for decision-making in code.

Uploaded by

Iulian Rusu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

SELECTION STATEMENTS

Normally, the statements are executed sequentially as written in


the program (i.e. Top to bottom). This is known as the normal flow
of control. But sometimes the operational flow of control has to
be altered depending upon a condition. The statements that allow
to do so, are known as selection statements. Most important
selection statement that python offers is the If statement.
Selection statements are also known as conditional statements or
decision statements.
THE IF STATEMENT
An if statement tests a particular condition; if the condition
evaluates to true, a course of action is followed i.e. a statement of
a set of statements is executed. If the condition is false the course
of action is ignored.
The syntax of if statement is as shown below:
if (expression) :
Statement

THE ELIF STATEMENT


In some cases the program requires you to write multiple if
statements. This results in many condition evaluations thus
slowing up the program. So to avoid this elif statements can be
used.
Unlike if statement elif statements stops further evaluation of
conditional statement once a condition is true. The keyword ‘elif‘
is short for ‘else if’, and is useful to avoid excessive indentation.
An if ... elif ... elif ... sequence is a substitute for
the switch or case statements found in other languages.

USING ELSE
If all the conditions inside conditional statements are false then
the body of the else loop will be executed.
If (expression) :
Statement1
Elif(expression):
Statement2
Else:
Statement3

If , elif and else this way, together form the if-elif-else ladder.

You might also like