Techyajay

header ads

Advertisement

Chapter 5: Control Statements in Java Language


Chapter 5: Control Statements in Java Language


Introduction 

Java programming ,  control statements are essential for guiding the flow of execution. 

These statements enable decision-making, looping, and jumping within your code, making it dynamic and responsive. 

Mastering control statements in Java is crucial for any developer.

5.1 Decision-Making Statements in Java

**Decision-making statements** in Java allow the program to choose between different actions based on certain conditions. This section will cover the core decision-making statements: `if`, `if-else`, `if-else-if`, and `switch`.

##### 5.1.1 `if` Statement in Java

The `if` statement is a fundamental **control statement in Java**. It checks a Boolean expression, and if the condition is true, the corresponding code block is executed.

**Syntax of `if` Statement in Java:**
```java
if (condition) {
    // code to be executed if condition is true
}
```

**Example of `if` Statement:**
```java
int number = 10;
if (number > 0) {
    System.out.println("The number is positive.");
}
```
In this example, the condition checks if `number` is greater than 0. Since it is true, "The number is positive." is printed.

##### 5.1.2 `if-else` Statement in Java

The `if-else` statement extends the `if` statement to provide an alternative action if the condition is false.

**Syntax of `if-else` Statement:**
```java
if (condition) {
    // code to be executed if condition is true
} else {
    // code to be executed if condition is false
}
```

**Example of `if-else` Statement:**
```java
int number = -5;
if (number > 0) {
    System.out.println("The number is positive.");
} else {
    System.out.println("The number is negative.");
}
```
Here, since `number` is negative, the `else` block executes, printing "The number is negative."

##### 5.1.3 `if-else-if` Ladder in Java

For multiple conditions, the `if-else-if` ladder is a powerful decision-making tool in Java.

**Syntax of `if-else-if` Ladder:**
```java
if (condition1) {
    // code to be executed if condition1 is true
} else if (condition2) {
    // code to be executed if condition2 is true
} else {
    // code to be executed if all conditions are false
}
```

**Example of `if-else-if` Ladder:**
```java
int number = 0;
if (number > 0) {
    System.out.println("The number is positive.");
} else if (number < 0) {
    System.out.println("The number is negative.");
} else {
    System.out.println("The number is zero.");
}
```
This example checks if `number` is positive, negative, or zero, and executes the corresponding code.

##### 5.1.4 `switch` Statement in Java

The `switch` statement in Java is used to select one of many code blocks to be executed. It is a more elegant alternative to the `if-else-if` ladder when dealing with multiple fixed values.

**Syntax of `switch` Statement:**
```java
switch (expression) {
    case value1:
        // code to be executed if expression == value1
        break;
    case value2:
        // code to be executed if expression == value2
        break;
    // you can have any number of case statements
    default:
        // code to be executed if expression doesn't match any case
}
```

**Example of `switch` Statement:**
```java
int day = 3;
switch (day) {
    case 1:
        System.out.println("Sunday");
        break;
    case 2:
        System.out.println("Monday");
        break;
    case 3:
        System.out.println("Tuesday");
        break;
    case 4:
        System.out.println("Wednesday");
        break;
    case 5:
        System.out.println("Thursday");
        break;
    case 6:
        System.out.println("Friday");
        break;
    case 7:
        System.out.println("Saturday");
        break;
    default:
        System.out.println("Invalid day");
}
```
In this example, the `switch` statement selects "Tuesday" based on the value of `day`.

---

#### 5.2 Looping Statements in Java

**Looping statements** in Java are used to repeat a block of code multiple times. Understanding and using loops effectively is key to writing efficient Java programs. This section covers `while`, `do-while`, `for`, and `for-each` loops.

##### 5.2.1 `while` Loop in Java

The `while` loop is a fundamental looping construct in Java that checks the condition before executing the loop body.

**Syntax of `while` Loop:**
```java
while (condition) {
    // code to be executed
}
```

**Example of `while` Loop:**
```java
int i = 1;
while (i <= 5) {
    System.out.println(i);
    i++;
}
```
This `while` loop prints numbers from 1 to 5.

##### 5.2.2 `do-while` Loop in Java

The `do-while` loop is similar to the `while` loop, but the condition is checked after the loop body is executed. This means the loop executes at least once.

**Syntax of `do-while` Loop:**
```java
do {
    // code to be executed
} while (condition);
```

**Example of `do-while` Loop:**
```java
int i = 1;
do {
    System.out.println(i);
    i++;
} while (i <= 5);
```
This loop will also print the numbers from 1 to 5.

##### 5.2.3 `for` Loop in Java

The `for` loop is used when the number of iterations is known before entering the loop. It is concise and powerful.

**Syntax of `for` Loop:**
```java
for (initialization; condition; increment/decrement) {
    // code to be executed
}
```

**Example of `for` Loop:**
```java
for (int i = 1; i <= 5; i++) {
    System.out.println(i);
}
```
This loop prints numbers from 1 to 5 in a more concise manner.

##### 5.2.4 `for-each` Loop in Java

The `for-each` loop simplifies the process of iterating over arrays and collections. It is an enhanced `for` loop introduced in Java 5.

**Syntax of `for-each` Loop:**
```java
for (type element : array/collection) {
    // code to be executed
}
```

**Example of `for-each` Loop:**
```java
int[] numbers = {1, 2, 3, 4, 5};
for (int number : numbers) {
    System.out.println(number);
}
```
This loop iterates through each element of the array and prints it.

---

#### 5.3 Jump Statements in Java

**Jump statements** in Java allow the control to jump to another part of the code. These include `break`, `continue`, and `return`.

##### 5.3.1 `break` Statement in Java

The `break` statement terminates the loop or switch statement, transferring control to the statement following the loop or switch.

**Syntax of `break` Statement:**
```java
break;
```

**Example of `break` Statement:**
```java
for (int i = 1; i <= 10; i++) {
    if (i == 5) {
        break;
    }
    System.out.println(i);
}
```
This loop stops when `i` equals 5, printing numbers 1 to 4.

##### 5.3.2 `continue` Statement in Java

The `continue` statement skips the current iteration of a loop and proceeds with the next iteration.

**Syntax of `continue` Statement:**
```java
continue;
```

**Example of `continue` Statement:**
```java
for (int i = 1; i <= 5; i++) {
    if (i == 3) {
        continue;
    }
    System.out.println(i);
}
```
This loop prints numbers 1, 2, 4, and 5, skipping 3.

##### 5.3.3 `return` Statement in Java

The `return` statement exits from the method and optionally returns a value.

**Syntax of `return` Statement:**
```java
return;
```

**Example of `return` Statement:**
```java
public int addNumbers(int a, int b) {
    return a + b;
}
```
This method returns the sum of `a` and `b`.

---

### Summary of Control Statements in Java

In this chapter on **control statements in Java**, we've explored how to direct the flow of a Java program using decision-making statements (`if`, `if-else`, `if-else-if`, `switch`), looping statements (`while`, `do-while`, `for`, `for-each`), and jump statements (`break`, `continue`, `return`). Mastery of these control structures is essential for writing efficient and effective Java programs.
"