• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in break and continue statements

 
Shruti Rao
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9529
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shruti please Quote Your Sources when you post a question...
 
Prasad Kharkar
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey
I had to delete the answer
as you have not quoted the source
I listen to moderators
I am a good boy
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Sandra Bachan
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prasad Kharkar wrote:
I am a good boy


This line made my day!
 
Shruti Rao
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my explaination consider the line no as mentioned by red colors
 
Mari marin
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic