0% found this document useful (0 votes)
9 views21 pages

Decision Control Statements 1690351742158

The document provides an overview of control statements in Python, detailing their types: sequential, selection, and iterative control. It includes examples of various control structures such as if statements, loops (while and for), and special statements like break, continue, and pass. Additionally, it explains the use of else statements with loops and the range() function for generating sequences.

Uploaded by

sanzy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views21 pages

Decision Control Statements 1690351742158

The document provides an overview of control statements in Python, detailing their types: sequential, selection, and iterative control. It includes examples of various control structures such as if statements, loops (while and for), and special statements like break, continue, and pass. Additionally, it explains the use of else statements with loops and the range() function for generating sequences.

Uploaded by

sanzy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Decision Control

Statements

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS


RESERVED.
Control Statements

A control statement is a statement that determines the control flow of a set of


instructions, i.e., it decides the sequence in which the instructions in a program are to
be executed.

Types of Control Statements —


• Sequential Control: A Python program is executed sequentially from the first line of
the program to its last line.
• Selection Control: To execute only a selected set of statements.
• Iterative Control: To execute a set of statements repeatedly.

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


If Statement

Example:

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS


RESERVED.
Program to determine the character enter by
user
char = input(“Enter any character”)
if(char.isalpha()):
print(“It is a character”)
if(char.isdigit()):
print(“It is a digit”)
if(char.isspace()):
print(“It is a space”)
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED. 4
If-Else Statement

Example:

Program to determine the number is odd or even


5

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS


RESERVED.
Nested if Statements
A statement that contains other statements is called a compound statement. To
perform more complex checks, if statements can be nested, that is, can be placed one
inside the other. In such a case, the inner if statement is the statement part of the
outer one. Nested if statements are used to check if more than one conditions are
satisfied.
Example:

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS


RESERVED.
If-elif-else Statement
Python supports if-elif-else statements to test additional conditions apart from the
initial test expression. The if-elif-else construct works in the same way as a usual if-
else statement. If-elif-else construct is also known as nested-if construct.

Example:

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS


RESERVED.
While Loop

Example:

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


Program to calculate the sum of numbers from m
to n
m=int(input(“Enter the value of m”)
n=int(input(“Enter the value of n”)
s=0
while(m<=n):
s=s+m
m=m+1
print(“sum=“,s)
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED. 9
Program to read the numbers until -1 is
encountered. Count the positive, zeros and
negatives entered by user

Program to find the given number is Armstrong or


not

Program to enter the number and find the sum of


digits

Program to print the countdown of a number


© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED. 10
For Loop
For loop provides a mechanism to repeat a task until a particular condition is True. It is
usually known as a determinate or definite loop because the programmer knows exactly
how many times the loop will repeat.
The for...in statement is a looping statement used in Python to iterate over a sequence of
objects.

11

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


For Loop and Range() Function
The range() function is a built-in function in Python that is used to iterate over a
sequence of numbers. The syntax of range() is range(beg, end, [step])
The range() produces a sequence of numbers starting with beg (inclusive) and ending
with one less than the number end. The step argument is option (that is why it is placed
in brackets). By default, every number in the range is incremented by 1 but we can
specify a different increment using step. It can be both negative and positive, but not
zero.
Examples:

12

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


Range() Function
If range() function is given a single argument, it produces an object with values from 0 to
argument-1. For example: range(10) is equal to writing range(0, 10).
• If range() is called with two arguments, it produces values from the first to the second.
For example, range(0,10).
• If range() has three arguments then the third argument specifies the interval of the
sequence produced. In this case, the third argument must be an integer. For example,
range(1,20,3).
Examples:

13

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


Condition-controlled and Counter-controlled Loops

14

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


Program to print the multiplication table of any
number from user

Program to print the numbers of a range m to n


and classify them as odd or even

Program to display the sum of series –


(1,1/2,…1/n)

Program to display the sum of series – (1/2,2/3,…


n/(n+1))
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED. 15
Nested Loops
Python allows its users to have nested loops, that is, loops that can be placed inside
other loops. Although this feature will work with any loop like while loop as well as for
loop.
A for loop can be used to control the number of times a particular set of statements will
be executed. Another outer loop could be used to control the number of times that a
whole loop is repeated.
Example:
Loops should be properly indented to identify which statements are contained within
each for statement.

16

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS


RESERVED.
Program to display the following patterns

* 1 1 1
** 1 2 2 2 121
*** 1 23 3 33 12321
**** 1 234 4 444 123432
***** 1 2345 5 5555 1
1234543
21
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED. 17
The Break Statement
The break statement is used to terminate the execution of the nearest enclosing loop in
which it appears. The break statement is widely used with for loop and while loop. When
compiler encounters a break statement, the control passes to the statement that follows
the loop in which the break statement appears.
Example:

18

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS


RESERVED.
The Continue Statement
Like the break statement, the continue statement can only appear in the body of a loop.
When the compiler encounters a continue statement then the rest of the statements in
the loop are skipped and the control is unconditionally transferred to the loop-
continuation
Example: portion of the nearest enclosing loop.

19

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS


RESERVED.
The Pass Statement
Pass statement is used when a statement is required syntactically but no command or
code has to be executed. It specified a null operation or simply No Operation (NOP)
statement. Nothing happens when the pass statement is executed.
Difference between comment and pass statements In Python programming, pass is a
null statement. The difference between a comment and pass statement is that while the
interpreter ignores a comment entirely, pass is not ignored. Comment is not executed
but pass statement is executed but nothing happens.
Example:

20

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


The Else Statement Used With Loops

Unlike C and C++, in Python you can have the else statement associated with a loop
statements. If the else statement is used with a for loop, the else statement is executed
when the loop has completed iterating. But when used with the while loop, the else
statement is executed when the condition becomes false.
Examples:

21

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.

You might also like