LATEST

How to stop while loop in C / C++?

"How to stop while loop in C / C++?", "Why doesn't the while loop stop?", ... These are frequently asked questions when you use while loop in C / C++.

Don't worried!!! In this tutorial, we will show you how to stop while loop in C / C++ (exit while loop).

Why doesn't the while loop stop?

When using while loop, there are many cases where the loop does not stop. It repeats a block of code over and over and has no breakpoints.

We will use examples to illustrate for you.

Example 1:

#include <iostream>
using namespace std;
int main() {
  int i = 0;
  while(i = 5){
    cout<<"Welcome to learnnc.com!!!\n";
    i++;
  }
}

Explain:

Here, the condition of our loop uses the assignment operator i = 5. Although inside the loop we change the value of i++, at the beginning of the loop i = 5 always occurs. So the while loop does not stop.

*Note: You should be able to distinguish between assignment and comparison operators in C / C++.

Output:

loop 01 PNG

Example 2:

#include <iostream>
using namespace std;
int main() {
  int i = 1;
  while(i <= 5){
    cout<<"While loop not stopping!!!\n";
  }
}

Explain:

In this example, we initially initialize i = 1. And the condition of the loop is i <= 5 (always true because i = 1). However inside the body of the loop we don't change the value of i i.e. always equal to 1. This leads the loop to not stop because the condition is always true.

*Note: You should change the value of the loop to increase or decrease so that the condition is false. Only then will the loop stop.

Output:

loop 02 PNG

Example 3:

#include <iostream>
using namespace std;
int main() {
  int i = 10;
  while(i >= 5){
    cout<<"Help me stop!!!\n";
    i++;
  }
}

Explain:

Here, we initialize the variable i = 10. The condition of the loop is i >= 5. Inside the loop change the value of i by 1 after each loop (i++).

However, when increasing i++, the value of i changes to 11, 12, 13, 14, ... after each loop. These values are always greater than or equal to 5 (<= 5). So the loop condition is always true leading to infinite loop.

*Note: Change the value of the loop so that the loop condition after some iterations will be false. Only then the loop can be stopped.

Output:

loop 03 PNG

So how to stop while loop in C / C++? or how to exit the while loop? Let's find out with us right below.

How to stop while loop in C / C++?

To be able to stop the while loop, we have two ways to do this:

  • Set the appropriate loop condition.
  • Use break statement in C / C++.

Set the appropriate loop condition

To illustrate this, we will give two examples to deal with when the loop cannot be stopped.

Example 1:

#include <iostream>
using namespace std;
int main() {
  int i = 1;
  while(i <= 5){
    cout<<"While loop not stopping!!!\n";
  }
}

This program is repeating infinitely, so how can I stop it? Let's see how below.

Solution: Add a statement that changes the value of i inside the loop. This causes the loop's condition to be false after a certain number of iterations.

Program after change:

#include <iostream>
using namespace std;
int main() {
  int i = 1;
  while(i <= 5){
    cout<<"While loop not stopping!!!\n";
    i++;
  }
}

Output:

loop 04 PNG

Example 2:

#include <iostream>
using namespace std;
int main() {
  int i = 10;
  while(i >= 5){
    cout<<"Help me stop!!!\n";
    i++;
  }
}

In this program, the while loop is looped indefinitely because changing the value of i inside the loop does not make the condition false. See the solution below.

Solution: Change the value of i increment or decrement to suit the program.

Program after change:

#include <iostream>
using namespace std;
int main() {
  int i = 10;
  while(i >= 5){
    cout<<"Help me stop!!!\n";
    i--;
  }
}

Output:

loop 05 PNG

Use break statement in C / C++

In some cases, we want to exit the while loop immediately without caring what's behind it. To do this, we use break statement in C/C++. Take a look at the example below:

#include <iostream>  
using namespace std;  
    
int main () {
   for (int i = 1; i <= 50; i++) {
    if(i*i >= 100) {
      break;
    }
    cout << "Square of " << i << " = " << i*i << endl;
  }
}

Explain:

In this program, we have a while loop that loops from 1 to 50. Inside the body of the loop we take i * i (the square of i).

If the square of i > 100 then we exit the loop. To do this we use break statement in C / C++.

The break statement has the function of exiting the loop immediately, regardless of whether there are other statements behind it.

Output:

loop 06 PNG

Cùng chuyên mục:

Why do we use arrays instead of other data structures?

Why do we use arrays instead of other data structures?

What happens if no break statement in switch...case?

What happens if no break statement in switch...case?

Which is better while or do while loop?

Which is better while or do while loop?

Why for loop in C / C++ not stopping?

Why for loop in C / C++ not stopping?

Top