class10pythonpptonloopsdifferenttypesansjumpstatement

samirparmar24 0 views 25 slides Oct 16, 2025
Slide 1
Slide 1 of 25
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25

About This Presentation

python


Slide Content

Loops in Python

What is a Loop In computer Programming, a Loop is used to execute a group of instructions or a block of code multiple times, without writing it repeatedly. The block of code is executed based on a certain condition. Loops are the control structures of a program.

The structure of a Loop can be virtually divided into two parts, namely: The control statement: It comprises the conditions that have to be met for the execution of the body of the Loop. For every iteration of the Loop, the conditions in the control statement have to be true. The body: It comprises the block of code or the sequence of logical statements that are to be executed multiple times. What is a Loop

What is a Loop When you use a Loop in your program, you will not have to write the block of code (written in the body of the Loop), over and over again in it. The block of code will be executed as many times as the control statement will hold true and the Loop will be terminated when the conditions in the control statement become false. If the conditions are not clearly defined in the control statement, the Loop will keep on executing. Such Loops are termed as infinite Loops . If no termination condition is provided in the control statement of a Loop, then it automatically becomes an infinite Loop.

What is a Loop

There are basically two types of Loops in most computer Programming languages, namely, Entry Controlled Loop In an entry controlled Loop, the control statement is written right at the beginning of the Loop. This type of Loop is also called a pre-checking Loop. The conditions in the control statements are checked at first, and only if the conditions are true, the body of the Loop is executed. If the condition turns out to be false, the lines of code in the body of the Loop will not be executed. Example: for loop & while loop. Types of Loops

Exit Controlled Loop In an exit controlled Loop, the control statement is written at the end of the Loop structure. The lines of codes in the body of the Loop are executed once before the condition is checked. Hence, this type of Loop is also called a post-checking Loop.  Example do… while loop Types of Loops

There are two types of Loops in Python, namely, f or Loop, and while Loop. When a Loop is written within another Loop, the control structure is termed as a nested Loop . Types of Loops

The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and stops before a specified number. Syntax range(start, stop, step) Start- Optional. An integer number specifying at which position to start. Default is 0 Stop- Required. An integer number specifying at which position to stop (not included). Step- Optional. An integer number specifying the incrementation. Default is 1 The range function

for loop A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string). for loopvar in range(argument/ aguments ): statements to be repeated for x in range(5): print(x, end=“ ”) Output: 0 1 2 3 4 for x in range(3, 6): print(x, end=“ ”) Output: 3 4 5 for x in range(3, 8, 2): print(x, end=“ ”) Output: 3 5 7 Examples :

for loop More Examples with list & string: fruits = ["apple", "banana", "cherry"] for x in fruits: print(x) Output: Apple Banana cherry s = "Geeks" for i in s: print( i ) Output: G e e k s

for loop WAP to display first ‘n’ numbers n=int(input(“Enter up to which no:”)) for i in range(n): print( i ) Output: If n is 5, then 1 2 3 4 5

for loop WAP to display all odd numbers below 20 for i in range(1,20,2): print( i , end=“ ”) Output: 1 3 5 7 9 11 13 15 17 19

for loop WAP to display all even numbers below 20 for i in range(2,20,2): print( i , end=“ ”) Output: 2 4 6 8 10 12 14 16 18

for loop WAP to display sum of first n numbers n=int(input(“Enter a number”) sum=0 for i in range(1,n+1): sum= sum+i print(sum)

for loop WAP to display factorial of n (product of first n numbers) n=int(input(“Enter a number”) product=1 for i in range(1,n+1): product=product* i print(product)

while loop A while Loop is an entry controlled Loop. The condition checking is done at the beginning of the Loop structure. The general syntax of the while Loop is given below. Initialize loop variable while(condition): Body of the Loop or statements to b e repeated increment/decrement loop variable

while loop The condition checking is done before the execution of the body of the while Loop. The block of code in the body of the While Loop is executed only if the condition is true. The body of the Loop gets executed as many times as the condition is true. After each iteration of the Loop, the control moves back to the condition checking part at the beginning of the While Loop. If the condition is not met, that is, if the Boolean expression in the braces (), turns out to be false, the while Loop is terminated.

while loop Example: n=5 i =1 while( i <=n): print ( i ) n+=1 Output: 1 2 3 4 5 In the given example, the variable ‘n’ is initialized as an integer, and its value is assigned as 5 and loop variable i is initialized as 1. Every time the condition ‘ i <=n’ is met the While Loop will be executed, and the value of i will be displayed on the screen. At every iteration, the value of i will be increased by 1. The Loop will be terminated when the value of ‘ i ’ becomes greater than 5. The above While Loop will display the numbers from 1 to 5.

break & continue statements break is used to exit a for loop or a while loop. Example : count = 0 while True: print(count, end=“ ”) count += 1 if count >= 5: break Output : 0 1 2 3 4 continue is used to skip the current block, and return to the "for" or "while" statement. Example : for x in range(10): if x % 2 == 0: continue print(x, end=“ ”) Output : 1,3,5,7,9

e lse with loop is used with both while and for loop. The else block is executed at the end of loop means when the given loop condition is false then the else block is executed. The else keyword in a for loop/while loop specifies a block of code to be executed when the loop is finished: Note: The else block will NOT be executed if the loop is stopped by a break statement. So let’s see the example of while loop and for loop with else below. else statements

else statements-examples for i in range(1, 4): print( i , end=“ ”) else: # Executed because no break in for print("No Break") Output : 1 2 3 No Break for i in range(1, 4):      print( i )      break else: # Not executed as there is a break      print("No Break") Output : 1

WAP to display first ‘n’ numbers. WAP to display odd numbers below ‘n’. WAP to display first ‘n’ odd numbers. WAP to display even numbers below ‘n’. WAP to display first ‘n’ even numbers. WAP to display the sum of first ‘n’ numbers. WAP to display the factorial of ‘n’. WAP to display all numbers in a range. Programs using loops

WAP to display numbers below ‘n’ in reverse order. WAP to reverse a numbers. WAP to find the sum of digits of a number. WAP to display the multiplication table of ‘n’ for ‘m’ rows. WAP to find whether the given number is a prime or not. WAP to find whether the given number is a perfect number or not. WAP to find whether the given number is an armstrong number or not. Programs using loops
Tags