Skip to content

Commit 3161ed6

Browse files
authored
Update WhileLoop.md
1 parent 6d5625a commit 3161ed6

1 file changed

Lines changed: 30 additions & 24 deletions

File tree

Basic/WhileLoop.md

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
## While Loop :performing_arts:
1+
## Loop Statement : While Loop
22

3-
### Introduction :barber:
3+
### Introduction
44

55
- While loops repeat as long as a certain boolean condition is met.
66
- The while loop in Python is used to iterate over a block of code as long as the test expression (condition) is true.
7-
- Mostly while loop is used when we want to iterate a dynamically number of times repeation.
7+
- Mostly while loop is used when we want to iterate a dynamical number of times repetition of an object.
88
- The loop iterates while the condition is true.
99
- When the condition becomes false, program control passes to the line immediately following the loop.
1010
- Iteration means executing the same block of code over and over, potentially many times.
@@ -18,10 +18,11 @@ Example:
1818
```python
1919
a=10
2020
while a!=0:
21-
print(a)
22-
a-=1
23-
'''
24-
#Result
21+
print(a)
22+
a-=1
23+
```
24+
Output:
25+
```
2526
10
2627
9
2728
8
@@ -32,58 +33,63 @@ while a!=0:
3233
3
3334
2
3435
1
35-
'''
3636
```
37+
3738
### Interruption :traffic_light: of Loop :loop: Iteration
3839
- In each example you have seen so far, the entire body of the while loop is executed on each iteration.
3940
- Python provides two keywords that terminate a loop iteration prematurely:
40-
1. break
41-
2. continue
41+
```
42+
1. break
43+
2. continue
44+
```
4245

43-
### 1.break :construction:
44-
- Immediately terminates a loop :loop: entirely.
46+
### 1.break
47+
- Immediately terminates a loop entirely.
4548
- Program execution proceeds to the first statement following the loop body.
4649

4750
Example:
4851
```python
49-
n = 5
52+
n= 5
5053
while n > 0:
5154
n -= 1
5255
if n == 2:
5356
break
5457
print(n)
5558
print('Loop ended.')
56-
'''
57-
Result
59+
```
60+
61+
Output:
62+
```
5863
4
5964
3
6065
Loop ended.
61-
'''
6266
```
67+
6368
when n becomes 2 the break statement is executed.loop is terminated completely,
6469
and program execution jumps to the **print()** statement on line 7.
6570

66-
### 2. continue :fuelpump:
71+
### 2. continue
6772
- Immediately terminates the current loop iteration.
6873
- Execution jumps to the top of the loop, and the controlling expression is re-evaluated to determine whether the loop will execute again or terminate.
6974

7075
Example:
7176
```python
7277
n = 5
7378
while n > 0:
74-
n -= 1
75-
if n == 2:
76-
continue
77-
print(n)
79+
n -= 1
80+
if n == 2:
81+
continue
82+
print(n)
7883
print('Loop ended.')
79-
'''
80-
#Result
84+
```
85+
86+
Output
87+
```
8188
4
8289
3
8390
1
8491
0
8592
Loop ended.
86-
'''
8793
```
8894
when n is 2, the continue statement causes termination of that iteration.
8995
Thus, 2 isn’t printed.

0 commit comments

Comments
 (0)