Skip to content

Latest commit

 

History

History
35 lines (27 loc) · 638 Bytes

boolean_expressions.md

File metadata and controls

35 lines (27 loc) · 638 Bytes

Boolean Expressions

A common thing I've seen students do is set the initial value of some boolean variable based on some condition.

~void main() {
int age = 22;

boolean canRent;
if (age > 25) {
    canRent = true;
}
else {
    canRent = false;
}

// or
// boolean canRent = age > 25 ? true : false;

System.out.println(canRent);
~}

This is valid code, but very often can be made simpler if you remember that the condition itself already evaluates to a boolean. You can directly assign the variable to that value.

~void main() {
int age = 22;
boolean canRent = age > 25;

System.out.println(canRent);
~}