+ 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

31st Oct 2017, 2:27 PM
Юлия Советова
Юлия Советова - avatar
20 Antworten
+ 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.
31st Oct 2017, 2:39 PM
jay
jay - avatar
+ 14
I have updated the code to highlight the values. Best to run this code on your pc though.
11th Dec 2017, 4:55 AM
jay
jay - avatar
+ 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
10th Dec 2017, 11:17 PM
jay
jay - avatar
+ 11
:) John, I would say I corrected you. It is an easy mistake to make.
10th Dec 2017, 11:49 PM
jay
jay - avatar
+ 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)
11th Dec 2017, 4:21 AM
jay
jay - avatar
+ 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).
1st Nov 2017, 7:44 AM
Юлия Советова
Юлия Советова - avatar
+ 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
1st Nov 2017, 12:08 AM
John Wells
John Wells - avatar
+ 4
@Jay thanks for the link and explaination 😊 👍
11th Dec 2017, 5:51 AM
Mason Neville
Mason Neville - avatar
+ 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 😞
11th Dec 2017, 4:09 AM
Mason Neville
Mason Neville - avatar
+ 3
I'm sorry to bother you with that but when does the negative countdown hit 0 ? 😦
11th Dec 2017, 4:15 AM
Mason Neville
Mason Neville - avatar
+ 2
Registered! Thanks guys !
11th Dec 2017, 3:35 AM
Mason Neville
Mason Neville - avatar
+ 2
Does it apply even if you don't include <climits> ?
11th Dec 2017, 3:37 AM
Mason Neville
Mason Neville - avatar
+ 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.
11th Dec 2017, 3:45 AM
John Wells
John Wells - avatar
+ 2
I'm guessing time limit changes based on online users. It ran perfectly when posted.
11th Dec 2017, 3:54 AM
John Wells
John Wells - avatar
+ 2
It ran 4 hours ago but not now for me. Loop stops at 0.
11th Dec 2017, 4:11 AM
John Wells
John Wells - avatar
+ 1
So if we had had a negative value for n, then we would have had an infinite loop right ?
10th Dec 2017, 7:06 PM
Mason Neville
Mason Neville - avatar
+ 1
yes Edited: I guess Jay proved me wrong...
10th Dec 2017, 7:18 PM
John Wells
John Wells - avatar
+ 1
How long exactly ? What's the value of INT_MIN ? Does it depend on the compiler ?
11th Dec 2017, 3:36 AM
Mason Neville
Mason Neville - avatar
+ 1
Code Playground gives me Time Limit Exceeded
11th Dec 2017, 3:44 AM
Mason Neville
Mason Neville - avatar