Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes Why is this 8? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why is this 8?" Watch "Why is this 8?" New topic
Author

Why is this 8?

Vivienne Ryan
Greenhorn

Joined: Dec 12, 2013
Posts: 12

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

Joined: Sep 28, 2004
Posts: 18103
    
  39


Please QuoteYourSources


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2052
    
  22

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

Joined: Dec 12, 2013
Posts: 12

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

Joined: Dec 12, 2013
Posts: 12

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
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
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
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
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
Bartender

Joined: Jan 17, 2008
Posts: 2052
    
  22

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
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
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

Joined: Dec 12, 2013
Posts: 12

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
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
Congratulations
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why is this 8?
 
Similar Threads
Flow Control -JQ+
Question from jqplus
Constructor question
I am not getting the required output...
confusing for Loop.