LucasGrillos
LucasGrillos

Reputation: 25

Why is a loop doing this in C++

Alright, beginner, take it easy. Thanks.

#include <iostream>
#include <windows.h>

using namespace std;
int main()
{

    int x = 0;
    bool while1 = true;
    while (while1)
    {
        cout << x << "\n";
        sleep(200);
        if (x < 10)
        {
            while1 = true;
        }
        else if (x >= 10)
        {
            while1 = false;
        }
        x = x+1;
    }
    cin.get();
}

Alright, so I don't understand why the program even gets x to 10 if I'm have the if statements check if x < 10 before I have 1 added to x... please explain.

Upvotes: 0

Views: 184

Answers (6)

Vince
Vince

Reputation: 121

What you are doing is:

  1. asking the program to print the value of x
  2. check to see if x is less than 10
  3. if it is, set while1 to true. Else set while1 to false
  4. increase the value of x by 1 (regardless of whether while1 is true or false)

...

  • At the start of the 10th loop, x = 9.
  • Prints out 9
  • Since x is still less than 10, while1 remains true
  • At the end of the 10th loop, x = 10 and while1 = true

Your program start another loop since while1 = true

  • At the start of the 11th loop, x = 10.
  • Prints out 10
  • Since x is now equal to 10, while1 is set to false
  • At the end of the 11th loop, x = 11 and while1 = false

Best thing to do is to just

int x = 0;
while(x < 10)
{
   cout << x << endl;      //this way your program will only print 0 to 9
   sleep(200);
   x++;                    //this is the same as x = x+1
}

If you insist on using your own code, I guess you can replace your while1 = false with break;

else if (x >= 10)
{
    break;
}

break; will let you break out of the while loop if x >= 10 without increasing x or going through the loop again...

Should be safe unless you are going to do more things in that while loop...

Upvotes: 0

Patashu
Patashu

Reputation: 21773

Order of operations is:

  • 1) stop the loop if 3) found x was >= 10 (if while1 is false)
  • 2) print out x
  • 3) check if x >= 10 here and set while1 to false if it is
  • 4) increment x

When x is 9 we proceed as follows:

  • Don't stop the loop (while1 is true)
  • Print out 9
  • x is not >= 10 yet, set while1 to true
  • x is incremented to 10
  • Don't stop the loop (while1 is true)
  • Print out 10
  • x is now >= 10, set while1 to false
  • x is incremented to 11
  • We stop the loop as while1 was set to false

So, x gets printed out when it's 10. The problem is that, the way you check and act on the check, you do one loop too many before stopping.

One way to fix this is

while (x < 10)
{
    cout << x << "\n";
    Sleep(200);
    x = x+1;
}

Now the loop will not execute on the same pass through that x is 10, not the pass immediately afterwards.

Even better is this:

for (int x = 0; x < 10; ++x)
{
    cout << x << "\n";
    Sleep(200);
}

Which is clearer about its intent over looping over incrementing values of x.

Upvotes: 4

Ankit
Ankit

Reputation: 1

if x>=10 in the loop than obviously the loop will iterate one more time when it reaches to 10 in the while loop

Upvotes: 0

raj raj
raj raj

Reputation: 1922

If you don't want x to be printed after x >= 10, do the checking after you increment x.

#include <iostream>
#include <windows.h>

using namespace std;
int main(
{
    int x = 0;
    bool while1 = true;
    while (while1)
    {
        cout << x << "\n"
        x++;
        Sleep(200);
        if (x < 10)
        {
            while1 = true;
        }
        else if (x >= 10)
        {
            while1 = false;
        }
    }
    cin.get();
}

Upvotes: 0

Nashibukasan
Nashibukasan

Reputation: 2028

else if (x >= 10)
{
    while1 = false;
}
x = x+1;

I understand the question as: Why is x getting to 10? Is this correct?

Your while loop does not immediately exit when setting while1 to false. So of course x will equal to 10 after exiting the loop as x is increase after checking that, so x will in fact be 11 after this code segment.

Upvotes: 0

Jean-Bernard Pellerin
Jean-Bernard Pellerin

Reputation: 12670

The program gets x to 10 because the x = x + 1 happens outside of the if.
So during one execution of the loop, it will check, then if x is less than 10 it will make while1 true. Otherwise it makes while1 false. After that it increases x no matter what. Then it repeats this process if while is true.

Upvotes: 0

Related Questions