Ch_6_Flow_of_control (1)
Ch_6_Flow_of_control (1)
INTRODUCTION
• A program is executed in sequence, that is, each statements are executed one by
one in order from top to bottom.
Selection
Repetition
OUTPUT
SELECTION
• if statement – executes the statement(s) inside if when the condition is TRUE only
If else statement
if...else statement executes the statement(s) inside if when the condition is true, otherwise
executes the statement(s) inside else (when the condition is false).
else if OR elif
Nested if
A nested if is an if that has another if in its if's body or in elif's body or in its else's body.
Syntax :
Example
INDENTATION
In most programming languages, the statements within a block are put inside curly brackets.
Python uses indentation for block as well as for nested block structures.
Leading whitespace (spaces and tabs) at the beginning of a statement is called indentation.
In Python, the same level of indentation associates statements into a single block of code.
The interpreter checks indentation levels very strictly and throws up syntax errors if
indentation is not correct.
It is a common practice to use a single tab for each level of indentation.
Example
Output
Repetition / looping / iteration
• A loop in an algorithm means execution of some program statements repeatedly till some
specified condition is satisfied.
Types of loops
For loop : For loop is used to iterate over a sequence of items Ex. list, tuple, etc.
Nested loop: A nested loop means a loop statement inside another loop statement. That is
why nested loops are also called “loop inside loops
The for statement is used to iterate over a range of values or a sequence. The for loop is executed
for each of the items in the range.
Example: 1
Example: 2
If step is also not specified, by default the value increases by 1 in each iteration.
The while statement executes a block of code repeatedly as long as the control condition of
the loop is true.
The control condition of the while loop is executed before any statement inside the loop is
executed.
Example
Example 2
o/p
we may want to exit from a loop (come out of the loop forever) or skip some statements of
the loop before continuing further in the loop.
These requirements can be achieved by using break and continue statements, respectively.
Break Statement
The break statement alters the normal flow of execution as it terminates the current loop
and resumes execution of the statement following that loop.
o/p
Ex2: Program to check if the input number is prime or not.
Prime number : A number that can be divided exactly only by itself and 1
o/p
Ex3: Find the sum of all the positive numbers entered by the user. As soon as the user enters a
negative number, stop taking in any further input from the user and display the sum.
o/p
Continue Statement
When a continue statement is encountered, the control skips the execution of remaining
statements inside the body of the loop for the current iteration and jumps to the beginning
of the loop for the next iteration.
Example
NESTED LOOPS
A loop may contain another loop inside it. A loop inside another loop is called a nested loop.
Ex2: Program to print the pattern for a number input by the user
Ex3: Write a program to calculate the factorial of a given number
Ex4: Program to find prime numbers between 2 to 25 using nested for loops.
o/p