0% found this document useful (0 votes)
6 views

chap loop pdf

Uploaded by

asfiasiddique0
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

chap loop pdf

Uploaded by

asfiasiddique0
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

LOOPS

A for loop in JavaScript is used to repeat a block of code a certain number of times. It's ideal
when you know in advance how many times the loop should run.

Basic Syntax:

javascript

Copy code

for (initialization; condition; increment) {

// Code to be executed

Breakdown:

Initialization: This step runs once before the loop starts. It's often used to declare and initialize a
loop counter, like let i = 0;.

Condition: Before each loop iteration, JavaScript checks this condition. If it's true, the code inside
the loop runs. If it's false, the loop stops.

Increment: After each iteration, this step is executed, often used to update the loop counter (e.g.,
i++).

Example:

javascript

Copy code

for (let i = 0; i < 5; i++) {

console.log(i);
}

This loop will output 0, 1, 2, 3, 4. It starts with i = 0, checks if i < 5, and increments i by 1 on each
iteration.

How it Works:

The loop begins with i = 0.

It checks the condition i < 5. If true, the loop runs.

After executing the code block, it increments i by 1 (i++).

The process repeats until the condition becomes false (i.e., when i = 5).

Key Points:

You can loop through arrays using for loops.

javascript

Copy code

const array = [10, 20, 30];

for (let i = 0; i < array.length; i++) {

console.log(array[i]);

You can also use different increments or even decrement:

javascript

Copy code

for (let i = 5; i > 0; i--) {

console.log(i);

This structure is fundamental in programming for repetitive tasks.


### **`for` Loop in JavaScript**

The `for` loop is one of the most commonly used loops in JavaScript. It is ideal when you know
how many times you want to execute a block of code.

#### **Syntax:**

```javascript

for (initialization; condition; iteration) {

// Code to be executed

```
**Explanation of Syntax:**

1. **Initialization**: This is where you define and initialize your loop control variable. It usually
runs once at the beginning of the loop.

2. **Condition**: The loop runs as long as this condition is `true`. Once it becomes `false`, the
loop stops.

3. **Iteration**: This part updates the loop control variable (usually increments or decrements)
after each iteration.

---

### **Example:**

```javascript

for (let i = 1; i <= 5; i++) {

console.log(i);

```

**Output:**

```

```

#### **Explanation:**
- **Initialization**: `let i = 1` sets the starting point for the loop, initializing `i` to 1.

- **Condition**: The loop runs as long as `i <= 5`. When `i` becomes greater than 5, the loop
stops.

- **Iteration**: After each iteration, `i++` increments `i` by 1.

- The code inside the loop (`console.log(i);`) is executed for each value of `i`.

---

### **Breaking it Down:**

1. **First iteration**:

- `i = 1`

- Condition `i <= 5` is true.

- Prints `1`, then increments `i` to `2`.

2. **Second iteration**:

- `i = 2`

- Condition `i <= 5` is true.

- Prints `2`, then increments `i` to `3`.

3. **Third iteration**:

- `i = 3`

- Condition `i <= 5` is true.

- Prints `3`, then increments `i` to `4`.

4. **Fourth iteration**:
- `i = 4`

- Condition `i <= 5` is true.

- Prints `4`, then increments `i` to `5`.

5. **Fifth iteration**:

- `i = 5`

- Condition `i <= 5` is true.

- Prints `5`, then increments `i` to `6`.

6. **Termination**:

- `i = 6`

- Condition `i <= 5` is false, so the loop stops.

---

### **Common Uses of `for` Loop:**

- Iterating over arrays:

```javascript

let colors = ["red", "green", "blue"];

for (let i = 0; i < colors.length; i++) {

console.log(colors[i]);

```

**Output:**

```
red

green

blue

```

- Running a loop a fixed number of times:

```javascript

for (let i = 0; i < 10; i++) {

console.log("Iteration: " + i);

```

---

### **Conclusion:**

The `for` loop is perfect when you know how many times a loop should run. It lets you initialize,
set a condition, and increment a control variable all in one line, making it concise and easy to
control the flow of the loop.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy