Win a copy of Murach's MySQL this week in the JDBC and Relational Databases forum!
  • 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

Help with Continue ..

 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone explain me the use of Continue..If we use continue how the flow of a program is..Can anyone explain with example..

Regards,
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



In the code shown the number 3 will be skipped because of the continue statement. The continue statement just means, skip this iteration.
 
Ranch Hand
Posts: 40
Android Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
continue continues with the next iteration of the loop skipping all the statements between the continue statement and the end of the loop. example:



In this example the seconf printf statement will not be executed as the control shifts to the next iteration when the continue statement is encountered.
 
Supriya Nimakuri
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How this program works...

public class MyFor{
public static void main(String argv[]){
int i;int j;
outer:
for (i=1;i <3;i++)
inner:
for(j=1; j<3; j++) {
if (j==2)
continue outer;
System.out.println("Value for i=" + i + " Value for j=" +j);
} }}


1) Value for i=1 Value for j=1
2) Value for i=2 Value for j=1
3) Value for i=2 Value for j=2
4) Value for i=3 Value for j=1
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't understand what you are asking here.
 
Supriya Nimakuri
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could you please tell me how the program works....and what will be the output(beloww are the 4 options)..what are the correct form the 4
 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Supriya Nimakuri:
How this program works...

public class MyFor{
public static void main(String argv[]){
int i;int j;
outer:
for (i=1;i <3;i++)
inner:
for(j=1; j<3; j++) {
if (j==2)
continue outer;
System.out.println("Value for i=" + i + " Value for j=" +j);
} }}


1) Value for i=1 Value for j=1
2) Value for i=2 Value for j=1
3) Value for i=2 Value for j=2
4) Value for i=3 Value for j=1



I guess the answer is: 1 and 2..
The flow is as follows: the control enters the Outer label then it will enter Inner label. Here, i=1 and j=1 now it"ll check if condition which is false So it will print i= 1 and j=1. Then the control again goes to for loop of Inner label where it will increment the value of j. So now i= 1 and j=2. Again if condition is checked which is now true. So it will "Continue Outer" . Hence, the control will not go to the System.out.println statement this time. IT WILL TERMINATE THE CURRENT ITERATION OF THE LOOP, AND AONTINUE WITH THE NEXT ITERATION OF THE OUTER LOOP.

Now the control will come to the next iteration of Oter label for loop. It will make i = 2 then it will again go through the same steps as above...when i=2 and j=1 it will print it, since if codition went false..then when i=2 and j=2 condition is reached it will again Continue Outer and go to the Outer for loop....there it will increment the value of i. But Since the for condition fails so it will come out of the loop...


Hope u got it...

Regards
Swapnil
 
Supriya Nimakuri
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the explanation Swapnil..good job..

when are you taking the exam..
 
Swapnil Trivedi
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
May be in the first week of July...when r u planning to take??



Regards
Swapnil
 
Supriya Nimakuri
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
May be, i was also planning in 1st week of July
 
Supriya Nimakuri
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what Mock exams are you taking....and how much is ur score...
reply
    Bookmark Topic Watch Topic
  • New Topic