Python Tutorials - While loop
- Meenakshi Sundaram
- Aug 29, 2019
- 2 min read

In this blog, we are going to see the loop in the python programming language. There are two primitive loops in python.
while
for
While loop:
While loop will repeat the statement until the condition is true. While loop requires a relevant indexing variable. We will declare 'i' here. Which we will set the values as 1.
Example:
i=1
while i<6:
print(i)
i+=1
Output:
1
2
3
4
5
Explanation:
Inline 1, we will assign the value 1 for i. Inline 2 we will use while keyword which defines that it executes the statement until it reaches the value before 6. Inline 3 will print the i.Inline 4, it will increase the value as 1,2,3 and so on until it reaches the condition.
Break Statement:
The break statement is used to break the loop even the while condition is true.
Example:
i=1
while i<6:
print(i)
if i==3:
break
i+=1 Output:
1
2
3 Continue statement:
Continue statement is used to break the current iteration and it will continue with the next one. Example:
i = 0
while i < 6:
i += 1
if i == 3:
continue
print(i) Output:
1
2
4
5
else statement:
Else statement in loop condition is used to execute the code block once when the condition is no longer true. Example:
i = 1
while i < 6:
print(i)
i += 1
else:
print("i is no longer less than 6") Output:
1
2
3
4
5
i is no longer less than 6
Comentários