Home > Software design >  Why is this while loop an infinite loop? High school introduction to CS exam review
Why is this while loop an infinite loop? High school introduction to CS exam review

Time:02-01

I'm doing my high school introduction to CS exam review, and I don't understand why this while loop is an infinite loop:

int b = 6;
while (b != 21)
{
    b   3;
    cout << b << " ";
}

Eventually, b will equal 21, so shouldn't it stop the loop?

I tried understanding, but I can't understand this.

CodePudding user response:

You're not updating the value of b inside the loop, so its value is always 6. You need to use an assignment operator to actually set a new value.

So, use b = b 3; or b = 3; instead.

  • Related