[go: up one dir, main page]

0% found this document useful (0 votes)
32 views17 pages

While Else Jump

Uploaded by

itssohail099
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)
32 views17 pages

While Else Jump

Uploaded by

itssohail099
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/ 17

Iteration Statements (Loops)

1. While Loop
It is used to execute a block of statement as long as a
given condition is true. And when the condition become
false, the control will come out of the loop. The condition
is checked every time at the beginning of the loop.
Syntax
while (condition):
statement
[statements]
e.g.
x=1 Output
while (x <= 4): 1
2
print(x) 3
x=x+1 4
Iteration Statements (Loops)
While Loop continue
While Loop with Else
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
*Write a program in python to find out the factorial of a given number
Iteration Statements (Loops)
2. For Loop continue
For Loop With Else
e.g.
for i in range(1, 4):
print(i)
else: # Executed because no break in for
print("No Break")

Output
1
2
3
No Break
Iteration Statements (Loops)

2. For Loop continue


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
Iteration Statements (Loops)

2. For Loop continue


Factorial of a number
factorial = int(input(‘enter a number’))

# check if the number is negative, positive or zero


if num < 0:
print("Sorry, factorial does not exist for negative
numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
for i in range(1,num + 1):
factorial = factorial*i
print("The factorial of",num,"is",factorial)
Iteration Statements (Loops)

2. For Loop continue


Compound Interest calculation
n=int(input("Enter the principle amount:"))
rate=int(input("Enter the rate:"))
years=int(input("Enter the number of years:"))

for i in range(years):
n=n+((n*rate)/100)
print(n)
Iteration Statements (Loops)
3. Jump Statements

Jump statements are used to transfer


the program's control from one location to another. Means
these are used to alter the flow of a loop like - to skip a
part of a loop or terminate a loop

There are three types of jump statements used in python.


1. break
2. continue
3.pass
Iteration Statements (Loops)
1. break
it is used to terminate the loop.
e.g.
for val in "string":
if val == "i":
break
print(val)

print("The end")

Output
s
t
r
The end
Iteration Statements (Loops)
2. continue
It is used to skip all the remaining statements
in the loop and move controls back to the top of
the loop.
e.g.
for val in "init":
if val == "i":
continue
print(val)
print("The end")

Output
n
t
The end
Iteration Statements (Loops)
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
while True:
pass # Busy-wait for keyboard interrupt (Ctrl+C)
In function
It makes a controller to pass by without executing any code.
e.g.
def myfun():
pass #if we don’t use pass here then error message will be shown
print(‘my program')

OUTPUT
My program
Iteration Statements (Loops)
3. pass Statement continue
e.g.
for i in 'initial':
if(i == 'i'):
pass
else:
print(i)

OUTPUT
n
t
a
L
NOTE: continue forces the loop to start at the next iteration
while pass means "there is no code to execute here" and
will continue through the remainder or the loop body.

You might also like