aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes for Loop with y++ etc...  Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "for Loop with y++ etc...  " Watch "for Loop with y++ etc...  " New topic
Author

for Loop with y++ etc...

zhewen
Ranch Hand

Joined: Jul 18, 2000
Posts: 35
haiy all, it is not easy, please answer in step, i know the result. thanks! zhewen

public class LabelTest {
public static void main(String[] args){
int j=3;
int y=0;
mainLoop: for(int i=0; i<j; i++){<br /> if (i>1) continue mainLoop;
y++;}
mainLoop: for(int=0; i<y; i++){>
y--;}
System.out.println(y);}}

Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040
zhewen:
Allow me in saying yout post is not very user friendly...
Original code should be:

The main point to be learnt from this code is that the
second mainLoop label will override the first label.
Thats my understanding.
I waited till now to see if anyone will participate...since
no one has participated, I did some work on your code and here
is a modified version to see how the control flows.

Regds.
- satya

[This message has been edited by Madhav Lakkapragada (edited June 20, 2000).]


Take a Minute, Donate an Hour, Change a Life
http://www.ashanet.org/workanhour/2006/?r=Javaranch_ML&a=81
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Originally posted by Madhav Lakkapragada:
zhewen:
[...]
The main point to be learnt from this code is that the
second mainLoop label will override the first label.
Thats my understanding.
[...]

i am not exactly clear on what this code is supposed to exemplify.
could you please elaborate on it? what do you mean by "override.
zhewen
Ranch Hand

Joined: Jul 18, 2000
Posts: 35
haiy satya, thanks for check. it is somewhat curiusly with the editor. wenn i now klick the symble editor, the code is ok. but in the post page just missing some code. to the programm, i think it is allowed with the same label, it is not overriding etc...,
anyone can help us further?
zhewen
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040
overriding not in the sense of method overriding or
stuff like that, but say overwrites the previous label...
is that ok?
michal:
Please observe that in the above code, there are two mainLoop
labels. The JVM transfers the control to the next mainLoop
it finds after the current loop. Note that it doesnot
get confused by the presence of more than one labels with the
same name. (Not sure of the practical importance of this,
zhewen might want to comment why (s)he wrote this code )
Some more code:

Hope I am clear now!
Regds.
- satya

[This message has been edited by Madhav Lakkapragada (edited June 20, 2000).]
zhewen
Ranch Hand

Joined: Jul 18, 2000
Posts: 35
i think the two Loops will be excuted one after another, the result will be printed. i don't run the programm to see the result. wenn i skizz the route, i get the wrong result?
zhewen
thanks
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Originally posted by Madhav Lakkapragada:
[b]overriding
Please observe that in the above code, there are two mainLoop
labels. The JVM transfers the control to the next mainLoop
it finds after the current loop. Note that it doesnot
get confused by the presence of more than one labels with the
same name. (Not sure of the practical importance of this,
zhewen might want to comment why (s)he wrote this code )
[/B]

Madhav:
so i guess what you are trying to say is that
labels in Java should be unique in the loop scope?
thanks,
PS
i just thought i was missing something here.
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040

so i guess what you are trying to say is that
labels in Java should be unique in the loop scope?

I don't think so...In the above examples, the labels are
not unique, IMO. What I understood was that the JVM
continues execution at the label given after the continue
stmt. However, if this label is declared (multiple number
of times) after the loop, it continues from the next
location where it finds the label. More elaborately:

Will continue from Line 1, till the loop terminating
condition is reached.

This will continue from line 7 (not at line 1).

This will also continue from line 7 (not at line 1
and not line 9). Code at line 7 is executed and then
the execution will continue on.
Hope its clear now!
Regds.
- satya

Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
I'd say that yes, the labels should be unique within their scopes, in the sense that this is good programming practice to avoid confusion. However it appears that this is not a requirement of Java, to my surprise.


"I'm not back." - Bill Harding, Twister
Shiny
Ranch Hand

Joined: May 29, 2000
Posts: 45
Satya
I see you are logging now, as Madhav [renameTo("Madhav")] !?
Well, I don't seem to get the behaviour (of the code) as you have explained above. I think, in a given scope, till the leading condition is satisfied, the 'continue label' does NOT move the flow forward in the code to next statement where 'label' is reused. Try this code:
public class Test {
public static void main(String args[]) {
int j=4;
int y=0;
mainLoop: for(int i = 0; i < j; i++){
if (i==2) {
System.out.println("Step Two");
continue mainLoop;
}
System.out.println("Step One");
y++;
}
mainLoop: for(int i = 0; i < y; i++){
System.out.println("Step Three");
y--; }
System.out.println("Step Four");
System.out.println(y);
}
}
The output on my machine says:
Step One
Step One
Step Two
Step One
Step Three
Step Three
Step Four
1
instead of (if your logic was true):
Step One
Step One
Step Two
Step Three
Step Three
Step Four
'whatever y evaluates to'
correct me if I am mistaken,
by the way, I am taking the exam tomorrow - wish me luck !
Michal Harezlak
Ranch Hand

Joined: Jul 06, 2000
Posts: 185

satya:
I thing I have gotten your point now.
I am afraid you might be mistaken.
1. a statement cannot be labeled with the same identifier
as one of its enclosing statement. (it is allowed to
reuse label later)

will not compile.
while this one will

2. continue statement will not jump out of
enclosing loop even if the label is reuse later.

continue loop will jump to the beginning of the enclosing loop.
regards, michal.
Michal Harezlak
Ranch Hand

Joined: Jul 06, 2000
Posts: 185
Originally posted by Madhav Lakkapragada:
[B]
so i guess what you are trying to say is that
labels in Java should be unique in the loop scope?

I don't think so...In the above examples, the labels are
not unique, IMO. What I understood was that the JVM
continues execution at the label given after the continue
stmt. However, if this label is declared (multiple number
of times) after the loop, it continues from the next
location where it finds the label. More elaborately:

Will continue from Line 1, till the loop terminating
condition is reached.

This will continue from line 7 (not at line 1).

This will also continue from line 7 (not at line 1
and not line 9). Code at line 7 is executed and then
the execution will continue on.
Hope its clear now!
Regds.
- satya
[/B]

I am afraid you might be mistaken.
1. a statement cannot be labeled with the same identifier
as one of its enclosing statement. (it is allowed to
reuse label later)

will not compile.
while this one will

2. continue statement will not jump out of
enclosing loop even if the label is reuse later.

continue loop will jump to the beginning of the enclosing loop.
regards, michal.
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040
Originally posted by Shiny:
Satya
I see you are logging now, as Madhav [renameTo("Madhav")] !"> /i]
yup, the bartender stuff needs a first_name last_name kinda...
[i]instead of (if your logic was true):
Step One
Step One
Step Two
Step Three
Step Three
Step Four
'whatever y evaluates to'
correct me if I am mistaken,

yes, indeed you are correct. When I posted the code, I forgot
to verify the for loop really terminates and thats the reason
why we goto the next mailLoop label and not b'coz of what I said
earlier...Thanks for correcting.
by the way, I am taking the exam tomorrow - wish me luck
best wishes...may be you are already done with your exam !
michal:
i stand corrected, please see above.
Regds.
- satya
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: for Loop with y++ etc...
 
Similar Threads
labelled break
Compilation Error
labelled break
compare values in String array
Math.pow