I wanted also ask about return 1 and return 0, good that I've seen this topic here. I am at page 122 in Head First! book. There is an exercise where we have this example:
My question is whats return value in for example 1st, 2nd, 3rd loop (for index = 0). And why is there return 1 and 0. Am I thinking right that when index value is bigger than 4 then return 0 = break?
I may just completely misunderstand your question.
This code snippet on its own does not really make that much sense, no matter what you set variable "m4" to it goes out of scope at line 7.
A class of Mix4 is created and a public member is set to some number. Either way this method will return the actual integer 1 or 0.
When you reach the return method the code will stop at that point, and return back to the code that invoked it (called it).
Hello Henrik. thanks for stopping by and answering. Since I can't edit my latest post Ill post here all code. After Ive checked it in Eclipse Ive figured out what was my exact point.
Sooo...I didnt know what return 1 and return 0 do here. I say what Ive came to and please correct me if I'm wrong.
In this case 'return 1' allows method maybeNew to run untill index is less than 5. Now 'return 0' : when index is equal or greater than 5, then'IF' loop no longer exist for main 'WHILE' loop and this is bassically amount = amount. (Because e.g. in m4a[5].maybeNew(5) index is equal 5 and maybeNew() returns 0).
Please don't hi‑jack threads by asking questions unrelated to the original subject. Fortunately I can split you off into a new topic. I don't have the time to go through Mix4 at the moment, but I would warn you it is more complicated than it looks at first sight.
'return 1' allows method maybeNew to run untill index is less than 5
The execution of the return 1 statement is done when the value of index is less than 5.
I'm not sure what is meant by "until". There is only one if statement in the maybeNew method that controls what is returned: either a 0 or a 1
then'IF' loop no longer exist
Some confusion there. An if statement is not a loop.
Yes of course IF is not a loop, it is statement, sorry for my mistake. I used word untill in my previous post because Untill x is 5 or more, that what is in IF statement is added to ‚amount’ value in WHILE loop, is it Right?
Sorry and thank you for splitting topics Campbell Richie.
Statements on lines 20 and 21 don't do anything that effects anything else in the program and could be removed without changing the values anywhere. m4 is a local variable that disappears when the maybeNew method exits.
No, code is exactly the same, I've just translated class and values names etc. I've looked into this code one more time, and tried few other examples e.g. x <20 and index < 5. As you have said, results are the same with and without lines 20-21. That's weird but it seems that I must take it for granted.