Help coderanch get a
new server
by contributing to the fundraiser
Plus win three weeks of access to Security in Spring Boot REST Web Service Applications this week in the Spring forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Jeanne Boyarsky
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Rob Spoor
• Devaka Cooray
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Carey Brown
• Frits Walraven
• Tim Moores
Bartenders:
• Mikalai Zaikin

# Break statement in a for loop

Ranch Hand
Posts: 70
• Number of slices to send:
Optional 'thank-you' note:
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.

Ranch Hand
Posts: 42
• Number of slices to send:
Optional 'thank-you' note:
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
Posts: 70
• Number of slices to send:
Optional 'thank-you' note:
Thanks a lot,i was focused on the loop and i totally forgot about the if,it's clear now.
Shame on me