0% found this document useful (0 votes)
3 views3 pages

L-8 Loops in Python - Copy

The document outlines control structures in Python, focusing on loops and jump statements. It explains the syntax and differences between for and while loops, as well as the functions of break and continue statements. Additionally, it provides examples for clarity and suggests completing specific programming exercises from Chapter 7.

Uploaded by

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

L-8 Loops in Python - Copy

The document outlines control structures in Python, focusing on loops and jump statements. It explains the syntax and differences between for and while loops, as well as the functions of break and continue statements. Additionally, it provides examples for clarity and suggests completing specific programming exercises from Chapter 7.

Uploaded by

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

L-7

Control Structures in Python

Q1. Write the syntax of for loop.

Ans- for<variable> in <iterator>:

Statements

Q3. What is the use of Jump Statement?

Ans- Sometimes, there is a situation when the control of the program needs to be transferred out of the
loop body,even if all the values of the iterations of the loop have not been completed.For this
purpose,jumping statements are used in Python.

Q4. Differentiate between for and while loop.

Ans- 1.The for statement is used to repeat an instruction or a set of instructions a fixed number of times
whereas, the while statement is used to repeat a set of instructions until a condition evaluates to
true.When the condition becomes false, the control comes out of the loop.

2. Syntax:

For loop

for<variable> in <iterator>:

Statements

While loop

While(test expression):

Statements

3. Example- To print number from 1 to 5

for Loop

for a in range(1,6):

print(a)

While loop

i=1

whilei<=5:

print(i)

i=i+1

else:

print(“The While loop ends here”)


Q5. Distinguish between break and Continue statement.

Ans- 1. The break is a keyword in Python which is used for bringing the program control out of the
loop.The break statement halts the execution of a loop and program flow switches to the statement
after the loop, whereas incontinue statement, control of the program jumps to the beginning of the loop
for next iteration, skipping the execution of rest of the statement of the loop for the current iteration.

2. Syntax of break statement:

# loop statement

break

Syntax of continue statement

# loop statements

continue

# the code to be skipped

3. Example of break statement

for number in range (0,12):

if number == 7:

break

print("Number is: ", number)

print(‘Out of loop’)

Output

Number is: 0

Number is: 1

Number is: 2

Number is: 3

Number is: 4

Number is: 5

Number is: 6

Out of loop

Example of Continue Statement

for number in range (0,12):

if number == 7:

continue

print("Number is: ", number)


print(‘Out of loop’)

Output-

Number is: 0

Number is: 1

Number is: 2

Number is: 3

Number is: 4

Number is: 5

Number is: 6

Number is: 8

Number is: 9

Number is: 10

Number is: 11

Out of loop

Do Program 1,2,3,4,7,9,10,11,13,14,15 of Ch-7

You might also like