Control statements allow you to control the flow of your programme—making decisions based on conditions. In Java, the primary control statements are arithmetic operators, relational operators, logical operators, and conditional statements (like if / else).
Arithmetic Operators
These operators allow you to perform mathematical calculations with numeric values.
| Operator | Name | Description | Example |
|---|---|---|---|
+ | Addition | Adds two numeric types. | int a = 5 + 6; // returns 11 |
- | Subtraction | Subtracts the right operand from the left operand. | int a = 10 - 6; // returns 4 |
* | Multiplication | Multiplies two numeric types. | int a = 3 * 3; // returns 9 |
/ | Division | Divides the left operand by the right operand. | int a = 9 / 3; // returns 3 |
| % | Modulus | Divides the left operand by the right operand and returns the remainder | int a = 10 % 3 // returns 1 |
Relational Operators
Relational operators compare two values and return a boolean result (true or false). These are often used to test conditions.
| Operator | Name | Description | Example |
|---|---|---|---|
== | Equality | Returns true if both values are equal. | 10 == 10; // true |
!= | Inequality | Returns true if the values are not equal. | 9 != 10; // true |
> | Greater than | Returns true if the left operand is greater than the right operand. | 10 > 9; // true |
< | Less than | Returns true if the left operand is less than the right operand. | 9 < 10; // true |
>= | Greater than or equal | Returns true if the left operand is greater than or equal to the right operand. | 11 >= 10; // true |
<= | Less than or equal | Returns true if the left operand is less than or equal to the right operand. | 9 <= 10; // true |
Note: Relational operators work on primitive types like int, float, char, etc. For objects (like String), use methods such as .equals() or .compareTo() for comparison.
Logical Operators
Logical operators are used to combine or reverse boolean values. They work specifically with boolean expressions (true or false).
| Operator | Name | Description | Example |
|---|---|---|---|
&& | AND | Returns true if both expressions are true. Evaluates the second expression only if necessary (short-circuiting). | int age = 19age > 18 && isStudent; // true if both conditions are true |
| || | OR | Returns true if either expression is true. Only checks the second value if necessary | int age = 16 boolean isStudent = true; age > 18 || isStudent // returns true as second value is true |
! | NOT | Reverses the value of the boolean expression. | !isStudent; // false if isStudent is true |
Conditional Statements: if, else if, else
Conditional statements allow you to execute different blocks of code based on conditions. The most common ones are if, else if, and else.
Anatomy of an if/else if/else Statement
if (condition) {
// code to run if the condition is true
} else if (anotherCondition) {
// code to run if anotherCondition is true
} else {
// code to run if none of the conditions are true
}
Example 1: Simple if Statement
int age = 18;
if (age >= 18) {
System.out.println("Is an adult");
}
Example 2: if / else Statement
int age = 16;
if (age >= 18) {
System.out.println("Is an adult");
} else {
System.out.println("Is not an adult");
}
Example 3: Using Multiple else if and else
double discount = 0.0;
int spend = 100;
if (spend > 200) {
discount = 0.20; // 20% discount
} else if (spend > 100) {
discount = 0.10; // 10% discount
} else if (spend > 50) {
discount = 0.05; // 5% discount
} else {
discount = 0.00; // no discount
}
System.out.println("Discount: " + discount); // the discount is calculated based on the amount spent
Summary
- Arithmetic Operators: Perform basic math (
+,-,*,/) on numeric values. - Relational Operators: Compare values and return
trueorfalse(==,!=,>,<,>=,<=). - Logical Operators: Combine or negate boolean values (
&&for AND,||for OR,!for NOT). - Conditional Statements: Use
if,else if, andelseto make decisions based on conditions. - Combining Operators: Use relational and logical operators inside conditionals to evaluate complex conditions.
Leave a Reply