Flow of Control

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

1. Explain in detail about Decision making and branching in python?

DECISION MAKING & BRANCHING


Decision making is about deciding the order of execution of statements based on
certain conditions. Decision structures evaluate multiple expressions which
produce TRUE or FALSE as outcome.

There are three types of conditions in python:

1. if statement
2. if-else statement
3. elif statement

1. if statement: It is a simple if statement. When condition is true, then code which


is associated with if statement will execute.
Example:
a=40
b=20
if a>b:
print(“a is greater than b”)

2. if-else statement: When the condition is true, then code associated with if
statement will execute, otherwise code associated with else statement will execute.
Example:
a=10
b=20
if a>b:
print(“a is greater”)
else:
print(“b is greater”)
3. elif statement: It is short form of else-if statement. If the previous conditions were
not true, then do this condition”. It is also known as nested if statement.
Example:
a=input(“Enter first number”)
b=input(“Enter Second Number:”)
if a>b:
print(“a is greater”)
elif a==b:
print(“both numbers are equal”)
else:
print(“b is greater”)

LOOPS in PYTHON

Loop: Execute a set of statements repeatedly until a particular condition is satisfied.

There are two types of loops in python:


1. while loop
2. for loop

i while loop: With the while loop we can execute a set of statements as long as a
condition is true. It requires to define an indexing variable.
Example: To print table of number 2
i=2
while i<=20:
print(i)
i+=2

ii.for loop : The for loop iterate over a given sequence (it may be list, tuple or string).
Note: The for loop does not require an indexing variable to set beforehand, as the for
command itself allows for this. primes = [2, 3, 5, 7]
for x in primes:
print(x)

2. The range( ) function:


it generates a list of numbers, which is generally used to iterate over with for loop.
range( ) function uses three types of parameters, which are:

 start: Starting number of the sequence.


 stop: Generate numbers up to, but not including last number.
 step: Difference between each number in the sequence.

Python use range( ) function in three ways:


a. range(stop)
b. range(start, stop)
c. range(start, stop, step)

Note:

 All parameters must be integers.


 All parameters can be positive or negative.

3. Explain in detail about break and continue statement

JUMP STATEMENTS:
There are two jump statements in python:

1. break
2. continue

1. break statement : With the break statement we can stop the loop even if it is true.
Example:
Note: If the break statement appears in a nested loop, then it will terminate the very loop
it is in i.e. if the break statement is inside the inner loop then it will terminate the inner
loop only and the outer loop will continue as it is.

2. continue statement : With the continue statement we can stop the current
iteration, and continue with the next iteration.
Example:

You might also like