You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Basic/WhileLoop.md
+30-24Lines changed: 30 additions & 24 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,10 +1,10 @@
1
-
## While Loop :performing_arts:
1
+
## Loop Statement : While Loop
2
2
3
-
### Introduction :barber:
3
+
### Introduction
4
4
5
5
- While loops repeat as long as a certain boolean condition is met.
6
6
- 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.
8
8
- The loop iterates while the condition is true.
9
9
- When the condition becomes false, program control passes to the line immediately following the loop.
10
10
- Iteration means executing the same block of code over and over, potentially many times.
@@ -18,10 +18,11 @@ Example:
18
18
```python
19
19
a=10
20
20
while a!=0:
21
-
print(a)
22
-
a-=1
23
-
'''
24
-
#Result
21
+
print(a)
22
+
a-=1
23
+
```
24
+
Output:
25
+
```
25
26
10
26
27
9
27
28
8
@@ -32,58 +33,63 @@ while a!=0:
32
33
3
33
34
2
34
35
1
35
-
'''
36
36
```
37
+
37
38
### Interruption :traffic_light: of Loop :loop: Iteration
38
39
- In each example you have seen so far, the entire body of the while loop is executed on each iteration.
39
40
- Python provides two keywords that terminate a loop iteration prematurely:
40
-
1. break
41
-
2. continue
41
+
```
42
+
1. break
43
+
2. continue
44
+
```
42
45
43
-
### 1.break:construction:
44
-
- Immediately terminates a loop :loop:entirely.
46
+
### 1.break
47
+
- Immediately terminates a loop entirely.
45
48
- Program execution proceeds to the first statement following the loop body.
46
49
47
50
Example:
48
51
```python
49
-
n=5
52
+
n=5
50
53
while n >0:
51
54
n -=1
52
55
if n ==2:
53
56
break
54
57
print(n)
55
58
print('Loop ended.')
56
-
'''
57
-
Result
59
+
```
60
+
61
+
Output:
62
+
```
58
63
4
59
64
3
60
65
Loop ended.
61
-
'''
62
66
```
67
+
63
68
when n becomes 2 the break statement is executed.loop is terminated completely,
64
69
and program execution jumps to the **print()** statement on line 7.
65
70
66
-
### 2. continue:fuelpump:
71
+
### 2. continue
67
72
- Immediately terminates the current loop iteration.
68
73
- 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.
69
74
70
75
Example:
71
76
```python
72
77
n =5
73
78
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)
78
83
print('Loop ended.')
79
-
'''
80
-
#Result
84
+
```
85
+
86
+
Output
87
+
```
81
88
4
82
89
3
83
90
1
84
91
0
85
92
Loop ended.
86
-
'''
87
93
```
88
94
when n is 2, the continue statement causes termination of that iteration.
0 commit comments