BackendBench
Coffee
Spot the Bug #127

Every day is a weekend

A easy Java puzzle in the logic and control flow family. One line below contains a real bug. Try to spot it first, then read why it fails and how to fix it.

easyLogic and control flowbuggy line: 7

Highlighted: line 7. Play this one as a puzzle without the answer.

What is wrong

Without break or return, execution falls through into the default branch, which overwrites the result. Every day, including 6 and 7, returns "weekday".

The fix

case 7: result = "weekend"; break; default: result = "weekday";

How to avoid it next time

End each case with break, or use the arrow form (case 6, 7 -> "weekend") which never falls through.

Want the background? Read Switch expressions and pattern matching in the Java concepts library.

About logic and control flow

Operator precedence, missing break statements, short-circuit evaluation and loop conditions that never change are simple to write and tiring to spot after a long day of reviews.

In code review: Read conditions out loud with explicit parentheses, check every switch branch for break or return, and make sure each loop makes progress toward its exit.

More puzzles in this family

๐Ÿž Play today's puzzleAll 132 bugsโ† #126#128 โ†’

Frequently asked questions

What is the bug in "Every day is a weekend"?

Without break or return, execution falls through into the default branch, which overwrites the result. Every day, including 6 and 7, returns "weekday". The faulty line is line 7: result = "weekend";

How do I fix it?

Replace it with: case 7: result = "weekend"; break; default: result = "weekday";. End each case with break, or use the arrow form (case 6, 7 -> "weekend") which never falls through.

How do I catch logic and control flow in code review?

Read conditions out loud with explicit parentheses, check every switch branch for break or return, and make sure each loop makes progress toward its exit.

Is there a new puzzle every day?

Yes. The daily Spot the Bug puzzle picks one of these snippets for each UTC day, with the difficulty set by the weekday. Your streak is stored only in your browser.

More tools you might need