Hi Paul,
It's correct if we replaced the break point "for" by "String", the program will print "hello" 2 times.
My question is why does the outer loop keep on going and the program ultimately prints "hello" 10 times. I don't see what's keeping it from continue looping. It loops only three times and exits without printing the 3rd time?
I did execute the code that was given in the explanation for the question. Sort of mind blown! So... we can have the same identifier for the class name, variable name, method name aaannnd label name?!
How exactly does this work? For my understanding, I was imagining a separate identifier list for classes, methods and variables till now. Now it seems like I'll have to add the label list in my mind as well. How many of these "lists" are there? ("lists" might not be how they work but I try to imagine it in that way to help me understand stuff. I hope you get what I mean to say i.e. how many different things can have the same identifier?)
Only keywords have a special meaning for the compiler. That is why they cannot be used as variable names etc. The meaning of everything else depends on the context and so it is possible to use the same name for a class, a variable, and a label.
Which code are you talking about? The one in the problem statement will not compile. The one in explanation will print hello twice. You will need to execute it step by step and work out the values of each variable at each step.
Paul.
When i is 0 or 1, i+j can never be > 10, so that prints a hello in first two iterations of outer for loop.
In the third iteration of outer for loop, break String; will get to execute and both the loops will be terminated.