FLOW OF CONTROL- CHAPTER 4

Type of statement in Python:-

Statements are the instructions given to the computer to perform any kind of action.

Type of statements:-

(1)Empty statements (ii) Simple statement (iii) Compound statement

Empty statement :-

The statement which does nothing . In python pass statement is an empty statement. Whenever python encounter a pass statement , it does nothing and move to next statement.

It is useful when language require presence of statement but logic of the program does not require any statement.

Simple statement:-Any single executable statement is a simple statement. E.g.

Name=input(“Enter any name “) or

print(Name);

Compound statement:-A compound statement represent a group of statement executed as a unit. E.g.

<Compound Statement header>:

<indented body containing multiple lines>

Header line: It begin with a keywords and ends with a colon.

A body:It contain one or more lines indented inside the header at same level.

STATEMENT FLOW OF CONTROL:-

(a)Sequence:- The sequence construct means the statements are being executed sequentially(one by one).

(b)Selection:- The selection construct means execution of statement depends on condition test. If condition is True one course of action is followed otherwise another course of action is followed.

(c)Iteration(Loop):- Theiterationconstruct means repetitionof statement depending upon condition test.Till the time a condition is True a set of statement is repeated again and again . as soon as condition become false the repetition stops.

Conditional Statements

Control statements are used to control the flowof execution depending upon the specified condition/logic.

There are three types of control statements.

1. Decision Making Statements

2. Iteration Statements (Loops)

3. Jump Statements (break, continue, pass)

Decision making statement used to control theflow of execution of program depending upon condition.

There are three types of decision making statement.

1. if statements

2. if-else statements

3. Nested if-else statement

1. if statements

An if statement is a programming conditional statement that, if proved true, performs a function or displays information.

if statements

Syntax:

if(condition):

statement

[statements]

e.g.1

marks=int(input(“Enter Marks “))

if marks>=33:

print(“Congratulation”)

print(“You are Pass”)

print(“End of Program”)

Note:To indicate a block of code in Python, you must indent each line of the block by the same amount. In above e.g. both print statements are part of if condition because of both are at same level indented but not the third print statement.

Eg.2.

Using logical operator in if statement

x=1 y=2

if(x==1 and y==2):

print(‘condition matching the criteria')

Output :-

condition matching the criteria

eg.3.

a=100

if not(a == 20):

print('a is not equal to 20')

Output :-

a is not equal to 20

2. if-else Statements

If-else statement executes some code if the test expression is true

(nonzero) and some other code if the test expression is false.

Syntax:

if(condition):

statements

else:

statements

e.g.

marks= int(input("Enter Marks "))

if marks>=33:

print("You are Pass ")

else:

print("You are Fail ")

print("End of Program ")

3. Nested if-else statement

If there is an if statement or if…else statement inside if body or else body then it is known as nested if statement. It allows you to check for multiple test expressions and execute different codes for more than two conditions.

3. Nested if-else statement

Syntax

If (condition):

statements

elif (condition):

statements

else:

statements

E.G.

num = float(input("Enter a number: "))

if num == 0:

print("No is Zero")

elif num>0

print("Positive number")

else:

print("Negative number")

OUTPUT

Enter a number: 5

Positive number

Iteration statements(loop)

Iteration statements(loop) are used to execute a blockof statements as long as the condition is true.Loops statements are used when we need to run same code again and again.

Loop Control elements:

(a)Initialization:- Before entering in a loop variable must be initialized It is the first value from where we start a loop.

(b)Test Expression-: It is the condition test whose truth value decides whether the loop body will be executed or not.

(c)Body of the loop: It is the statements that are executed repeatedly until the condition is true.

(d)Update Expression:- The update expression change the value of a loop variable.

Counting loop:- The loop which repeat certain no of time eg for Loop

Conditioning loop:- The loop which repeat until certain condition is true. Eg while loop

Python Iteration (Loops) statements are of three type :-

1. While Loop

2. For Loop

3. Nested For Loops

1. While Loop

It is used to execute a block of statement as long as agiven condition is true. And when the condition becomefalse, the control will come out of the loop. Thecondition is checked
Syntax

while (condition):
statements

x = 1

while (x <= 5):

print(x)

x = x + 1

print(“Loop Over”)

Output

1

2

3

4

5

Loop Over

While Loop With else clause

Both the loop in python have else clause which is different from else of if ..else statement . The else of loop execute only when loop ends normally.

e.g.

x = 1

while (x < 3):

print('inside while loop value of x is ',x)

x = x + 1

else:

print('inside else value of x is ', x)

Output

inside while loop value of x is 1

inside while loop value of x is 2

inside else value of x is 3

range() Function Parameters

The range function is used in Loop. It is used to generate a list of value which is special sequence type.

Parameters:-

start: Starting number of the sequence.

stop: Generate numbers up to, but not including this number. If stop value is n then it run upto n-1

step(Optional): Determines the increment between each numbers in the sequence.

Eg.

Range(10)= 0,1,2,3,4,5,6,7,8,9,

Range(12,18)= 12,13,14,15,16,17

Range(1,11,2)= 1,3,5,7,9

Operator in and not in

The in operator is use with range function:-

To check whether a value is contained inside the list

3 in [1,2,3,4] = true

5 in [1,2,3,4]= False

5 not in [1,2,3,4]= True

‘a’ in “trade”= True

“ash’ in “trash”= True

2. For Loop continue

2. For Loop

It is used to iterate over items of any sequence, suchas a list or a string.

Syntax

for val in sequence:

statements

e.g1.

for i in range(1,5,1):

print(i)

Output

1

2

3

4

Eg.2.

for i in range(5,3,-1):

print(i)

Output

5

4

Eg.3.

for a in [1,4,7]:

print(a*a)

1

16

49

Eg

for ch in "kvk":

print(ch)

k

v

k

For Loop With Else

e.g.

for i in range(1, 4):

print(i)

else:

print("End of loop")

Output

1

2

3

End of loop

Nested For Loop

e.g.

for i in range(1,3):

for j in range(1,11):

k=i*j

print (k, end=' ')

print()

Output

1 2 3 4 5 6 7 8 9 10

2 4 6 8 10 12 14 16 18 20

e.g.

for i in range(1,5):

for j in range(1,i):

print (“*”, end=' ')

print()

Output

*

**

***

****

3. Jump Statements

Jump statements are used to transfer the control from one location to another.

There are three types of jump statements used in python.

1.break

2.continue

3.pass

1.break

It is used to terminate the loop. It enable a program to skip over a part of the code.

for val in range(1,10,1):

if val == 5:

break

print(val)

print("The end")

Output

1

2

3

4

The end

2.continue

It is used to skip all the remaining statements in the loop and move controls back to the next iteration (repetition) of the loop. e.g.

for val in range(1,10,1):

if val == 5:

continue

print(val)

Output

It will print 1 to 10 except 5.

3. pass Statement

This statement does nothing. It can be used when a statement is required syntactically but the program requires no action.

Use in loop

if (marks>=33):

pass

else:

print(“Sorry! You are Fail “)