CHAPTER-4
CONTROL STATEMENTS (FLOW OF CONTROL)
FLOW OF CONTROL
Flow of control means the order in which the statements will be [Link] 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 [Link] statements are sometimes important to write
in the code where we feel that the presence of statement is [Link] python
empty statements are given by pass statement.
Note:- Python pass statement is a empty statement as it does [Link] is written in
such a case where presence of statement is mandatory.
2. Simple statement
Any single executable statement is called simple statement.
3. Compound statement
A compound statement represents group of statements executed as a unit.
Syntax:-
Compound statementHeader:
Set of statements
SELECTION CONSTRUCT(DECISION MAKING)
Decision making or sequential construct are used to control the flow of execution in a program
depending upon the condition.
In python,decision making can be done in four ways:-
1. if statement
2. if-else statement
3. if-elif ladder
4. Nested if else statement
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 [Link] 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 [Link] 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.
Some more variations of Nested if:
It can have the following forms:
ALGORITHM:-
An algorithm is a step by step procedure to solve a problem.
Example of algorithm
FLOWCHARTS:-
Flowchart is the pictorial representation of a task. It shows different subtasks
with different symbols.
Algorithm and flowchart is the preplanning done for a program.
PSEUDOCODE:-
Pseudocode is an informal way of describing the steps of a describing the steps of a
program’s solution without using any strict programming language syntax or underlying
technology considerations.
Pseudocode to check whether first number is divisible by second number or not
Input first number in variable n1
Input second number in variable n2
Remainder=firstnumber modulus second number
if the remainder is 0
Display the first number is divisible by second number
else
Display the first number is not divisible by second number