# A little help with a nested loop

Daniel Andres
Greenhorn
Posts: 25
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 post-decrement 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"

Roel De Nijs
Sheriff
Posts: 10662
144
My answer would be: code doesn't compile! And I proceed to the next question, no need to process complex iteration logic...

Daniel Andres
Greenhorn
Posts: 25
Roel De Nijs wrote:My answer would be: code doesn't compile! And I proceed to the next question, no need to process complex iteration logic...

lol I'm sorry, ignore the numbers

1
2
3

It is assumed that code before it is correct. Could you now help me out?

Roel De Nijs
Sheriff
Posts: 10662
144
• 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 do-while loop and 2 is subtracted from x (18). That's the 1st iteration of the do-while loop.
3/ x (18) is greater than 5, so you stay in the do-while loop and in the 2nd iteration 2 is subtracted from x (16)
4/ x (16) is greater than 5, so you stay in the do-while loop and in the 3rd iteration 2 is subtracted from x (14)
5/ x (14) is greater than 5, so you stay in the do-while 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 do-while 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 do-while loop again and 2 is subtracted from x (1)
13/ x (1) is not greater than 5, so you exit the do-while 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

Daniel Andres
Greenhorn
Posts: 25
• 1
Roel De Nijs wrote:14/ x (0) is not greater than 0, so you exit the while loop. The end!

Hope it helps!
Kind regards,
Roel

Ahhhh! Thank you for the explanation. Now I see it clearly.
Thanks again!

ps.- I did miss the closing curly brace