Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update AboutConditionals.java #80

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 34 additions & 6 deletions koans/src/beginner/AboutConditionals.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,39 @@ public void switchStatementSwitchValues() {

@Koan
public void shortCircuit() {
int i = 1;
int a = 6;
// Why did we use a variable here?
// What happens if you replace 'a' with '6' below?
if ((a < 9) || (++i < 8)) i = i + 1;
assertEquals(i, __);
Counter trueCount = new Counter(true);
Counter falseCount = new Counter(false);
String x = "Hai";
if (trueCount.count() || falseCount.count()) {
x = "kthxbai";
}
assertEquals(x, __);
assertEquals(trueCount.count, __);
assertEquals(falseCount.count, __);
}

@Koan
public void bitwise() {
Counter trueCount = new Counter(true);
Counter falseCount = new Counter(false);
String x = "Hai";
if (trueCount.count() | falseCount.count()) {
x = "kthxbai";
}
assertEquals(x, __);
assertEquals(trueCount.count, __);
assertEquals(falseCount.count, __);
}

class Counter {
boolean returnValue;
int count = 0;
Counter(boolean returnValue) {
this.returnValue = returnValue;
}
boolean count() {
count++;
return returnValue;
}
}
}