Skip to content

Commit 1cd8c1e

Browse files
kjcmatyb
authored andcommitted
Restored nestedIfsWithoutCurlysAreReallyMisleading
...and ifAsIntended. Closes matyb#84 (I hope). Removing the curly braces makes the dangling else actually, um, dangle. The conditional is once again misleading, as intended. Also, changed the conditional(s) and variables in ifAsIntended to match those in nestedIfsWithoutCurlysAreReallyMisleading, so there is no question that the curly braces caused the as-intended result.
1 parent 8023ff1 commit 1cd8c1e

1 file changed

Lines changed: 20 additions & 4 deletions

File tree

‎koans/src/beginner/AboutConditionals.java‎

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,31 @@ public void nestedIfsWithoutCurlysAreReallyMisleading() {
5252
boolean secretBoolean = false;
5353
boolean otherBooleanCondition = true;
5454
// Ifs without curly braces are ugly and not recommended but still valid:
55+
if (secretBoolean)
56+
x++;
57+
if (otherBooleanCondition)
58+
x = 10;
59+
else
60+
x--;
61+
// Where does this else belong to!?
62+
assertEquals(x, __);
63+
}
64+
65+
@Koan
66+
public void ifAsIntended() {
67+
int x = 1;
68+
boolean secretBoolean = false;
69+
boolean otherBooleanCondition = true;
5570
if (secretBoolean) {
5671
x++;
57-
}
58-
if (otherBooleanCondition) {
59-
x = 10;
72+
if (otherBooleanCondition) {
73+
x = 10;
74+
}
6075
} else {
6176
x--;
6277
}
63-
// Where does this else belong to!?
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.
6480
assertEquals(x, __);
6581
}
6682

0 commit comments

Comments
 (0)