Conditional and Control Flow Flashcards

1
Q

else Statement

A

The else statement executes a block of code when the condition inside the if statement is false. The else statement is always the last condition.

EX.

boolean condition1 = false;

if (condition1) {
System.out.println(“conditional1 is true”);
}
else{
System.out.println(“condition1 is not true”);
}
// Prints: condition1 is not true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

else if Statements

A

else - if statements can be chained together to check multiple conditions. Once a condition is true, a code block will be executed and the conditional statement will be exited.

There can be multiple else = if statements in a single conditional statement.

EX.

int testScore = 76;
char grade;

if (testScore >= 90) {
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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

if Statement

A

An if statement executes a block of code when a specified boolean expression is evaluated as true.

EX.
if (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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Nested Conditional Statements

A

A nested conditional statement is a conditional statement nested inside another conditional statement. The outer conditional statement is evaluated first; if the condition is true, then the nested conditional statement will be evaluated.

EX.

boolean studied = true;
boolean wellRested = true;

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!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

AND Operator

A

The AND logical operator is represented by &&. This operator returns true if the boolean expressions on both sides of the operator are true; otherwise, it returns false.

EX.

System.out.println(true && false); //Prints: true
System.out.println(true && false); //Prints: false
System.out.println(false && true); //Prints: false
System.out.println(false && false); //Prints: false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

NOT Operator

A

The NOT logical operator is represented by !. This operator negates the value of a boolean expression.

EX.

boolean 1 = true;
System.out.println(!a); //Prints: false

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

The OR Operator

A

The logical OR operator is represented by ||. This operator will return true if at least one of the boolean expressions being compared has a true value; otherwise, it will return false.

EX.

System.out.println(true || true); //Prints: true
System.out.println(true || false); //Prints: true
System.out.println(false || true); //Prints: true
System.out.println(false || false); //Prints: false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Conditional Operators - Order of Evaluation

A

If an expression contains multiple conditional operators, the order of evaluation is as follows: Expressions in parentheses -> NOT -> AND -> OR.

EX.

boolean foo = true && (!false || true); // 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. */

How well did you know this?
1
Not at all
2
3
4
5
Perfectly