<<

Class-XI Computer Science

Iterative statements

Notes

In Python, the iterative statements are also known as looping statements or repetitive statements. The iterative statements are used to execute a part of the program repeatedly as long as a given condition is True.

Python provides the following iterative statements.

 while statement  for statement while statement In Python, the while statement is used to execute a set of statements repeatedly. In Python, the while statement is also known as entry control loop statement because in the case of the while statement, first, the given condition is verified then the execution of statements is determined based on the condition result. The general syntax of while statement in Python is as follows.

Syntax while condition: Statement_1 Statement_2 Statement_3 ... The execution flow of while statement is as shown in the following figure.

Flow chart of :

When we define a while statement, the block of statements must be specified using indentation only. The indentation is a series of white-spaces. Here, the number of white-spaces may variable, but all statements must use the identical number of white-spaces. Let's look at the following example Python code. Features of while loop

 Python while loop is used to repeat a block of code until the specified condition is False.  The while loop is used when we don’t know the number of times the code block has to execute.  We should take proper care in writing while loop condition if the condition never returns False, the while loop will go into the .  Every object in Python has a Boolean value. If the value is 0 or None, then the boolean value is False. Otherwise, the Boolean value is True.  We can define an object boolean value by implementing __bool__() function.  We use the reserved keyword – while – to implement the while loop in Python.  We can terminate the while loop using the break statement.  We can use continue statement inside while loop to skip the code block execution.  Python supports nested while loops.

Example: count = int(input('How many times you want to say "Hello": ')) i = 1 while i <= count: print('Hello') i += 1 print('Job is done! Thank you!!')

while statement with 'else' clause in Python In Python, the else clause can be used with a while statement. The else block is gets executed whenever the condition of the while statement is evaluated to false. But, if the while loop is terminated with break statement then else doesn't execute.

for statement in Python In Python, the for statement is used to iterate through a sequence like a list, a tuple, a set, a dictionary, or a string. The for statement is used to repeat the execution of a set of statements for every element of a sequence. The general syntax of for statement in Python is as follows.

Syntax for in : Statement_1 Statement_2 Statement_3 ... In the above syntax, the variable is stored with each element from the sequence for every iteration.

Flow chart of

Python code to illustrate for statement with List

# Python code to illustrate for statement with List my_list = [1, 2, 3, 4, 5] for value in my_list: print(value) print('Job is done!')

Python code to illustrate for statement with Tuple

# Python code to illustrate for statement with Tuple my_tuple = (1, 2, 3, 4, 5) for value in my_tuple: print(value) print('Job is done!') for statement with 'else' clause in Python In Python, the else clause can be used with a for a statement. The else block is gets executed whenever the for statement is does not terminated with a break statement. But, if the for loop is terminated with break statement then else block doesn't execute. # Here, else block is gets executed because break statement does not executed count = int(input('How many times you want to say "Hello": ')) i = 1 while i <= count: if count > 10: print('I cann\'t say more than 10 times!') break print('Hello') i += 1 else: print('This is else block of while!!!') print('Job is done! Thank you!!')

# Here, else block is gets executed because break statement does not executed for item in 'Python': if item == 'x': break print(item) else: print('else block says for is successfully completed!') print('Job is done!!') Iteration statements can be controlled by the following keyword:

• break • continue • Pass In Python, break and continue statements can alter the flow of a normal loop.

Break Statements

The break statement terminates the loop containing it. Control of the program flows to the statement immediately after the body of the loop. Syntax: break;

Flow Chart for Break Statements

The working of break statement in for loop and while loop is shown below.

Example: Use of break statement inside the loop for val in "string": if val == "i": break print(val) print("The end") for val in "string": if val == "i": break print(val) print("The end")

for i in [12, 16, 17, 24, 29]: if i % 2 == 1: # if the number is odd break # immediately exit the loop print(i) print("done")

This print:

12 16 done

Continue Statement:

The continue statement is used to skip the rest of the code inside a loop for the current iteration only. Loop does not terminate but continues on with the next iteration. Syntax:

Continue;

Flow chart for Continue Statements

The working of continue statement in for and while loop is shown below

Example: for val in "string": if val == "i": continue print(val) print("The end")

# Program to show the use of continue statement inside loops for val in "string": if val == "i": continue print(val) print("The end") pass Statement:

In Python programming, pass is a null statement. Syntax: pass; Example: pass_test.py sequence = {'p', 'a', 's', 's'} for val in sequence: pass

RANGE() FUNCTION:  range function is used to iterate the elements in sequence manner  range function is used in for loops  range function makes programmer to index free while iterating the loops  RANGE() FUNCTION ARGUMENTS: range () function has two set of arguments. They are,

1. SINGLE ARGUMENT RANGE FUNCTION: SYNTAX: range (stop)

STOP: To generate the numbers in sequence up to “stop-1” numbers. It will start from zero.

EX: range(5)=[0,1,2,3,4] 2. TWO ARGUMENT RANGE FUNCTION: SYNTAX: range (start, stop)

START: Generating the numbers starting from “start”

STOP: To generate the numbers in sequence up to “stop-1” numbers.

3. THREE ARGUMENT RANGE FUNCTION SYNTAX: range([start], stop [, step])

START: Generating the numbers starting from “start”

STOP: To generate a number in sequence up to “stop-1” numbers.

STEP: difference between each number in the sequence

NESTED LOOP:

Python allows to use one loop inside another loop. Following section shows few examples to illustrate the concept. Syntax for iterating in sequence: for iterating in sequence: statements(s) statements(s) The syntax for a nested while loop statement in Python programming language is as follows − while expression: while expression: statement(s) statement(s) final note on loop is that you can put any type of loop inside of any other type of loop. For example a for loop can be inside a while loop or vice versa.

Example of Nested Loop

FLOW CHART OF NESTED LOOP

EXAMPLE

i = 2 while (i < 100): j = 2 while (j <= (i/j)): if not(i%j): break j = j + 1 if (j > i/j) : print i, " is prime" i = i + 1 print "Good bye!"

PRACTICE QUESTIONS BASED ON LOOPS: Q1. Program to print table of a number

N=int(input(“enter the number”)) for I in range (1,11): print(n, “X”, I , “=”, (n*I) )

Q2. Program to find sum of digits of an integer number, input by the user. num=int(input(“Enter number : ”) ssum =0 nnum=num while(nnum != 0): Digit =nnum %10 nnum=nnum //10 ssum += digit print(“sum of the digit of the number”,ssum)

Q3. Program to check whether an input number is a palindrome or not. num= int (input(“enter number :”)) rev =0 nnum=num while(nnum !=0) digit = nnum % 10 nnum=nnum // 10 rev= rev *10 + digit if rev== num: print(num, “It is a palindrome ”) else: print(num, “is not a palindrome”) Q4. Program to generate the sequence: -5,10, -15,20, -25 ------up to n terms. n =int(input(“enter limit : ” ) sign = -1 for i in range(5, n, 5): term = i*sign sign=sign * -1 print(term, end = “ ”) Q5. Program to find maximum and minimum of five numbers entered by the user. n=int(input(“enter a number : ”) mn, mx = n , n for i in range(4) : n=int(input(“enter a number : “)) if mx n: mn = n print(“maximum number is : ”,mx) print(“minimum number is :” ,mn)

Q6. Write a program to print the following pattern: 1 2 3 4 5 1 2 3 4 1 2 3 1 2 1 n=5 s=0 for i in range(n,0,-1): for j in range(1, s+1): print(end= ‘ ’) for j in range(1, i+1): print(j, end= ‘ ’) s+ = 2 print()

MEMORY MAP