#PYTHON(DAY15)
loop statements
Looping means repeating something over and over until a particular condition is satisfied.
for loop
- for loop keeps executing the block of code till it reaches the end of the iterator
- iterator: is an object which has some value, and next time we execute the loop, this value will be taken/considered
syntax
for var_name in iterator:
block of code
x = [1,2,3]
for ele in x:
print(ele)
print('outside the loop')
And the output is:
1
2
3
outside the loop
while
- while is a ‘looping statement’ in python which uses ‘c.e’ to determine whether to execute the loop or not
- if the given c.e turns out to be ‘True’, the code within the ‘while’ block will be executed
- in order to exit the loop, we need to alter or change the o/p of the given c.e to ‘False’
- if not, the code within the ‘while’ block will be executed till the memory is exhausted
- we can change the given c.e o/p to ‘False’ by using a statements like ‘assignment’, ‘if-block’ or a ‘control-flow’
syntax
while expression:
statement(s)
Note: if our while loop doesnot have the statement that alters the o/p of ‘conditional execution’ to ‘False’, the loop keeps running till the memory runs out
Example
x = 1
while x <=10:
print(x, end =',')
x+=1
And the output is:
1,2,3,4,5,6,7,8,9,10,
Using else statement with While Loop in Python
The else clause is only executed when your while condition becomes false. If you break out of the loop, or if an exception is raised, it won’t be executed.
Syntax of While Loop with else statement:
while condition:
# execute these statements
else:
# execute these statements
Example
count = 0
while (count < 3):
count = count + 1
print("Hello world")
else:
print("In Else Block")
And the output is:
Hello world
Hello world
Hello world
In Else Block
loop control statements
- these statements are used to alter the way execution of our loop statements.
- there are three control flow statements
- ‘pass’: it acts like a place holder for your code
- ‘continue’: it terminates the current loop iteration and runs the loop with next value
- ‘break’: it abruptly ends and exits the loop.
Pass Statement
We use pass statement in Python to write empty loops. Pass is also used for empty control statements, functions and classes.
if True:
pass # for escaping the value
Continue Statement
the continue statement in Python returns the control to the beginning of the loop.
for letter in 'geeksforgeeks':
if letter == 'e' or letter == 's':
continue
print('Current Letter :', letter)
And the output is:
Current Letter : g
Current Letter : k
Current Letter : f
Current Letter : o
Current Letter : r
Current Letter : g
Current Letter : k
Break Statement
The break statement in Python brings control out of the loop.
for letter in 'geeksforgeeks':
# break the loop as soon it sees 'e'
# or 's'
if letter == 'e' or letter == 's':
break
print('Current Letter :', letter)
And the output is:
a = 0
b = 3
while a <=b:
print(a)
while b>=a:
print(b)
b-=1
a+=1
And the output is:
0
3
2
1
0
No comments:
Post a Comment