Python Programming – Conditional and Iterative Statements

You can learn about Control Statements in Python Programs with Outputs helped you to understand the language better.

Python Programming – Conditional and Iterative Statements

Control statements define the direction of flow according to which execution of a program should take place. Statements that are executed one after the other or the statements that are executed sequentially are the normal flow-of-control statements in a program. Normally, the Python program begins with the first statement and is executed till the end (Sequence construct). [See Figure 5.1.] Control statements also implement decisions and repetitions in programs.

Python Programming – Conditional and Iterative Statements chapter 5 img 1

Construct Control statements are of the following two types:

  • Conditional branching or statement
  • Looping or iteration

In Conditional branching, a program decides whether or not to execute the next statement(s), based on the resultant value of the current expression. For example, go to IInd year if you pass Ist year exams, else repeat Ist year (See Figure 5.2).
In Looping, a program executes a set of instructions repeatedly till the given condition is true. Looping, for example, adds 1 to the number x till the current value of x becomes 100. It is also called iteration. Iteration is a process of executing a statement or a set of statements repeatedly until the specified condition is

Python Programming – Conditional and Iterative Statements chapter 5 img 2

Jump statement

All the control structures are explained further in the following sections.

Those statements which transfer which program con-trial, unconditionally within the program, from one place to another are called jump statements. These statements included the following:

(a) break statements
(b) continue statements
(c) pass statements

Break Statement

The break statement causes an immediate exit from the innermost loop structure. it transfers control out of while or for structure. It is mostly required when, due to some external condition, you need to exit from a loop. If you need to come out from a running program immediately without letting it perform any further operation in a loop, then you can break the statement. the break statement exits the current loop (for or while) and transfers control to the statement immediately following
the control structure.
Figure 5.51 shows the use of the break statement.

Example
Demo of a break statement.

# Demo of using break statement
count = 1
while(count > 0):
print (“Count :11, count) count += 1
#Breaks out of the while loop when counting>5
if(count>5):
break
print(“End of Program”)RUN
>>>
Count : 1
Count : 2
Count : 3
Count : 4
Count : 5
End of Program
>>>

Note that in the following example, the break statement causes the program to immediately exit the loop; so it will print COMP.

Example
Demo of a break statement.

# Demo of using break statement
for ch in ‘COMPUTER’:
if ch == ‘U’:
break
print(ch)RUN
>>>
C
0
M
P
>>>

pass STATEMENT

The pass statement in python is used when a statement is required syntactically, but you want any command or code to execute.
this sentiment is a null operation; nothing happens when it executes. The pass is also useful in places when your code will eventually go,
but it has not been written yet.
For example,

if ( condition ) :
pass

it can be used as a placeholder while you are writing code. For example, you may write an if statement and you want to try it, but you do not have the code for one of your blocks. considered the following:
name = input ( ‘ Enter your name: ‘)
Print (‘ welcome! ‘ )
e1if ( name== ‘ Remesh ‘ )
# not finished yet…
e1if ( name== ‘ Rohan ‘ )
print (‘ Access Denied ‘)
This code wlii not run because an empty block is illegal in python. To fix this simply add the pass statement to the middle block:
name=input(‘Enter your name: ‘)
if(name==’Ram’):
print(‘Welcome!’)
elif(name==’Ramesh’):
# Not finished yet…
pass
elif(name==’Rohan’):
print(‘Access Denied’)

Leave a Reply

Your email address will not be published. Required fields are marked *