aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes continue.....doubt.... 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 "continue.....doubt...." Watch "continue.....doubt...." New topic
Author

continue.....doubt....

Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362
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 ]

SCJP5 and SCWCD1.5
Think Twice Act Wise...
Vipul Prajapati
Greenhorn

Joined: Aug 29, 2008
Posts: 9
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

Joined: Aug 03, 2008
Posts: 9313
    
  17

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



SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Vinod Awar
Ranch Hand

Joined: Nov 06, 2006
Posts: 125
control will go to the begining of the do while loop.


The biggest bankruptcy is the loss of enthusiasm
Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362
hi ankit.
i have taken this from old posts so i dnt know the actual source.
Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362
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

Joined: Aug 29, 2008
Posts: 9
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

Joined: Aug 29, 2008
Posts: 9
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

Joined: Oct 13, 2008
Posts: 362
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

Joined: Aug 16, 2005
Posts: 14345
    
  22

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
 
jQuery in Action, 2nd edition
 
subject: continue.....doubt....