Control Structures

1. Control Structures

1.1. Example 1:

java

public class SequentialExample {

    public static void main(String[] args) {

        int a = 5;

        int b = 10;

        int sum = a + b;

        System.out.println("Sum: " + sum); // Output: Sum: 15

    }

}

Notes:

· No special syntax is required for sequential execution.

· All statements are executed unless altered by other control structures.

2.2 Decision-Making (Conditional) Structures

Conditional statements execute a block of code based on whether a condition evaluates to true or false.

2.2.1 if Statement

Executes a block of code if a condition is true.

Syntax:

if (condition) {

    // Code to execute if condition is true

}

Example:

public class IfExample {

    public static void main(String[] args) {

        int num = 10;

        if (num > 0) {

            System.out.println(num + " is positive"); // Output: 10 is positive

        }

    }

}

2.2.2 if-else Statement

Executes one block if the condition is true and another if it’s false.

Syntax:

if (condition) {

    // Code if condition is true

} else {

    // Code if condition is false

}