This week's book giveaway is in the OCAJP 8 forum.We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!See this thread for details.
Big Moose Saloon
 Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies Register / Login Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!

# 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

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
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