programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Why is this 8?

Vivienne Ryan
Greenhorn
Posts: 12
1
Hi there came across this piece of code in a test:

why is the result 8?
My source is from a test I got in class. Our instructor just printed out the questions on a sheet, no idea where they come from sorry.

Henry Wong
author
Sheriff
Posts: 23295
125

Jayesh A Lalwani
Rancher
Posts: 2762
32
• 1
The answer is rather elementary if you know how to read the code. I am suspecting that your instructor wants you to learn how to read code. So, I'm goi9g to give you tips on how to read code rather than spoon feed the answer.

I suggest that you take a piece of paper, and go over the code line by line and print out the state of each variable as you go over each line. When you are inside the loops, go through each iteration of the loop one by one. IOW, pretend you are the computer and execute the statement on paper.

I would start off like this

Line 3: c=0
Line 4: start of A loop; i = 0, c=0
Line 5: start of B loop; i = 0, j=0, c = 0
Line 6: start of C loop; i = 0, j = 0, k = 0, c = 0
Line 7: c++; i = 0, j = 0, k = 0, c = 1
Line 8: is (K > j) .. no, continue; i = 0, j = 0, k = 0, c = 1
Line 9: end of C loop, jump to line 6; i = 0, j = 0, k = 0, c = 1
Line 6: next iteration of C loop, k++ means k=1. is k<2... yes so loop is not complete; i = 0, j = 0, k = 1, c = 1

and so on

You will have to go through each step meticulously.

Vivienne Ryan
Greenhorn
Posts: 12
1
when I break, do I then break out of C: or do I break out to the very first for?

Vivienne Ryan
Greenhorn
Posts: 12
1
Jayesh A Lalwani wrote:The answer is rather elementary if you know how to read the code. I am suspecting that your instructor wants you to learn how to read code. So, I'm goi9g to give you tips on how to read code rather than spoon feed the answer.

Thank you very much! You have just helped me to fully understand what the code is supposed to do!

Campbell Ritchie
Marshal
Posts: 56593
172
Hope it is not too late to welcome you as a new ranch member

You will find practice exams have that sort of code in, which would get you the sack if you produced it in real life. They tend to put features in to confuse you, e.g. incorrect indentation. At least you have correct indentation, but that code has at least three red herrings in.

Campbell Ritchie
Marshal
Posts: 56593
172
Vivienne Ryan wrote:when I break, do I then break out of C: or do I break out to the very first for?
Out of the innermost loop only.

Jayesh A Lalwani
Rancher
Posts: 2762
32
Vivienne Ryan wrote:
Jayesh A Lalwani wrote:The answer is rather elementary if you know how to read the code. I am suspecting that your instructor wants you to learn how to read code. So, I'm goi9g to give you tips on how to read code rather than spoon feed the answer.

Thank you very much! You have just helped me to fully understand what the code is supposed to do!

You are welcome. Actually, what I showed you is the secret sauce to understanding every program in existence. You can figure out what a program does by just reading it line by line. It's difficult initially, but when you get the hang of it, you can read it much faster. TO draw an analogy, when kids first learn to read, thyey read word by wrod, then string the words together to decode the meaning in the sentence. As they practice more, they don't need to "read" the whole word to understand the meaning. Then, you start learing speed reading:- You don't need to read the whole sentence to understand it (until you reach a difficult or poorly worded sentence). Reading code is just like that. You start of going line by line, but eventually, as long as the code follows convention, and is well written, you can read it much faster.

Vivienne Ryan wrote:when I break, do I then break out of C: or do I break out to the very first for?

You break out of the loop you are in. So, in this case, it's the innermost loop.

Look at the condition in the break statement and compare it with your loop variables. You will see what Campbell is saying about a "red herring"

Campbell Ritchie
Marshal
Posts: 56593
172
• 1
Jayesh A Lalwani wrote: . . . You will see what Campbell is saying about a "red herring"
I didn't say, “red herring”.

I said, “red herrings”.
Challenge to Vivienne Ryan: count the red herrings.

Vivienne Ryan
Greenhorn
Posts: 12
1
Campbell Ritchie wrote:
Jayesh A Lalwani wrote: . . . You will see what Campbell is saying about a "red herring"
I didn't say, “red herring”.

I said, “red herrings”.
Challenge to Vivienne Ryan: count the red herrings.

Will do!
Right now though I am celebrating passing the Programmer I exam

Campbell Ritchie
Marshal
Posts: 56593
172
Congratulations

 With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.