Skip to content

Latest commit

 

History

History
295 lines (253 loc) · 5.38 KB

14. break and continue.md

File metadata and controls

295 lines (253 loc) · 5.38 KB

14. break and continue

JavaScript break and continue Statements

break Statement:

The break statement is used in JavaScript to exit a loop prematurely. It is often used when a certain condition is met, and you want to stop the loop's execution immediately.

Syntax:

for (let i = 0; i < 10; i++) {
    if (/* condition */) {
        break;
    }
    // Code to be executed in each iteration
}
  • The break statement causes the loop to terminate, and the program continues with the code outside the loop.

Example 1: Using break in a for loop

In this example, we use the break statement to exit a for loop when the value of i becomes 5.

for (let i = 1; i <= 10; i++) {
    console.log(i);
    if (i === 5) {
        break;
    }
}

Output:

1
2
3
4
5

The loop stops executing when i is equal to 5 due to the break statement.

continue Statement:

The continue statement is used to skip the current iteration of a loop and continue with the next iteration. It's helpful when you want to skip specific elements or conditions in the loop without terminating it entirely.

Syntax:

for (let i = 0; i < 10; i++) {
    if (/* condition */) {
        continue;
    }
    // Code to be executed in each iteration
}
  • The continue statement skips the current iteration and continues with the next iteration of the loop.

Example 2: Using continue in a for loop

In this example, we use the continue statement to skip printing numbers divisible by 3 in a for loop.

for (let i = 1; i <= 10; i++) {
    if (i % 3 === 0) {
        continue;
    }
    console.log(i);
}

Output:

1
2
4
5
7
8
10

The continue statement skips the numbers that are divisible by 3, allowing the loop to continue with the next iteration.

Example 3: Using break in a while loop

Here's an example of using the break statement to exit a while loop when a certain condition is met.

let number = 1;
while (number <= 10) {
    console.log(number);
    if (number === 7) {
        break;
    }
    number++;
}

Output:

1
2
3
4
5
6
7

The break statement is used to exit the while loop when number becomes 7.

Example 4: Using continue in a while loop

In this example, the continue statement is used within a while loop to skip the even numbers (divisible by 2) and print the odd numbers.

let number = 1;
while (number <= 10) {
    if (number % 2 === 0) {
        number++;
        continue;
    }
    console.log(number);
    number++;
}

Output:

1
3
5
7
9

The continue statement skips even numbers, allowing the loop to print only the odd numbers.

Both the break and continue statements are valuable tools in controlling the flow of loops in JavaScript, enabling you to exit loops prematurely or skip specific iterations as needed. These statements can help you write more efficient and precise code when working with loops in your JavaScript programs.

for loop without break Example

for (let i = 1; i <= 5; i++) {
    console.log(i);
}

Output

1
2
3
4
5

for loop with break Example

for (let i = 1; i <= 5; i++) {
    console.log(i);
    break;
}

Output

1

break with specific condition

for (let i = 1; i <= 5; i++) {
    if (i == 3) {
        break;
    }
    console.log(i);
}

Output

1
2

break with while

while (true) {
    let number = parseFloat(prompt("Enter a number: "));
    if (number < 0) {
        break;
    }
    console.log(number);
}

Output

Enter a number: 5
5
Enter a number: 9
9
Enter a number: -4
-4

continue Statement

for (let i = 1; i <= 5; i++) {
 
    if (i == 3) {
        continue;
    }

    console.log(i);
}

Output

1
2
4
5

break and continue with while

while (true) {
    let number = parseFloat(prompt("Enter a number: "));
    if (number < 0) {
        break;
    }
    if (number % 2 != 0) {
      continue;
    }

    console.log(number);
}

Output

Enter a number: 4
4
Enter a number: 9
Enter a number: 28
28
Enter a number: -34

Assignment and Task

Can you create a program that takes the input from the user. If the user enters a prime number, print the prime number. If the user enters a negative or non-prime number, ask the user for another number. And when the user enters a number greater than 100, terminate the loop.

Solution:

while (true) {
    let number = parseFloat(prompt("Enter a number: "));
    if(number > 100) {
        break;
    }
    
    let isPrime = true;
    for (let i = 2; i < number; i++) {
        if (number % i == 0) {
            isPrime = false;
            break;
        }
    }
    
    if (number < 0 || !isPrime) {
        continue;
    }
    
    else if (isPrime) {
    	console.log(number);
    }
}

Output

Enter a number: 5
Enter a number: 8
Enter a number: 11
Enter a number: 111
5
11

p4n Quiz

Q. Which of the following keywords is used to terminate a loop?

  1. terminate
  2. break
  3. continue
  4. loop

Answer: 2