0% found this document useful (0 votes)
31 views

Ch_6_Flow_of_control (1)

Uploaded by

nothinghuman76
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)
31 views

Ch_6_Flow_of_control (1)

Uploaded by

nothinghuman76
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/ 16

Ch 6: Flow of control

INTRODUCTION

• The order of execution of the statements in a program is known as flow of control.

• A program is executed in sequence, that is, each statements are executed one by
one in order from top to bottom.

The flow of control can be implemented using control structures

 Selection

 Repetition

Program to print the difference of two numbers.

OUTPUT

SELECTION

• The selection construct means the execution of statement(s) depending upon a


condition-test.

• If a condition evaluates to True, a set of statements is followed otherwise another


set of statements followed.
if or simple if statement

• if statement – executes the statement(s) inside if when the condition is TRUE only

Example : To check is the person eligible to vote


Ex 2

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

• [if...elif...else] is use to check multiple conditions and execute statements


accordingly.

We can also write else if instead of elif for more clarity


Example

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.

We can have many levels of nesting inside each statements.

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.

• repetition is also known as iteration or loop

Types of loops

 For loop : For loop is used to iterate over a sequence of items Ex. list, tuple, etc.

 While loop: repeatedly execute a block of statements while a condition is true.

 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’ Loop

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

The Range() Function

The range() is a built-in function in Python.

Syntax of range() function is:

 In function range(), start, stop and step are parameters.

 The start and step parameters are optional.

 If start value is not specified, by default the list starts from 0.

 If step is also not specified, by default the value increases by 1 in each iteration.

 All parameters of range() function must be integers.

 The step parameter can be a positive or a negative integer excluding zero


Example

The ‘While’ Loop

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

BREAK AND CONTINUE STATEMENT

Looping constructs allow programmers to repeat tasks efficiently.

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.

Ex1:Program to demonstrate working of nested for loops

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

You might also like