+ 12
Why isn't this loop running infinitely?
I can't understand why this loop isn't running infinitely: int n=5; while (n--) { if(n%2==0) { cout<<"0";} else{ cout<<n;}} /*Output 03010*/ and what will be difference if we use condition (--n)? I'm beginner so don't judge me, please) Thank you :-)) https://code.sololearn.com/c2e147hLC2OX/?ref=app
20 Answers
+ 19
Without running the code I would hazard to say:
the while loop runs on a true/false condition. The condition must be true for the loop to execute(or continue)
False is defined as 0.
True is any value that is not 0.
Since you specify --n (--n returns its value) as the conditon each time the condition is checked n is reduced in value by 1 until its value is 0 (or false) at which point the while loop exits.
+ 14
I have updated the code to highlight the values. Best to run this code on your pc though.
+ 13
no... it would wrap around when it hit INT_MIN.
Granted it would be a very long loop. It would not be infinite.
https://code.sololearn.com/cACrWy19Ym5b/?ref=app
+ 11
:) John, I would say I corrected you. It is an easy mistake to make.
+ 11
see the size limits of int.
http://en.cppreference.com/w/cpp/language/types
climits INT_MIN just provides a define for the lowest integer possible. this could be replaced with -2,147,483,647 (edit)
Once INT_MIN is exceeded it wraps back around to INT_MAX (2,147,483,647) and continues to subtract until it reaches 0 (false)
+ 8
Thank you for your answers. I've understood that "0" is always false in C++ in any code. Isn't it? So the loop exits when the condition become 0 ( false).
+ 7
Your output with --n would be 0301 as the loop only execute 4 times instead of 5.
1: int n=5;
2: while (--n) {
3: if(n%2==0) {
4: cout<<"0";
5: } else {
6: cout<<n;
7: }
8: }
Line 1 sets n to 5
Line 2 subtracts 1 from n getting 4
Line 3 calculates 4%2 getting 0 so goes to 4
Line 4 outputs 0
Line 5 goes to 8
Line 8 goes to 2
Line 2 subtracts 1 from n getting 3
Line 3 calculates 3%1 getting 1 so goes to 6
Line 6 outputs 3
Line 7 goes to 8
Line 8 goes to 2
Line 2 subtracts 1 from n getting 2
Line 3 calculates 2%2 getting 0 so goes to 4
Line 4 outputs 0
Line 5 goes to 8
Line 8 goes to 2
Line 2 subtracts 1 from n getting 1
Line 3 calculates 1%1 getting 1 so goes to 6
Line 6 outputs 1
Line 7 goes to 8
Line 8 goes to 2
Line 2 subtracts 1 from n getting 0 which is false so goes to 9
+ 4
@Jay thanks for the link and explaination 😊 👍
+ 3
I'm on an android device, maybe the app compiler is not the same ? But what's the final value of n that breaks the loop ? My brain sends me a compiling error on your explanatory post 😞
+ 3
I'm sorry to bother you with that but when does the negative countdown hit 0 ? 😦
+ 2
Registered! Thanks guys !
+ 2
Does it apply even if you don't include <climits> ?
+ 2
Most compilers use 64 bits these days, though standard only requires 32. The looping only requires hardware subtract instruction. He saved half the loops by started at the largest negative number so one subtract wrapped to largest positive number.
+ 2
I'm guessing time limit changes based on online users. It ran perfectly when posted.
+ 2
It ran 4 hours ago but not now for me. Loop stops at 0.
+ 1
So if we had had a negative value for n, then we would have had an infinite loop right ?
+ 1
yes
Edited: I guess Jay proved me wrong...
+ 1
How long exactly ? What's the value of INT_MIN ? Does it depend on the compiler ?
+ 1
Code Playground gives me Time Limit Exceeded