• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Dan's Exam on Flow control

 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 384
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Gayatri Ganesh
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Davy.
 
Gayatri Ganesh
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rama,
Thanks for replying.
But isn't the inner loop for the
case 2, the switch statement ?
Thanks,
Gayatri.
 
Davy Kelly
Ranch Hand
Posts: 384
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 384
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 384
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 384
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much to you all.
I am now able to understand and able to
solve the this example correctly.
Thanks,
Gayatri.
 
reply
    Bookmark Topic Watch Topic
  • New Topic