Skip to content

Commit bb1a863

Browse files
kjcmatyb
authored andcommitted
Focus comments on the "dangling else" problem.
1 parent 1cd8c1e commit bb1a863

1 file changed

Lines changed: 4 additions & 5 deletions

File tree

koans/src/beginner/AboutConditionals.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,17 @@ public void basicIfElseIfElse() {
4747

4848
@Koan
4949
public void nestedIfsWithoutCurlysAreReallyMisleading() {
50-
// Why are these ugly you ask? Well, try for yourself
5150
int x = 1;
5251
boolean secretBoolean = false;
5352
boolean otherBooleanCondition = true;
54-
// Ifs without curly braces are ugly and not recommended but still valid:
53+
// Curly braces after an "if" or "else" are not required...
5554
if (secretBoolean)
5655
x++;
5756
if (otherBooleanCondition)
5857
x = 10;
5958
else
6059
x--;
61-
// Where does this else belong to!?
60+
// ...but they are recommended.
6261
assertEquals(x, __);
6362
}
6463

@@ -67,6 +66,8 @@ public void ifAsIntended() {
6766
int x = 1;
6867
boolean secretBoolean = false;
6968
boolean otherBooleanCondition = true;
69+
// Adding curly braces avoids the "dangling else" problem seen
70+
// above.
7071
if (secretBoolean) {
7172
x++;
7273
if (otherBooleanCondition) {
@@ -75,8 +76,6 @@ public void ifAsIntended() {
7576
} else {
7677
x--;
7778
}
78-
// There are different opinions on where the curly braces go...
79-
// But as long as you put them here. You avoid problems as seen above.
8079
assertEquals(x, __);
8180
}
8281

0 commit comments

Comments
 (0)