Control Statements

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.

OperatorNameDescriptionExample
+AdditionAdds two numeric types.int a = 5 + 6; // returns 11
-SubtractionSubtracts the right operand from the left operand.int a = 10 - 6; // returns 4
*MultiplicationMultiplies two numeric types.int a = 3 * 3; // returns 9
/DivisionDivides the left operand by the right operand.int a = 9 / 3; // returns 3
%ModulusDivides the left operand by the right operand and returns the remainderint 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.

OperatorNameDescriptionExample
==EqualityReturns true if both values are equal.10 == 10; // true
!=InequalityReturns true if the values are not equal.9 != 10; // true
>Greater thanReturns true if the left operand is greater than the right operand.10 > 9; // true
<Less thanReturns true if the left operand is less than the right operand.9 < 10; // true
>=Greater than or equalReturns true if the left operand is greater than or equal to the right operand.11 >= 10; // true
<=Less than or equalReturns 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).

OperatorNameDescriptionExample
&&ANDReturns true if both expressions are true. Evaluates the second expression only if necessary (short-circuiting).int age = 19
age > 18 && isStudent; // true if both conditions are true
||ORReturns true if either expression is true. Only checks the second value if necessaryint age = 16
boolean isStudent = true;
age > 18 || isStudent // returns true as second value is true
!NOTReverses 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 true or false (==, !=, >, <, >=, <=).
  • Logical Operators: Combine or negate boolean values (&& for AND, || for OR, ! for NOT).
  • Conditional Statements: Use if, else if, and else to make decisions based on conditions.
  • Combining Operators: Use relational and logical operators inside conditionals to evaluate complex conditions.

Further Reading


Leave a Reply

Your email address will not be published. Required fields are marked *

This site builds beginner confidence through fundamental coding concepts and regular practice. Java is the primary language, but the techniques apply across all programming languages.

Learn. Practice. Master

Categories