+ 2
How can I understand the more compact while loop?
int x=10; while(x){ x++ ;} cout<< x ;
2 ответов
+ 2
In your loop, you've created an infinite loop because you're updating X each loop through and basing your while condition upon X simply existing.
For example:
while(x){
x++;
}
The while condition is simply if x is true, and as long as it exists, it'll stay true. When you increase the value of x inside of the loop, it's updating that value to the while condition also, so the while condition will always stay true. You'll want to specify better conditions for it, such as:
int x = 10;
while(x < 10){
x++;
}
^Now we have a condition that isn't infinite. Each loop through it'll check if x is less than 10, if it is then continue with the loop. However, if it's equal to or greater than 10, it'll stop the loop.