Skip to content

Commit 5912473

Browse files
Update README.md
1 parent 564d0a9 commit 5912473

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

week_5/README.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Loops in Programming
2+
3+
Loops are structures in programming that allow a set of instructions to be repeated multiple times. There are two common types of loops:
4+
5+
1. **For Loops**
6+
2. **While Loops**
7+
8+
## While Loop
9+
10+
A `while` loop is used when you want to repeat a block of code as long as a certain condition is true.
11+
12+
### Example: Print numbers from 1 to 5 using a while loop
13+
14+
```python
15+
counter = 1
16+
while counter <= 5:
17+
print(counter)
18+
counter += 1
19+
```
20+
## For Loop
21+
A `for` loop is used when you know in advance how many times you want to execute a block of code. It typically iterates over a sequence (like a range of numbers).
22+
23+
### Example: Print numbers from 1 to 9 using a for loop
24+
```
25+
for i in range(1, 10):
26+
print(i)
27+
```
28+
29+
### Practice Questions
30+
31+
1. **Print Even Numbers:**
32+
Write a `for` loop that prints all even numbers between 2 and 20.
33+
34+
2. **Sum of Numbers:**
35+
Write a `while` loop that calculates the sum of numbers from 1 to 100.
36+
37+
3. **Factorial Calculation:**
38+
Write a `for` loop to calculate the factorial of a number `n`.
39+
40+
4. **Multiplication Table:**
41+
Write a `for` loop that prints the multiplication table for a number `n` from 1 to 10.
42+
43+
5. **Count Down:**
44+
Write a `while` loop that prints numbers from 10 to 1 in descending order.
45+
46+
47+
48+
49+

0 commit comments

Comments
 (0)