Operators and Statements in Java

Operators and Statements in Java

Lesson 11: Operators

 

Operators are fundamental constructs in Java that allow us to manipulate variables and perform various operations. Java provides a rich set of operators, including arithmetic, relational, and logical operators.

 

Arithmetic Operators

 

Arithmetic operators are used to perform mathematical operations on operands. Here are the commonly used arithmetic operators in Java:

 

  1. Addition (`+`): Adds two operands.
  2. Subtraction (`-`): Subtracts the second operand from the first.
  3. Multiplication (``): Multiplies two operands.
  4. Division (`/`): Divides the first operand by the second.
  5. Modulus (`%`): Returns the remainder of the division operation.

 

Let's consider an example of using arithmetic operators:

 

```java

int x = 10;

int y = 5;

int sum = x + y;        // sum = 15

int difference = x - y; // difference = 5

int product = x  y;    // product = 50

int quotient = x / y;   // quotient = 2

int remainder = x % y;  // remainder = 0

```

 

 Relational Operators

 

Relational operators are used to establish relationships between operands. They are often used in conditional statements and loops. Here are the relational operators in Java:

 

  1. Equal to (`==`): Checks if two operands are equal.
  2. Not equal to (`!=`): Checks if two operands are not equal.
  3. Greater than (`>`): Checks if the left operand is greater than the right.
  4. Less than (`<`): Checks if the left operand is less than the right.
  5. Greater than or equal to (`>=`): Checks if the left operand is greater than or equal to the right.
  6. Less than or equal to (`<=`): Checks if the left operand is less than or equal to the right.

 

Let's illustrate the usage of relational operators:

 

```java

int a = 10;

int b = 5;

boolean isEqual = (a == b);     // isEqual = false

boolean isNotEqual = (a != b);  // isNotEqual = true

boolean isGreaterThan = (a > b); // isGreaterThan = true

```

 

 Logical Operators

 

Logical operators are used to combine conditional statements. They allow us to perform logical AND (`&&`), logical OR (`||`), and logical NOT (`!`) operations. Here's how they work:

 

  1. Logical AND (`&&`): Returns true if both operands are true.
  2. Logical OR (`||`): Returns true if at least one operand is true.
  3. Logical NOT (`!`): Returns the inverse of the operand's value.

 

Let's see logical operators in action:

 

```java

boolean condition1 = true;

boolean condition2 = false;

boolean resultAND = (condition1 && condition2);  // resultAND = false

boolean resultOR = (condition1 || condition2);   // resultOR = true

boolean resultNOT = !condition1;                 // resultNOT = false

```

 

 Lesson 12: Control Flow Statements

 

Control flow statements in Java allow us to control the flow of execution based on conditions. They include `if`, `if...else` statements, and loops such as `for`, `while`, and `do-while` loops.

 

 if Statement

 

The `if` statement is used to execute a block of code only if a specified condition is true. Here's its syntax:

 

```java

if (condition) {

    // block of code to be executed if the condition is true

}

```

 

If the condition evaluates to true, the code block inside the if statement is executed. Otherwise, it is skipped.

 

 if...else Statement

 

The `if...else` statement allows us to execute one block of code if the condition is true and another block if the condition is false. Here's how it looks:

 

```java

if (condition) {

    // block of code to be executed if the condition is true

} else {

    // block of code to be executed if the condition is false

}

```

 

 Loops: for, while, and do-while

 

Loops are used to execute a block of code repeatedly as long as a specified condition is true. Java supports three types of loops:

 

 for Loop: Executes a sequence of statements multiple times and abbreviates the code that manages the loop variable.

 while Loop: Repeats a statement or block of statements while a given condition is true. It tests the condition before executing the loop body.

 do-while Loop: Similar to the while loop, but the condition is tested after the loop body is executed, ensuring that the body is executed at least once.

 

Here's a brief example demonstrating each type of loop:

 

```java

// for loop

for (int i = 0; i < 5; i++) {

    System.out.println("Iteration " + i);

}

 

// while loop

int j = 0;

while (j < 5) {

    System.out.println("Iteration " + j);

    j++;

}

 

// do-while loop

int k = 0;

do {

    System.out.println("Iteration " + k);

    k++;

} while (k < 5);

```

 

 Lesson 13: Switch Statement

 

The `switch` statement is used to select one of many code blocks to be executed based on the value of an expression. It provides an efficient way to handle multiple possible execution paths.

 

 Syntax

 

```java

switch (expression) {

    case value1:

        // code block

        break;

    case value2:

        // code block

        break;

    ...

    default:

        // default code block

}

```

  • The `expression` is evaluated once.
  • The value of the expression is compared with the values of each `case`.
  • If there is a match, the corresponding block of code is executed.
  • The `break` statement terminates the switch statement. If omitted, the execution will continue to the next `case` until a `break` is encountered or the end of the switch statement is reached.
  • The `default` case is executed if no match is found.

 

 Example

Let's consider an example of a switch statement:

 

```java

int day = 4;

String dayString;

switch (day) {

    case 1:

        dayString = "Monday";

        break;

    case 2:

        dayString = "Tuesday";

        break;

    case 3:

        dayString = "Wednesday";

        break;

    case 4:

        dayString = "Thursday";

        break;

    case 5:

        dayString = "Friday";

        break;

    case 6:

        dayString = "Saturday";

        break;

    case 7:

        dayString = "Sunday";

        break;

    default:

        dayString = "Invalid day";

        break;

}

System.out.println("Day of the week: " +

 

 dayString);

```

 

In this example, the value of the variable `day` is compared with different cases, and the corresponding `dayString` is assigned based on the match. If no match is found, the default case is executed.

 

Conclusion

In this module, we've covered the essential topics of operators and statements in Java. Operators allow us to manipulate variables and perform various operations, while control flow statements help us define the flow of execution based on conditions. The switch statement provides an efficient way to handle multiple execution paths based on the value of an expression. Mastering these concepts will empower you to write more efficient, concise, and expressive Java code. Practice implementing these concepts in your projects to strengthen your Java skills and become a proficient Java developer. Stay tuned for more advanced topics in our Java programming series!