My professor said we have to write a page about how to trace this
for(int count = 0; count <= 3; count++)
for(int count2 = 0 < count; count2++)
System,out,println(count2)
Can somebody give me a general idea of how to trace this
I know the answer is
0
0
1
0
1
2
but idk how i got it

Play computer with the code. Take a piece of paper and write down the values of the variables as you mentally execute each statement.
For example the first time this statement is executed:
for(int count = 0; count <= 3; count++)
count will have a value of 0

Add some more print outs to the code to show the values of count to help you see how the code works compile and execute it to see what is printed out.

The next statement looks wrong: for(int count2 = 0 < count; count2++)

Edited 6 Years Ago by NormR1: n/a

I made a mistake this is the right code

for(int count = 0; count <= 3; count++)
for(int count2 = 0; count2 < count; count2++)
System,out,println(count2)

this was an example out of our textbook and we have to explain how we got the output. Thats what im struggling with

Write a program that executes the code. Add some more print outs to it, compile it and execute it.
Otherwise do as I suggested above: Get a piece of paper and play computer statement by statement, writing down the values of the variables as they change.

We are not allowed to write a java program for the code we must use pencil and paper

But you are allowed to ask some one on a forum?

Do you know how a for statement works? You need to know that to solve this puzzle.

Either read your textbook or Google for a description of how the for statement works.

Please fill the following table on a page to trace the nested for each loop:

for(int count = 0; count <= 3; count++)
for(int count2 = 0; count2 < count; count2++)
System,out,println(count2)

Only when both contitions: (count2<count) and (count<=3) are true, the output is available. If so fill the output accordingly. Otherwise fill the output with N/A
value of count value of count2 (count2<count)? (count<=3)? output
0 0 F T N/A
1 0 T T 0
1 1 F T N/A
2 0 T T 0
2 ...
2
3
3
3
3 ...

Edited 6 Years Ago by tong1: n/a

Please fill the following table on a page to trace the nested for each loop:
for(int count = 0; count <= 3; count++)
for(int count2 = 0; count2 < count; count2++)
System,out,println(count2)
Only when both contitions: (count2<count) and (count<=3) are true, the output is available. If so fill the output accordingly. Otherwise fill the output with N/A

This article has been dead for over six months. Start a new discussion instead.