Skip to content
Open
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
6 changes: 3 additions & 3 deletions ternary.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,15 @@ int abs(int x) {
if(x>=0)
return x;
else
return x-2*x;
return x*(-1);
}
```

Reduced to one line:

```
int abs(int x) {
return (x>=0)? x: x-2*x;
return (x>=0)? x: x*(-1);
}
```

Expand Down Expand Up @@ -111,4 +111,4 @@ doThis() if predicate else doThat()
print("Come in" if age>21 else "Go away!")
```

C, C++, Java, JavaScript, Python, Go, and a bunch of other languages have if expressions. Check your favorite language and see if it does, and go forth and refactor your code!
C, C++, Java, JavaScript, Python, Go, and a bunch of other languages have if expressions. Check your favorite language and see if it does, and go forth and refactor your code!