x

Python control statements or control flow

PREV     NEXT

Sequential Statements:

  • Sequential statements are set of statements where the execution process will happen in sequence manner. So, these kind of statements are called as sequential statements.
  • The problem in sequential statements is, if the logic has broken in any one of the line, then complete source code execution will get broken. So, we are going to decision making, iterative and jump statements to avoid this kind of problems.

Decision Making Statements:

  • Decision Making Statements allow the program to take the decision as which statement should be executed next.
  • Decision Making statements are used when we want a set of instructions should be executed in one situation and different instructions should be executed in another situation .
  • Decision making can be implemented in python using,
  1.           if statements
  2.           if-else statements
  3.           if-elif-else statements
  4.           nested if statements

1. if Statements:

Syntax

:

i


f condition:
statement 1
statement 2
———-
———-
statement n;

if we observe above syntax, colon is mandatory ‘:’ at the end of the condition (but if observe in c/c++ programming, we can write conditions in parenthesis)

  • In python, decision making statement’s body starts with indentation and end with indentation
  • Here, if the condition becomes true then it will enter into the if condition and start the execution of the statements, till to reach end of the indent. Once indent end reached, then it will execute further statements.

Here, true means non zero value, false or none means zero.

Flow Chart

Example:

Output:

Person not eligible for vote
****************************
Person eligible for vote
Person not eligible for vote 

if we observe above example, here trying to check eligibility of a person for voting,


  • Here variable age is initialized as age=17, after that we have written the if statement for the age whether it is equal to or more than 18. Here condition is false. i.e. age>=18. So, it will execute the the statement and print “Person not eligible for vote”.
  • After that again the variable is modified as age=18
  • Here, variable age is initialized as age=18, after that we have written the if statement for the age is given equal to or more than 18, here condition is true i.e. age>=18
  • So, it executes the print statement “Person eligible for vote”. Once executed the statement, it reaches the end of the body after that it will execute the next statements if available.

2. if else Statements:

If we observe above if statement, it will evaluate only the true statement. But, we can evaluate the false statements by using if else statements. Below is the syntax for if else statements.

Syntax

:

i

f condition:
statement1
statement2
———-
———-
Statement

else:
statement1
statement2
———-
———-
Statement
next_statement(s)

Flow Chart
Example:

Output:

Person not eligible for vote
****************************
Person eligible for vote

3. if –elif -else Statements:

To evaluate one statement from multiple statements, we are going to use “if elif else” control statements

Syntax

:

i

f condition:
statement1
statement2
———-
———-
Statement

elif condition:
statement1
statement2
———-
———-
statement

else:
statement1
statement2
———-
———-
statement
next_statement(s)

Flow Chart:

Example:

Output:

Person eligible for vote

PREV     NEXT



Like it? Please Spread the word!