GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Break statement in a for loop 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 "Break statement in a for loop" Watch "Break statement in a for loop" New topic
Author

Break statement in a for loop

Mehdi Ben Larbi
Ranch Hand

Joined: Aug 17, 2010
Posts: 70
Hello everyone,

I got confused with this question form the Study Guide ( Chapter 4 ) :



Question : What is the result?

A. same old
B. newly new
C. Compilation fails due to multiple errors
D. Compilation fails due only to an error on line 7
E. Compilation fails due only to an error on line 8
F. Compilation fails due only to an error on line 11
G. Compilation fails due only to an error on line 13

Answer : A.same old

I tired to run it and it's correct.
d2 and d1 are both equal to Days.TH but isn't d2 supposed to be equal to Days.F ?
In the for loop we say: if(d.equals(Days.F)) break;
So how is d equal to Days.TH and not Days.F ?

Thanks.


SCJP 93 % ,preparing for SCWCD
Chad Michaels
Ranch Hand

Joined: Jun 25, 2010
Posts: 42
Mehdi,


Go through each day.

If the current day equals F, then break the loop.

If the current day does not equal F, assign it to d2.

Here we go:

Does M equal F? No. So, D2 equals M.
Does T equal F? No. So, D2 equals T.
Does W equal F? No. So, D2 equals W.
Does TH equal F? No. So, D2 equals TH.
Does F equal F? Yes, BREAK!!!

What was the last value assigned to D2 above?

Hope this helps!
Mehdi Ben Larbi
Ranch Hand

Joined: Aug 17, 2010
Posts: 70
Thanks a lot,i was focused on the loop and i totally forgot about the if,it's clear now.
Shame on me
Chad Michaels
Ranch Hand

Joined: Jun 25, 2010
Posts: 42
No prob. Happens to everyone!
 
wood burning stoves
 
subject: Break statement in a for loop