File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Dan's Exam on Flow control Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Dan Watch "Dan New topic
Author

Dan's Exam on Flow control

Gayatri Ganesh
Ranch Hand

Joined: Dec 23, 2003
Posts: 143
Hi,
I have a question regarding Dan's exam on Flow control :
class JMM120 {
public static void main (String args[]) {
int i = 0, j = 0, k = 0;
label1:
for (; { i++;
label2:
do {
k = i + j;
switch (k) {
case 0: continue label2;
case 1: continue label1;
case 2: break;
case 3: break label2;
case 4: break label1;
default: break label1;
}
} while (++j<5);
}
System.out.println(i + "," + j);
}}
if i put a
System.out.print("("+i+","+j+","+k+")");
just before the switch statement, it prints the following :
(1,0,1)(2,0,2)(2,1,3)(3,1,4)3,1
But when I try to do it manually, I get :
(1,0,1) , (2,1,3), (2,2,4), (3,3,6)
I think there is difference when j is incremented at
while (++j<5);
Shouldn't be always incremented irrespective of whether we have
break label1/2 or a continue label1/2/
Can anyone please explain me this .
Thanks,
Gayatri
Gayatri Ganesh
Ranch Hand

Joined: Dec 23, 2003
Posts: 143
The for statement above is
for ( ; ; ) { i++;
So the above question from Dan's test is
class JMM120 {
public static void main (String args[]) {
int i = 0, j = 0, k = 0;
label1:
for ( ; ; ) { i++;
label2:
do {
k = i + j;
switch (k) {
case 0: continue label2;
case 1: continue label1;
case 2: break;
case 3: break label2;
case 4: break label1;
default: break label1;
}
} while (++j<5);
}
System.out.println(i + "," + j);
}}
I guess its showing symbols there.
Thanks,
Gayatri
Davy Kelly
Ranch Hand

Joined: Jan 12, 2004
Posts: 384
hey gayatri,
I ammended your code, with print statements to show you how it will loop through, test it.

The continue statement will forget the code after it and continue with either the immediate loop or the loop with a certain label, break will break out of the immediate loop or the loop with a certain label.
I hope this helps, but the above code should help you understand how it is working, but look at the results from the above changed code, with your basic code without statements to see what is happening.
Davy


How simple does it have to be???
Gayatri Ganesh
Ranch Hand

Joined: Dec 23, 2003
Posts: 143
Thanks Davy.
Gayatri Ganesh
Ranch Hand

Joined: Dec 23, 2003
Posts: 143
Hi Davy,
Thanks for the reply.
But I did not understand 1 point.
When the code reaches case 2, for k=2,
there is a break statement.
This break should break out of the switch(k)
statement, and contine with the do-while loop, right ?
But instead it reaches label1.
Can you please explain me this.
Thanks,
Gayatri.
Rama Kumar PV
Greenhorn

Joined: Dec 26, 2003
Posts: 26
The break statement for case 2 will take you out of both switch and the "do..while" loop and hence the result..(break will always take you out of the enclosing loop unless a label is mentioned after break).
Rama
Gayatri Ganesh
Ranch Hand

Joined: Dec 23, 2003
Posts: 143
Hi Rama,
Thanks for replying.
But isn't the inner loop for the
case 2, the switch statement ?
Thanks,
Gayatri.
Davy Kelly
Ranch Hand

Joined: Jan 12, 2004
Posts: 384
Hey Gayatri,
in the code above, when i compiled it the output was this:
//simplified output

rememebr in the code you are only really interested in k, the rest is just to show you what is happening.
So when the switch statement got reached, it just broke out of the switch statement and continued to look at the while statement below it, so it incremented j before testing if it is less than 5.
I hope this explains it
Davy
Davy Kelly
Ranch Hand

Joined: Jan 12, 2004
Posts: 384
sorry Gayatri,
remember that if your code can see the while(++j < 5) then it will pre increment the j to increase its value, but if your code tells it to continue with the loop it ignores what is below that continue statement and does another iteration, same with break if it does have a label it breaks away from that loop, if no label the loop you are in.
Davy
Rama Kumar PV
Greenhorn

Joined: Dec 26, 2003
Posts: 26
Gayatri,
The if statement and the switch statement are decision making controls and are not loops. So, the enclosing loop for your switch construct will be the "do..while" loop.
Cheers,
Rama
Davy Kelly
Ranch Hand

Joined: Jan 12, 2004
Posts: 384
yes rama,
but in the switch statement although it is a selective statement, it does use the break statement, if the break statement is used in the switch statement without a label, then the flow of control will just break out of the switch and go straight for the next line of code, in this case, the while(++j < 5);.
Davy
Gayatri Ganesh
Ranch Hand

Joined: Dec 23, 2003
Posts: 143
Hi,
Thanks Rama and Davy for answering my questions.
I understood case 2.
Now I have a difficulty in case 3.
When k=3, it goes to
case 3: break label2;
At label2 we have the do-while loop.
So it calculates the value ok k=i+j;
Since i=2,j=1 hence k=3.
And again it should go to case3.
But this does not happen.
Instead i is getting incremented,
that means it is going to label1.
Can anybody please explain me this.
Thanks,
Gayatri
Davy Kelly
Ranch Hand

Joined: Jan 12, 2004
Posts: 384
hey Gayatri, don't worry we will get there!

i hope the comments make the code have more sense, i think you were breaking out of the labels 2 but still looking at the while statement, these towo go hand in hand, the do while will do the code, then check the while, if it equals true, it will do another iteration of the do statement until the while has a false value, then that loop is finished, but the label to this do...while loop will end after the [Bwhile[/B] statement.
does this clear up the flow control?
if not, i will try to write it out in a diagram for you if you leave your email address i will email you it.
davy
David Hadiprijanto
Ranch Hand

Joined: Sep 14, 2003
Posts: 52
Hello Gayatri,
at case 3 (when k=3, i=2, j=1), it breaks on label2, which like you said is the do-while. It then goes to the next statement to be executed after the do-while, (line 3 - the closing bracket for the for-loop) which tells it should execute the next iteration of the for-loop (line 1). Hence, i is now 3, and you get k = 3 + 1 = 4, and case 4 is executed.
If you execute the code below, you will get :
...
Inside do-while. i=2 j=1 k=3
Beginning of for loop, i is incremented to : 3.
...
It shows that after case 3: break label2; it goes back up to the beginning
of the for loop, and i becomes 3.
Hope this helps.
Gayatri Ganesh
Ranch Hand

Joined: Dec 23, 2003
Posts: 143
Thank you very much to you all.
I am now able to understand and able to
solve the this example correctly.
Thanks,
Gayatri.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Dan's Exam on Flow control
 
Similar Threads
Regarding Continue Keyword
Switch/Label
what is the diffrence b/W i and i++ and explain inner loops with conditions
Label control doubt??
pls consider this program i don't know how h value is incremented in Label1: