Use app×
Join Bloom Tuition
One on One Online Tuition
JEE MAIN 2025 Foundation Course
NEET 2025 Foundation Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
0 votes
221 views
in C++ by (176k points)
Learn about the powerful control flow statements in C++, break and continue, and how they can enhance your programming efficiency. Discover practical examples, usage guidelines, and best practices for implementing break and continue in your C++ code. Boost your programming skills with this comprehensive guide.

Please log in or register to answer this question.

2 Answers

0 votes
by (176k points)

C++ Break and Continue

The Break Statement

The break statement is used in C++ to exit a loop prematurely. It allows you to terminate the loop's execution and continue with the code that follows the loop. The break statement is commonly used when you want to exit a loop based on a certain condition.

The syntax of the break statement is as follows:

break;
 

The Continue Statement

The continue statement is used in C++ to skip the remaining statements within a loop iteration and move to the next iteration. It allows you to bypass the remaining code in the loop's body and jump to the loop's conditional check. The continue statement is commonly used when you want to skip specific iterations based on a certain condition.

The syntax of the continue statement is as follows:

continue;
 

Break and Continue in While Loop

Using Break in a While Loop

The break statement can be used within a while loop to exit the loop prematurely. Here's the general structure of a while loop with a break statement:

while (condition) {
  // code statements
  
  if (condition_to_exit_loop) {
    break;
  }
  
  // more code statements
}
 

In the example above, the loop will continue executing until the condition specified in the while statement is false. However, if the condition_to_exit_loop is met, the break statement is executed, causing an immediate exit from the loop.

Let's see an example to illustrate the usage of break in a while loop:

#include <iostream>

int main() {
  int count = 1;

  while (true) {
    std::cout << "Count: " << count << std::endl;

    if (count == 5) {
      break;
    }

    count++;
  }

  std::cout << "Loop exited." << std::endl;
  return 0;
}
 

In the above code, the while loop will continue indefinitely (true condition), but the break statement is triggered when the count variable reaches 5. As a result, the loop is terminated, and the message "Loop exited" is printed.

Using Continue in a While Loop

The continue statement can be used within a while loop to skip the remaining code within the loop's body and move on to the next iteration. Here's the general structure of a while loop with a continue statement:

while (condition) {
  // code statements
  
  if (condition_to_skip_iteration) {
    continue;
  }
  
  // more code statements
}
 

In the example above, if the condition_to_skip_iteration is met, the continue statement is executed. This causes the remaining code within the loop's body to be skipped, and the next iteration of the loop starts immediately.

Let's see an example to illustrate the usage of continue in a while loop:

#include <iostream>

int main() {
  int count = 1;

  while (count <= 5) {
    if (count == 3) {
      count++;
      continue;
    }

    std::cout << "Count: " << count << std::endl;
    count++;
  }

  return 0;
}
 

In the above code, the while loop iterates from 1 to 5. However, when the count variable equals 3, the continue statement is triggered. As a result, the remaining code within the loop's body is skipped for that iteration, and the loop moves on to the next iteration. Therefore, the number 3 is not printed, and the loop continues normally.

0 votes
by (176k points)

FAQs on C++ Break and Continue

Q: What is the break statement in C++? 

A: The break statement is used to exit or terminate a loop prematurely. When encountered inside a loop (such as for, while, or do-while), the break statement immediately terminates the loop and transfers control to the next statement outside the loop.

Example:

for (int i = 0; i < 10; i++) {
  if (i == 5) {
    break; // Exit the loop when i equals 5
  }
  cout << i << " ";
}
// Output: 0 1 2 3 4
 

Q: What is the continue statement in C++? 

A: The continue statement is used to skip the rest of the loop body and move on to the next iteration. When encountered inside a loop, the continue statement immediately jumps to the loop's update expression or the loop condition, bypassing any remaining statements within the loop body.

Example:

for (int i = 0; i < 5; i++) {
  if (i == 2) {
    continue; // Skip the current iteration when i equals 2
  }
  cout << i << " ";
}
// Output: 0 1 3 4
 

Q: How can break and continue be used with nested loops? 

A: break and continue can also be used with nested loops. When break is encountered inside a nested loop, it only terminates the innermost loop and control is transferred to the next statement outside that loop. Similarly, when continue is encountered, it only skips the current iteration of the innermost loop.

Example:

for (int i = 1; i <= 3; i++) {
  for (int j = 1; j <= 3; j++) {
    if (i * j == 6) {
      break; // Exit the inner loop when i*j equals 6
    }
    cout << i * j << " ";
  }
  cout << endl;
}
// Output:
// 1 2 3
// 2 4
// 3
 

Q: Can break or continue be used outside a loop? 

A: No, break and continue statements are designed to be used within loops. Attempting to use them outside a loop will result in a compilation error.

Example (Incorrect usage):

int x = 5;
break; // Compilation error: 'break' statement not inside loop or switch statement

if (x > 0) {
  continue; // Compilation error: 'continue' statement not inside loop
}
 

Remember, break and continue are powerful control flow statements that allow you to customize the behavior of loops based on specific conditions.

Important Interview Questions and Answers on C++ Break and Continue

Q: What is the purpose of the break statement in C++?

The break statement is used to exit or terminate a loop or switch statement prematurely. When encountered, the break statement immediately terminates the innermost loop or switch statement and control is transferred to the next statement after the loop or switch.

Example:

for (int i = 1; i <= 5; i++) {
    if (i == 3) {
        break;  // Terminates the loop when i equals 3
    }
    cout << i << " ";
}
// Output: 1 2
 

Q: How is the continue statement used in C++?

The continue statement is used to skip the remaining statements in the current iteration of a loop and move to the next iteration. It allows you to bypass certain iterations based on a condition and continue with the next iteration.

Example:

for (int i = 1; i <= 5; i++) {
    if (i == 3) {
        continue;  // Skips the current iteration when i equals 3
    }
    cout << i << " ";
}
// Output: 1 2 4 5
 

Q: Can the break statement be used outside of loops and switch statements?

No, the break statement can only be used within the body of a loop or switch statement. It cannot be used outside of these constructs, as there is no loop or switch to break out of.

Q: Can the continue statement be used outside of loops?

No, the continue statement is specific to loops and cannot be used outside of them. It is designed to skip the remaining statements within a loop and move to the next iteration.

Q: What happens if multiple loops are nested, and a break statement is encountered?

When a break statement is encountered in nested loops, it will only terminate the innermost loop and resume execution with the next statement after that loop. The outer loops will continue their execution.

Example:

for (int i = 1; i <= 3; i++) {
    for (int j = 1; j <= 3; j++) {
        if (i == 2 && j == 2) {
            break;  // Terminates the inner loop when i equals 2 and j equals 2
        }
        cout << i << "-" << j << " ";
    }
}
// Output: 1-1 1-2 1-3 3-1 3-2 3-3
 

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

Categories

...