• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

continue statement.......

 
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
source: https://coderanch.com/t/263901/java-programmer-SCJP/certification/loops]

class JMM121 {
public static void main (String args[]) {
int h = 0, i = 0, j = 0, k = 0;
label1:
for (;
{
h++;
label2:
do {
i++;
k = h + i + j;
System.out.println("value of j is:"+j);
switch (k) {
default: break label1;
case 1: continue label1;
case 2: break;
case 3: break label2;
case 4: continue label2;
case 5: continue label1;
}
System.out.println("change j:");
} while (++j < 5);
}
System.out.println(h + "," + i + "," + j);
}
}


i got output as:

value of j is:0
change j:
value of j is:1
value of j is:2
1 3 2


when continue label2; is executed the control has to check for while condition for next iteration of do-while loop.

In this case it is not executing System.out.println statement which is placed in between those lines.
why?

Is it jump to check the conditon directly?



[ November 05, 2008: Message edited by: Ganeshkumar cheekati ]

[ November 05, 2008: Message edited by: Ganeshkumar cheekati ]

[ November 05, 2008: Message edited by: Ganeshkumar cheekati ]

[ November 05, 2008: Message edited by: Ganeshkumar cheekati ]
[ November 06, 2008: Message edited by: Jesper Young ]
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ganesh this is your second question without proper source. Please QuoteYourSources. This is a mandatory thing...
 
Ganeshkumar cheekati
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can anyone clarify me?
 
Ranch Hand
Posts: 179
Mac Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
nope sources first please.
 
Ganeshkumar cheekati
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i mentioned source from where i got this question.......
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, Let's go through the flow of execution

Control will enter into for( ; ; )

h++ will set h to 1

do loop will be entered

i++ will set i to 1

k is set to h + i + j i.e. 1+1+0 i.e. 2

"value of j" i.e. "0" is displayed

switch(k) will result in 2 i.e. the switch will be breaked

"change j:" is displayed

++j is executed so j becomes 1

control will return to first statement of do while

i++ will be executed so i will become 2

k = h + i + j i.e. 1 + 2 + 1 i.e. 4

"value of j" i.e. "1" is displayed

switch(k) will result in continue label2

this will take control to while(++j < 5)

so j will become 2

again control will reach to first statement of do while

i++ will set i to 3

k = h + i + j i.e. 1+3+2 i.e. 6

"value of j" i.e. "2" will be displayed

none of the case labels match so default i.e. break label1 will be executed.

This will break the outer infinite for loop so control will reach System.out.println(h + "," + i + "," + j); and "1 3 2" will be displayed....
 
Ganeshkumar cheekati
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so after the execution of continue label2; statement
the control directly go to while(...) to check condition with out condering the System.out.println statement....

i got it now...

thanks ankit...your explanation is so sooth....
 
reply
    Bookmark Topic Watch Topic
  • New Topic