LATEST

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

In this tutorial, we will cover cases around the "Why for loop in C / C++ not stopping?" problem. This is one of the common problems when using for loop in C / C++.

So what are the reasons why the for loop can't stop? Let's get started with us !!!

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

There are two main reasons why the for loop in C / C++ can't be stopped.

  • The for loop does not use the condition.
  • The condition of the for loop is always true.

Now, we will go through each of the reasons why the for loop can't be stopped.

The for loop does not use the condition

In this case, we use a for loop with no conditions. That means, we don't define the end point of the loop.

For example:

#include <iostream>  
using namespace std;  
    
int main () {  
    for (; ;) {    
         cout << "Hello, I'm learnnc.com!!";    
    }    
}

Explain:

The for loop in the above program has no conditions. That means its condition is always true. So the statements inside the loop body are executed and repeated infinitely.

Output:

loop 01 PNG

The condition of the for loop is always true

Another case that programmers often make is setting the wrong condition of the loop. This results in the loop's condition being always true and repeating infinitely. Let's see the example below.

For example:

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

Explain:

In this program, we have a for loop with i = 5 and the condition of the loop is i > 0. This means that the condition is true, the statements inside the loop body are executed.

In subsequent iterations, the value of i increases to 6, 7, 8, 9,…. When compared with the condition i > 0, it is always true. That is, the loop will loop indefinitely and without stopping.

Output:

loop 02 PNG

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

There are two solutions to stop for loop in C / C++:

  • Set loop stop condition.
  • Use break command.

Now, we will learn about each solution.

Set loop stop condition

For this solution, you need to define the stopping condition of the loop. Then the loop will no longer have an infinite loop.

For example: We have a program that is getting an error that cannot stop the for loop as follows.

#include <iostream>  
using namespace std;  
    
int main () {
   cout << "How many grades would you like to enter? (Up to ten)\n";
   int x;
   cin >> x;
   int grade[x];
   cout << "What is your first grade?";
   cin >> grade[0];
   int i = 1;
   for (i; i = x; i++) {
      cout << "What is the next number?\n";
      cin >> grade[i];
   }
}

The program gets up to the point where it ask for the next number. Then you input the number and it keeps asking you that forever.

Solution:

What you have now (i = x) is the assignment of x to i, whereas you probably intended a comparison. In this case, the correct comparison to use is "less-than" (i < x).

The for loop should read:

for (int i = 1; i < x; i++)

Full code:

#include <iostream>  
using namespace std;  
    
int main () {
   cout << "How many grades would you like to enter? (Up to ten): ";
   int x;
   cin >> x;
   int grade[x];
   cout << "What is your first grade?";
   cin >> grade[0];
   for (int i = 1; i < x; i++) {
      cout << "What is the next number?\n";
      cin >> grade[i];
   }
}

Output:

loop 03 PNG

Use break command

To stop the loop without regard to its condition, we can use break statement in C/C++.

We will show you how to use break statement in another article. Now let's see the following example to understand better.

For example:

#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 for 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 04 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?

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

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

Top