Java Tutorial

Operators

Flow Control

String

Number and Date

Built-in Classes

Array

Class and Object

Inheritance and Polymorphism

Exception Handling

Collections, Generics and Enumerations

Reflection

Input/Output Stream

Annotation

Summary Of Java Selection Structures And Looping Structures

Java provides selection structures and looping structures that allow developers to control the flow of their programs based on certain conditions. Here is a summary of the selection structures and looping structures available in Java:

  1. Selection Structures:

    • The if statement: Allows a program to execute a block of code if a certain condition is true.
    • The if-else statement: Allows a program to execute one block of code if a certain condition is true, and another block of code if the condition is false.
    • The switch statement: Allows a program to execute different blocks of code depending on the value of an expression.
  2. Looping Structures:

    • The while loop: Repeatedly executes a block of code as long as a certain condition is true.
    • The do-while loop: Repeatedly executes a block of code at least once, and continues to execute the block as long as a certain condition is true.
    • The for loop: Repeatedly executes a block of code a specific number of times.
    • The foreach loop: Iterates over the elements of an array or a collection.

These structures are essential for building complex programs that perform different tasks based on certain conditions or that need to repeat certain tasks multiple times. By combining these structures, developers can create programs that are flexible and efficient.

It's important to note that the selection and looping structures in Java can be nested within each other, allowing for even more complex control flow within a program.

  1. Using switch-case in Java

    The switch-case statement allows you to perform different actions based on the value of an expression.

    int dayOfWeek = 3;
    
    switch (dayOfWeek) {
        case 1:
            System.out.println("Monday");
            break;
        case 2:
            System.out.println("Tuesday");
            break;
        // ... other cases
        default:
            System.out.println("Invalid day");
    }
    
  2. Nested if-else statements in Java

    Nesting if-else statements allows you to create more complex decision structures.

    int age = 25;
    boolean isStudent = false;
    
    if (age > 18) {
        if (isStudent) {
            System.out.println("Adult student");
        } else {
            System.out.println("Adult");
        }
    } else {
        System.out.println("Minor");
    }
    
  3. Ternary operator usage in Java selection structures

    The ternary operator is a concise way to express simple conditional statements.

    int num = 5;
    String result = (num % 2 == 0) ? "Even" : "Odd";
    
  4. While loop in Java and its variations

    The while loop repeatedly executes a block of code while a given condition is true.

    int count = 0;
    while (count < 5) {
        System.out.println("Count: " + count);
        count++;
    }
    
  5. Enhanced for loop in Java for arrays and collections

    The enhanced for loop simplifies iteration over arrays and collections.

    int[] numbers = {1, 2, 3, 4, 5};
    
    for (int num : numbers) {
        System.out.println(num);
    }
    
  6. Nested loops in Java programming

    You can nest loops to create more complex patterns or iterate through multi-dimensional arrays.

    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            System.out.print(i + "," + j + " ");
        }
        System.out.println();
    }
    
  7. Java do-while loop examples and scenarios

    The do-while loop executes a block of code at least once, and then repeatedly as long as a specified condition is true.

    int i = 0;
    do {
        System.out.println("Iteration: " + i);
        i++;
    } while (i < 5);
    
  8. Using loops for menu-driven programs in Java

    Loops are commonly used in menu-driven programs to repeatedly display options until an exit condition is met.

    Scanner scanner = new Scanner(System.in);
    int choice;
    
    do {
        System.out.println("1. Option 1");
        System.out.println("2. Option 2");
        System.out.println("3. Exit");
        System.out.print("Enter your choice: ");
        choice = scanner.nextInt();
    
        switch (choice) {
            case 1:
                // Process Option 1
                break;
            case 2:
                // Process Option 2
                break;
        }
    } while (choice != 3);
    
  9. Looping through arrays with conditional statements in Java

    You can loop through arrays and apply conditional statements based on array elements.

    int[] numbers = {10, 20, 30, 40, 50};
    
    for (int num : numbers) {
        if (num > 30) {
            System.out.println("Number greater than 30: " + num);
        }
    }
    
  10. Control flow with if-else inside loops in Java

    Adding if-else statements inside loops allows you to conditionally execute code during each iteration.

    int[] numbers = {10, 20, 30, 40, 50};
    
    for (int num : numbers) {
        if (num % 2 == 0) {
            System.out.println("Even number: " + num);
        } else {
            System.out.println("Odd number: " + num);
        }
    }
    
  11. Break and continue statements in Java loops

    The break statement is used to exit a loop prematurely, and continue is used to skip the rest of the code in the current iteration.

    for (int i = 0; i < 5; i++) {
        if (i == 3) {
            break; // Exit the loop when i is 3
        }
        System.out.println("Iteration: " + i);
    }