posted 2 years 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

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

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"**
posted 2 years ago

But the code is

Assuming you missed the closing curly brace of the

1/

2/ you enter the

3/

4/

5/

6/ iteration 5:

7/ iteration 6:

8/ iteration 7:

9/ iteration 8:

10/

11/

12/ you enter the

13/

14/

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

__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.__

**not**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