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

Control Structures

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)
8 views

Control Structures

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/ 4

FLOW OF CONTROL

Control Structures:

In a program, statements may be executed sequentially, selectively or iteratively.

Types of Constructs :
1) Sequential : All the statements written in a program gets executed one after the other in a
sequential manner.

2) Selection : In Selection construct execution of statement(s) depends upon a condition test. If


the condition is true, statement or set of statements gets executed otherwise a different set of
statements or statement gets executed.

3) Iteration (Looping) : The iteration construct means repetition of a statement or set of


statements depending upon a condition test. Till the time, the condition is true, statements gets
repeated again and again. As soon as the condition becomes false, the repetition stops.

Selection Statements :

i) if Statement :

Syntax : if condition :
statement(s)
else :
statement(s)

e.g. n = int(input ("Enter number"))


if n > 0 :
print("Number is positive")
else :
print("Number is negative")

Statement(s) written in if part gets executed if the condition evaluates to true and the
statement(s) written in else part gets executed if the condition evaluates to false.

ii) if - elif -- else statement : is used when there is a need to check another condition in case the
test condition of if evaluates to false
.e.g. To find the grade of a student on the basis of percentage obtained.
To display dayname on the basis of day number.

Program

dayno = int(input("Enter day number : "))


if dayno == 1 :
print("Monday")
elif dayno == 2 :
print("Tuesday")
elif dayno == 3 :
print("Wednesday")
elif dayno == 4 :
print("Thursday")
elif dayno == 5 :
print("Friday")
elif dayno == 6 :
print("Saturday")
elif dayno == 7 :
print("Sunday")
else :
print("Wrong input")

Program : Read two numbers and an arithmetic operator and display the
computed result.

Iteration :
Iteration statements allow a set of statements to be performed repeatedly until a
certain condition is fulfilled.
or Iteration statements (loop) are used to execute a block of statements as long as
the condition is true.

Types of loops:
1) for (2) while

for loop works with range function.

range( ) function : contains three arguments i.e. starting value, ending value and
step value. By default step value is 1.
e.g. range( 1 , n):
will produce a list having values starting from 1,2,3… upto n-1.
Last value is one less than the specified end value.
range(1,9,2) : step value is 2. Values are 1,3,5,7.

range(5,1,-1) : will produce 5,4,3,2

range(5) :will produce 0,1,2,3,4. Here default start value is 0.

for loop : A for loop is used to repeatedly execute a statement or a set of statements
based on a condition. The loop terminates when the condition becomes false. A for
loop is considered deterministic because it is typically used for a fixed number of
iterations or repetitions
Syntax:
for val in range() :
statements

e.g.
Program to display even numbers and their sum in the range of 1 to 20.
sum = 0
for x in range(2,21,2) :
print(x)
sum = sum + x
print("Sum of even numbers = ", sum)

Output:
2
4
6
8
.
.
20
Sum of even numbers = 110

Sum of even numbers from 20 to 1:


sum = 0
for x in range(20,1, - 2) :
print(x)
sum = sum + x
print("Sum of even numbers = ", sum)
while loop : A while loop is also used to repeatedly execute a statement or a set of
statements based on a condition. The while loop is considered a non-deterministic
loop, meaning it can be used in situations where we know the termination condition
but do not know the exact number of repetitions required.

e.g. program to display the odd numbers and their sum in the range of 1 to 20.

sum = 0
num = 1
while num < 20 :
print(num)
sum = sum + num
num = num + 2
print(“Sum of odd numbers =”, sum)

You might also like