Cheatsheets / Programming Logic with Java
Conditionals and Control Flow
if Statement
An if statement executes a block of code when a
specified boolean expression is evaluated as true . if (true) {
[Link]("This code
executes");
}
// Prints: This code executes
if (false) {
[Link]("This code does not
execute");
}
// There is no output for the above
statement
else Statement
The else statement executes a block of code when the
condition inside the if statement is false . The else boolean condition1 = false;
statement is always the last condition.
if (condition1){
[Link]("condition1 is
true");
}
else{
[Link]("condition1 is not
true");
}
// Prints: condition1 is not true
else if Statements
else - if statements can be chained together to check
multiple conditions. Once a condition is true , a code int testScore = 76;
block will be executed and the conditional statement will char grade;
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';
}
[Link]("Grade: " + grade); //
Prints: C
Nested Conditional Statements
A nested conditional statement is a conditional statement
nested inside of another conditional statement. The outer boolean studied = true;
conditional statement is evaluated first; if the condition is boolean wellRested = true;
true , then the nested conditional statement will be
evaluated.
if (wellRested) {
[Link]("Best of luck
today!");
if (studied) {
[Link]("You are prepared
for your exam!");
} else {
[Link]("Study before your
exam!");
}
}
// Prints: Best of luck today!
// Prints: You are prepared for your exam!
AND Operator
The AND logical operator is represented by && . This
operator returns true if the boolean expressions on [Link](true && true); //
both sides of the operator are true ; otherwise, it returns Prints: true
false . [Link](true && false); //
Prints: false
[Link](false && true); //
Prints: false
[Link](false && false); //
Prints: false
The OR Operator
The logical OR operator is represented by || . This
operator will return true if at least one of the boolean [Link](true || true); //
expressions being compared has a true value; Prints: true
otherwise, it will return false . [Link](true || false); //
Prints: true
[Link](false || true); //
Prints: true
[Link](false || false); //
Prints: false
NOT Operator
The NOT logical operator is represented by ! . This
operator negates the value of a boolean expression. boolean a = true;
[Link](!a); // Prints: false
[Link](!true) // Prints: true
Conditional Operators - Order of Evaluation
If an expression contains multiple conditional operators,
the order of evaluation is as follows: Expressions in boolean foo = true && (!false || true); //
parentheses -> NOT -> AND -> OR. true
/*
(!false || true) is evaluated first
because it is contained within
parentheses.
Then !false is evaluated as true because
it uses the NOT operator.
Next, (true || true) is evaluation as
true.
Finally, true && true is evaluated as true
meaning foo is true. */