0% found this document useful (0 votes)
8 views6 pages

Iterative Statements Complete Notes

Uploaded by

aishaaltaf892
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views6 pages

Iterative Statements Complete Notes

Uploaded by

aishaaltaf892
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Class 12 IT - Comprehensive Notes on Iterative Statements

### Iterative Statements: In-Depth Explanation

**What is Iteration?**

- Iteration is the repetition of a block of code until a specified condition is satisfied.

- It is a fundamental concept in programming that helps to perform repetitive tasks efficiently.

---

### Types of Iterative Constructs (Loops)

1. **For Loop**

- Used when the number of iterations is fixed or known beforehand.

- **Syntax:**

```python

for variable in range(start, stop, step):

# Code block to execute

```

- **Parameters of range():**

- `start`: The starting value (default is 0).

- `stop`: The end value (loop stops before this value).

- `step`: The increment or decrement value (default is 1).

- **Example:**

```python

for i in range(1, 6):


print(i)

```

**Output:** 1, 2, 3, 4, 5

- **Use Case:** Printing numbers, iterating over lists, etc.

---

2. **While Loop**

- Used when the number of iterations is not fixed and depends on a condition.

- **Syntax:**

```python

while condition:

# Code block to execute

```

- **Example:**

```python

count = 1

while count <= 5:

print(count)

count += 1

```

**Output:** 1, 2, 3, 4, 5

- **Use Case:** Waiting for user input, processing data until a condition is met.

---
3. **Nested Loops**

- Definition: Loops within loops.

- **Example:**

```python

for i in range(1, 4):

for j in range(1, 4):

print(f"i={i}, j={j}")

```

**Output:** Prints all combinations of i and j.

---

### Special Loop Features

1. **Break Statement**

- Exits the loop immediately, regardless of the condition.

- **Example:**

```python

for i in range(5):

if i == 3:

break

print(i)

```

**Output:** 0, 1, 2

2. **Continue Statement**
- Skips the current iteration and proceeds to the next.

- **Example:**

```python

for i in range(5):

if i == 2:

continue

print(i)

```

**Output:** 0, 1, 3, 4

3. **Else Clause in Loops**

- Runs if the loop completes all iterations without a break.

- **Example:**

```python

for i in range(3):

print(i)

else:

print("Loop finished!")

```

**Output:** 0, 1, 2, Loop finished!

---

### Advanced Topics

1. **Loop Control with Conditions**

- Combining conditions within loops to create complex iterations.


- **Example:**

```python

for i in range(1, 11):

if i % 2 == 0:

print(f"{i} is even")

else:

print(f"{i} is odd")

```

2. **Infinite Loops**

- Loops that continue indefinitely until stopped manually.

- **Example:**

```python

while True:

print("This is an infinite loop")

```

3. **Nested Loop with Break**

- Break out of inner loops while outer loops continue.

- **Example:**

```python

for i in range(1, 4):

for j in range(1, 4):

if j == 2:

break

print(f"i={i}, j={j}")

```
---

### Importance of Iterative Statements

- Simplify repetitive tasks.

- Essential for handling large data sets.

- Enable dynamic solutions to complex problems.

You might also like