File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt in break and continue statements 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 "Doubt in break and continue statements" Watch "Doubt in break and continue statements" New topic
Author

Doubt in break and continue statements

Shruti Rao
Greenhorn

Joined: Feb 01, 2010
Posts: 9
Hello everyone,

Can anyone explain me the flow of the following 2 programs??

3. public class Circles {
4. public static void main(String[] args) {
5. int[] ia = {1,3,5,7,9};
6. for(int x : ia) {
7. for(int j = 0; j < 3; j++) {
8. if(x > 4 && x < 8) continue;
9. System.out.print(" " + x);
10. if(j == 1) break;
11. continue;
12. }
13. continue;
14. }
15. }
16. }

Ans : D. 1 1 3 3 9 9 (HOW???)





3. public class Wind {
4. public static void main(String[] args) {
5. foreach:
6. for(int j=0; j<5; j++) {
7. for(int k=0; k< 3; k++) {
8. System.out.print(" " + j);
9. if(j==3 && k==1) break foreach;
10. if(j==0 || j==2) break;
11. }
12. }
13. }
14. }

Ans: C. 0 1 1 1 2 3 3 (HOW???)
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9302
    
  17

Shruti please Quote Your Sources when you post a question...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 440

hey
I had to delete the answer
as you have not quoted the source
I listen to moderators
I am a good boy


SCJP 6 [86%] June 30th, 2010
OCPWCD [84%] March 26th, 2012
OCEJPAD [83%] September 6th, 2014
If you find any post useful, click the "plus one" sign on the right
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Ankit Garg wrote:Shruti please Quote Your Sources when you post a question...


Please Follow the Ranch rules!

You can simplify your code to...



Then it seems to be simple!


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
Prasad Kharkar wrote:
I am a good boy


This line made my day!


Marriage Made in Heaven
http://www.youtube.com/user/RohitWaliaWedsSonia
Shruti Rao
Greenhorn

Joined: Feb 01, 2010
Posts: 9
K&B book,SelfTest Question number 10 and 12,Flow Control chapter wrote:Hello everyone,

Can anyone explain me the flow of the following 2 programs??

3. public class Circles {
4. public static void main(String[] args) {
5. int[] ia = {1,3,5,7,9};
6. for(int x : ia) {
7. for(int j = 0; j < 3; j++) {
8. if(x > 4 && x < 8) continue;
9. System.out.print(" " + x);
10. if(j == 1) break;
11. continue;
12. }
13. continue;
14. }
15. }
16. }

Ans : D. 1 1 3 3 9 9 (HOW???)





3. public class Wind {
4. public static void main(String[] args) {
5. foreach:
6. for(int j=0; j<5; j++) {
7. for(int k=0; k< 3; k++) {
8. System.out.print(" " + j);
9. if(j==3 && k==1) break foreach;
10. if(j==0 || j==2) break;
11. }
12. }
13. }
14. }

Ans: C. 0 1 1 1 2 3 3 (HOW???)
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

but tell us that what you dont understand in the program . I want to give you some points .

break ;----> it exit the inner most loop

break lablename ;-->it exit the loop[whether it is inner most or outer most, doesnt matter] which named/labled as lablename

continue ;---> when control flow encoounter this. it goes to an inner most for loop's increment/decrement part

continue lablename ;---> when control flow encoounter this. it goes to the for loop's increment/decrement part which named/labled as lablename

additionally, && and || these are all short-circuit operator because,

if the condition which is on left side of the operator is enough to find out the result then it wont check the condition of the operator which is on right side.
example (x > 8) && (x++ < 2) and say x = 5 , so on lefthand side x > 8 fails so simply the operator ignore the right-hand side of x++ < 2 , this behavior is same with ||

hth
Mari marin
Greenhorn

Joined: Aug 15, 2010
Posts: 2
i have the same doubt, i dont understand why that is the correct answer, if someone can help me i will be very grateful, my questions are:

- why it take the first number (1) again two times, and the for cycle should be for 3 times . I think its related with the continues
but i cant understand well. I hope someone can explain me please.
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

I think the line which confuses you is
6. for(int x : ia) {
you can replace it with this and then continue with the same

for(int x=0;x<=ia.length;x++){

now your first code becomes like this




Remember what break and continue will do.
Continue will transfer the control to the beginning of the loop like here continue will transfer the control to the
line no 7
break will break the loop..here break will break the loop starting at line no 7.

so now take pen or pencil and try the code by yourself.

i will point you but after reading the post try it yourself.
for x=1 && j=0 at line no 8 if any of the two condition return false then continue will not execute
both condtion return false so line no 9 executes and it prints 1.
the control continue on executing and when it reaches line no 11.@continue keyword will transfer the control to line no 7
now x=1 and j=1.
again condition check and line no 9 prints 1.
now when line no 10 reaches break will break the loop of line no 7.and control is transfered to line no 6.

now till now output is 1 1..
now x=3 and j=0 the same thing as mentioned above will happen and output will be 3 3 becaause when j=1 then it breaks
and j=2 will never get executed.

but for x=5 and x=7, line no 8 turns out to be true and controls will continuer transfers it loop till j=2.for these x values
lines after line 8 will never be executed.

now for x=9,the output will again be 9 9.

combined for x=1,3,9
line no 9 prints 1 1 3 3 9 9


please try it yourself using pen/pencil.because it not possible to explain the complete flow.
but i have tried to cover everything by my explaination.

Hope you have understand.

Happy Learning

SCJP6.0,My blog Ranchers from Delhi
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

In my explaination consider the line no as mentioned by red colors
Mari marin
Greenhorn

Joined: Aug 15, 2010
Posts: 2
I'm very grateful with your explanation, i did it with paper and i could understand, my problem was with the sentences continue and break..! Thanks you, i'm studying for the exam.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt in break and continue statements