Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
16 views

Learn Java - Conditionals and Control Flow Cheatsheet - Codecademy

The document discusses different types of conditional statements in Java including if, else if, else statements and how to chain them together. It also covers logical operators like AND, OR and NOT and the order of evaluation when multiple operators are used.

Uploaded by

shirley
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Learn Java - Conditionals and Control Flow Cheatsheet - Codecademy

The document discusses different types of conditional statements in Java including if, else if, else statements and how to chain them together. It also covers logical operators like AND, OR and NOT and the order of evaluation when multiple operators are used.

Uploaded by

shirley
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Cheatsheets / Learn Java

Conditionals and Control Flow

else Statement
The else statement executes a block of code when boolean condition1 = false;
the 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 int testScore = 76;
check multiple conditions. Once a condition is true , a
char grade;
code block 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';
}

System.out.println("Grade: " + grade); //


Prints: C
if Statement
An if statement executes a block of code when a if (true) {
specified boolean expression is evaluated as true .
System.out.println("This code
executes");
}
// Prints: This code executes

if (false) {
System.out.println("This code
does not execute");
}
// There is no output for the above
statement

Nested Conditional Statements


A nested conditional statement is a conditional boolean studied = true;
statement nested inside of another conditional
boolean wellRested = true;
statement. The outer conditional statement is
evaluated first; if the condition is true , then the
nested conditional statement will be evaluated. if (wellRested) {
System.out.println("Best of luck
today!");
if (studied) {
System.out.println("You are prepared
for your exam!");
} else {
System.out.println("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 System.out.println(true && true); //
operator returns true if the boolean expressions on
Prints: true
both 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

System.out.println(!false) // Prints:
true

The OR Operator
The logical OR operator is represented by || . This System.out.println(true || true); //
operator will return true if at least one of the
Prints: true
boolean expressions being compared has a true
value; otherwise, it will return false .
System.out.println(true || false); //
Prints: true
System.out.println(false || true); //
Prints: true
System.out.println(false || false); //
Prints: false
Conditional Operators - Order of Evaluation
If an expression contains multiple conditional boolean foo = true && (!false || true);
operators, the order of evaluation is as follows:
// true
Expressions in parentheses -> NOT -> AND -> OR.
/*
(!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. */

Print Share

You might also like