📖 generic · 12th TN - English Medium · COMPUTER SCIENCE · Page 73question

CONTROL STRUCTURES · Part 5

Chapter 2: 3 · COMPUTER SCIENCE

control variable i gets updated as i=i+ (this can also be written as i += using shorthand assignment operator). When i becomes , the condition is returned as False and this will terminate the loop. Note print can have end, sep as parameters. end parameter can be used when we need to give any escape sequences like ‘\t’ for tab, ‘\n’ for new line and so on.

sep as parameter can be used to specify any special characters like, (comma) ; (semicolon) as separator between values (Recall the concept which you have learnt in previous chapter about the formatting options in print()). Note Following is an example for using else part within while loop. i= # intializing part of the control variable while (i<= ): # test condition print (i,end='\t') # statements - block i=i+ # Updation of the control variable else: print ("\nValue of i when the loop exit ",i) Output: Value of i when the loop exit Example . : program to illustrate the use of while loop - with else part (ii) for loop The for loop is usually known as a definite loop, because the programmer knows exactly how many times the loop will be executed.

Syntax: for in sequence: statements-block [else: # optional block statements-block ] The for .... in statement is a looping statement used in Python to iterate over a sequence of objects, i.e., it goes through each item in a sequence. Here the sequence is the collection of ordered or unordered values or even a string. 12th Computer Chapter - - Control Structures The control variable accesses each item of the sequence on each iteration until it reaches the last item in the sequence.

for x in "Hello World": print(x, end=' ') Output: H e l l o W o r l d Example . (a) for x in ( , , , , ): print("Hello World") Output: Hello World Hello World Hello World Hello World Hello World Example . (b) In the above example . (a), we have created a string “Hello World”,

Related topics

Have a question about this topic?

Get an AI answer grounded in your actual textbook — with the exact page reference.

Ask AI about this topic →