This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

continue.....doubt....

 
Ganeshkumar cheekati
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
source: http://www.coderanch.com/t/263903/java-programmer-SCJP/certification/loops

class JMM119 {
public static void main (String[] args) {
int i = 0, j = 9;
do {
if (j < 4) {break;} else if (j-- < 7) {continue;}
i++;
} while (i++ < 7);
System.out.print(i + "," + j);
}}

What is the result of attempting to compile and run the program?

a. Prints: 4,7
b. Prints: 6,6
c. Prints: 6,5
d. Prints: 6,4
e. Prints: 7,5
f. Prints: 8,4
g. Run-time error
h. Compile-time error
i. None of the above

output is:8,4.

here if continue statement is executed which is in the else-if block where the control will go after that?

is it go to if or else-if?

clear me...


[ November 06, 2008: Message edited by: Ganeshkumar cheekati ]
 
Vipul Prajapati
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ganesh,

"continue" is used to resume program execution at the end of the current loop body. If followed by a label, continue resumes execution at the end of the enclosing labeled loop body.

After the execution of "continue" statement, the control will be passed at the end of the LOOP. In this case the loop structure used is do-while, so it'll go at the and of the do-while loop and it will execute while statement.
[ November 05, 2008: Message edited by: Vipul Prajapati ]
 
Ankit Garg
Sheriff
Posts: 9528
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well ganesh please tell the source of your question. By the time I have just modified your code a bit. Try to think about the output and flow of execution from this

 
Vinod Awar
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
control will go to the begining of the do while loop.
 
Ganeshkumar cheekati
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi ankit.
i have taken this from old posts so i dnt know the actual source.
 
Ganeshkumar cheekati
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah i got it now.
the control will go to begining of do-while loop if while condition is true.

The best way to understand this type of programs is to insert System.out.println statements in those loops and conditional statements then it is easy for you.

 
Vipul Prajapati
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or You can use DEBUGGING facility of any IDE to trace the flow of your program.
[ November 06, 2008: Message edited by: Vipul Prajapati ]
 
Vipul Prajapati
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And be clear about the "continue" statement it will go at the end of the loop not the beginning of the loop in which it is declared.

For the precise meaning of "continue" keyword please refer following link:

Java Glossary
 
Ganeshkumar cheekati
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it depends upon the loop in which you are using continue statement.
after the execution of continue it has to for next iteration by checking the condition of that loop.

from K&B:

The continue statement causes only the current iteration of the innermost loop to cease and the next iteration of the same loop to start if the condition of the loop is met.

 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15441
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ganeshkumar cheekati:
hi ankit.
i have taken this from old posts so i dnt know the actual source.

Old posts on JavaRanch? Then please include a link to the old post.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic