ch-3 Flow of Control, Types of Statement, If, If Else, If Elifladder, Nested If, Algorithm, Flowchart, Pseudocode
ch-3 Flow of Control, Types of Statement, If, If Else, If Elifladder, Nested If, Algorithm, Flowchart, Pseudocode
FLOW OF CONTROL
Flow of control means the order in which the statements will be executed.This order can be
sequentially,selectively or iteratively.
Sequentially means every lines of the program will be executed line by line.
Selectively means some part of the code is executed as per the desired condition and some
may not.
In the example given below,some statements are executed as per the condition and some
are not executing.
Iteration means some part of the code executed desired number of times.
Types of statements in python:
Statements in python are divided into 3 categories:-
1. Empty statement
Empty statement does nothing.Empty statements are sometimes important to write
in the code where we feel that the presence of statement is compulsory.In python
empty statements are given by pass statement.
3. Compound statement
A compound statement represents group of statements executed as a unit.
Syntax:-
Compound statementHeader:
Set of statements
if statement:-
In case of if statement, if the condition is true then python will execute the statements given
inside if block, but if the condition is false it does nothing.
Syntax:-
if condition:
statement1
statement 2
……………
……………
if the condition is true then statement 1 and statement 2 will be exceuted.
We can also give the round brackets with condition but it is optional.
Note:-The condition given with if is tested.If the condition evaluates to true,then statements
inside if block will be executed.
if –else statement
In case of if-else statement firstly the condition given with if is tested.If the
condition evaluates to true then the statements in the if block will be executed
otherwise the statement in the else block will be executed.
Syntax:-
if condition:
statements
else:
Statements
Note:-
if and else are keywords
if elif ladder
In case of if elif ladder multiple condition testing can be done.
In this case,first the condition given with if is tested,if the condition
evaluates to false then condition given with elif is tested ,if again the
condition is false,then condition is being tested for all consecutive
elif’s otherwise the statement beneath else will be executed.
Syntax:
if condition1:
statement
elif condition2:
statement
elif condition3:
statement
….
….
….
…..
…
else:
statement
Note:-
In case of if-elif ladder,else part is optional
Nested if:-
An if inside another if body or elif body or else body is called nested if.