A little help with a nested loop
posted 1 year ago
Hello once again everyone! I am trying to understand what this nested loop is doing:
As I see it:
First x decrements to 18 and then the while loops checks. Since it is bigger than 5 it decrements by 1 but is it still 18 since it is a postdecrement operator? . I mean, the value of x is 17 but the code iterates from 18 and then decrements again to 16 and so forth until it reaches 4. Is this right?
This is what I have
18 16 14 12 10 8 6 4
17 15 13 11 9 7 5 3
When it reaches 3 then it prints out 3. Then it decrements again to 1 and it checks again even though it is less than 5 and then it goes down to 0 and then it prints out.
The final answer is: 3 0 and this is the procedure that makes more sense to me after a long while trying to understand this "simple code"
As I see it:
First x decrements to 18 and then the while loops checks. Since it is bigger than 5 it decrements by 1 but is it still 18 since it is a postdecrement operator? . I mean, the value of x is 17 but the code iterates from 18 and then decrements again to 16 and so forth until it reaches 4. Is this right?
This is what I have
18 16 14 12 10 8 6 4
17 15 13 11 9 7 5 3
When it reaches 3 then it prints out 3. Then it decrements again to 1 and it checks again even though it is less than 5 and then it goes down to 0 and then it prints out.
The final answer is: 3 0 and this is the procedure that makes more sense to me after a long while trying to understand this "simple code"
posted 1 year ago
But the code is not correct! A curly brace is missing (which is more important than the missing semicolon) and you'll get different output based on where the missing curly brace will be added.
Assuming you missed the closing curly brace of the while loop at the end of the code snippet, the code snippet would look like thisAnd this is what's happening:
1/ x (20) is greater than 0, so you enter the while loop and start with the 1st iteration of the while loop.
2/ you enter the dowhile loop and 2 is subtracted from x (18). That's the 1st iteration of the dowhile loop.
3/ x (18) is greater than 5, so you stay in the dowhile loop and in the 2nd iteration 2 is subtracted from x (16)
4/ x (16) is greater than 5, so you stay in the dowhile loop and in the 3rd iteration 2 is subtracted from x (14)
5/ x (14) is greater than 5, so you stay in the dowhile loop and in the 4th iteration 2 is subtracted from x (12)
6/ iteration 5: x (12) is greater than 5, 2 is subtracted from x (10)
7/ iteration 6: x (10) is greater than 5, 2 is subtracted from x (8)
8/ iteration 7: x (8) is greater than 5, 2 is subtracted from x (6)
9/ iteration 8: x (6) is greater than 5, 2 is subtracted from x (4)
10/ x (4) is not greater than 5, so you exit the dowhile loop and x is decremented with 1 (3) and 3 is printed
11/ x (3) is greater than 0, so you stay in the while loop and start the 2nd iteration.
12/ you enter the dowhile loop again and 2 is subtracted from x (1)
13/ x (1) is not greater than 5, so you exit the dowhile loop and x is decremented with 1 (0) and 0 is printed
14/ x (0) is not greater than 0, so you exit the while loop. The end!
Hope it helps!
Kind regards,
Roel
 1
Daniel Andres wrote:It is assumed that code before it is correct. Could you now help me out?
But the code is not correct! A curly brace is missing (which is more important than the missing semicolon) and you'll get different output based on where the missing curly brace will be added.
Assuming you missed the closing curly brace of the while loop at the end of the code snippet, the code snippet would look like thisAnd this is what's happening:
1/ x (20) is greater than 0, so you enter the while loop and start with the 1st iteration of the while loop.
2/ you enter the dowhile loop and 2 is subtracted from x (18). That's the 1st iteration of the dowhile loop.
3/ x (18) is greater than 5, so you stay in the dowhile loop and in the 2nd iteration 2 is subtracted from x (16)
4/ x (16) is greater than 5, so you stay in the dowhile loop and in the 3rd iteration 2 is subtracted from x (14)
5/ x (14) is greater than 5, so you stay in the dowhile loop and in the 4th iteration 2 is subtracted from x (12)
6/ iteration 5: x (12) is greater than 5, 2 is subtracted from x (10)
7/ iteration 6: x (10) is greater than 5, 2 is subtracted from x (8)
8/ iteration 7: x (8) is greater than 5, 2 is subtracted from x (6)
9/ iteration 8: x (6) is greater than 5, 2 is subtracted from x (4)
10/ x (4) is not greater than 5, so you exit the dowhile loop and x is decremented with 1 (3) and 3 is printed
11/ x (3) is greater than 0, so you stay in the while loop and start the 2nd iteration.
12/ you enter the dowhile loop again and 2 is subtracted from x (1)
13/ x (1) is not greater than 5, so you exit the dowhile loop and x is decremented with 1 (0) and 0 is printed
14/ x (0) is not greater than 0, so you exit the while loop. The end!
Hope it helps!
Kind regards,
Roel
What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/ThreadBoostfeature
