Learn Java - Conditionals and Control Flow Cheatsheet - Codecademy
Learn Java - Conditionals and Control Flow Cheatsheet - Codecademy
else Statement
The else statement executes a block of code when the boolean condition1 = false;
condition inside the if statement is false . The else
statement is always the last condition.
if (condition1){
System.out.println("condition1 is true");
}
else{
System.out.println("condition1 is not
true");
}
// Prints: condition1 is not true
else if Statements
else - if statements can be chained together to check int testScore = 76;
multiple conditions. Once a condition is true , a code block
char grade;
will be executed and the conditional statement will be
exited.
There can be multiple else - if statements in a single if (testScore >= 90) {
conditional statement. grade = 'A';
} else if (testScore >= 80) {
grade = 'B';
} else if (testScore >= 70) {
grade = 'C';
} else if (testScore >= 60) {
grade = 'D';
} else {
grade = 'F';
}
if (false) {
System.out.println("This code does not
execute");
}
// There is no output for the above statement
AND Operator
The AND logical operator is represented by && . This System.out.println(true && true); // Prints:
operator returns true if the boolean expressions on both true
sides of the operator are true ; otherwise, it returns false .
System.out.println(true && false); // Prints:
false
System.out.println(false && true); // Prints:
false
System.out.println(false && false); // Prints:
false
NOT Operator
The NOT logical operator is represented by ! . This boolean a = true;
operator negates the value of a boolean expression.
System.out.println(!a); // Prints: false
The OR Operator
The logical OR operator is represented by || . This operator System.out.println(true || true); // Prints:
will return true if at least one of the boolean expressions true
being compared has a true value; otherwise, it will return
System.out.println(true || false); // Prints:
false .
true
System.out.println(false || true); // Prints:
true
System.out.println(false || false); // Prints:
false
Print Share