0% found this document useful (0 votes)
9 views2 pages

Conditions Reviewer and Example Updated

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views2 pages

Conditions Reviewer and Example Updated

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Reviewer - Chapter III: Conditions

Overview:
- In real life, decisions depend on certain conditions (e.g.,
passing/failing a student).
- In programming, we use Boolean data types, relational and
logical operators to create conditions.
- Java supports decision-making with if, if-else, multi-if, and
switch statements.

Relational Operators (Comparison Operators) and their Meanings:


- == : Equal to (True if both values are the same)
- != : Not equal to (True if values are different)
- > : Greater than (True if left value is larger than right
value)
- < : Less than (True if left value is smaller than right value)
- >= : Greater than or equal to (True if left value is larger than
or equal to right value)
- <= : Less than or equal to (True if left value is smaller than
or equal to right value)
- Example: if (grade >= 75) — checks if grade is passing.

Logical Operators:
- Combine multiple conditions:
- && (AND): True if both conditions are true.
- || (OR): True if at least one condition is true.
- ! (NOT): True if condition is false.
- ^ (XOR): True if conditions differ.

Compound Expressions:
- Using logical operators to connect multiple boolean expressions.

Conditional Statements:
- If: Executes block if condition is true.
- If-else: If condition is true, do block A; else, do block B.
- Multi-if (if-else if): Checks multiple conditions in sequence.

Switch Statement:
- Tests a variable against several possible values (cases).
- Uses break to prevent fall-through.
- Default handles unmatched cases.

---

Sample Java Code: Conditions

public class ConditionsExample {


public static void main(String[] args) {
int grade = 85;

// IF Statement
if (grade >= 75) {
System.out.println("You passed!");
} else {
System.out.println("You failed!");
}

// MULTI-IF Example
if (grade >= 90) {
System.out.println("Excellent!");
} else if (grade >= 80) {
System.out.println("Very Good!");
} else if (grade >= 75) {
System.out.println("Good!");
} else {
System.out.println("Needs Improvement.");
}

// SWITCH Example
int day = 3; // 1=Monday, 2=Tuesday, 3=Wednesday, etc.
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
default:
System.out.println("Invalid day");
break;
}
}
}

You might also like