Java Decision Marking and Looping

 

Java looping

In Java, **looping** allows a block of code to be executed repeatedly based on a condition. Loops are especially useful when you need to perform repetitive tasks, process collections, or iterate over arrays.

There are four main types of loops in Java:

1. For Loop


The `for` loop is used when the number of iterations is known in advance. It consists of three parts: initialization, condition, and update.


for (initialization; condition; update) {
    // code to be executed
}


Example


for (int i = 0; i < 5; i++) {
    System.out.println("Iteration: " + i);
}


In this example:


- Initialization: `int i = 0` (the loop starts at 0).


- Condition: `i < 5` (the loop runs while `i` is less than 5).


Update: `i++` (the value of `i` is incremented by 1 after each iteration).

2. while Loop


The `while` loop repeats a block of code while a given condition is `true`. It is used when the number of iterations is unknown, and the condition is checked before each iteration.


while (condition) {
    // code to be executed
}

Example:


int i = 0;
while (i < 5) {
    System.out.println("Iteration: " + i);
    i++;
}


In this example, the loop will continue running as long as the condition `i < 5` is true.

3. do-while` Loop


The `do-while` loop is similar to the `while` loop, but the condition is checked **after** the loop body is executed. This ensures that the loop executes at least once, even if the condition is `false` from the beginning.

do {
    // code to be executed
} while (condition);


Example:

int i = 0;
do {
    System.out.println("Iteration: " + i);
    i++;
} while (i < 5);

Here, the loop will run at least once, and then check the condition `i < 5`.

4. Enhanced `for` Loop (for-each loop)


The enhanced `for` loop (also known as the "for-each" loop) is specifically used to iterate over arrays or collections. It is simpler than a regular `for` loop because you don't need to manage an index variable.

```java
for (type element : array) {
    // code to be executed for each element
}

Example:


int [ ] numbers = {1, 2, 3, 4, 5};
for (int num : numbers) {
    System.out.println("Number: " + num);
}


In this example, the loop iterates over each element in the `numbers` array and prints it.

5. break and continue Statements in Loops


break: Exits the loop immediately, regardless of the loop condition.


continue: Skips the current iteration and moves to the next iteration of the loop.

Example of `break`:


for (int i = 0; i < 10; i++) {
    if (i == 5) {
        break;  // exits the loop when i equals 5
    }
    System.out.println(i);
}

Example of `continue`:


for (int i = 0; i < 10; i++) 

{
    if (i == 5) 

    {
        continue;  // skips the iteration when i equals 5
    }
    System.out.println(i);
}

Summary of Looping in Java

for loop: Best when the number of iterations is known.


while loop: Best when the number of iterations is not known and the condition is checked before each iteration.


do-while loop: Similar to `while`, but guarantees at least one iteration.


Enhanced for loop : Best for iterating over arrays or collections (for-each).

 Example Combining Loops:


 class LoopExamples {
    public static void main(String[] args) {
        // for loop example
        for (int i = 0; i < 3; i++) {
            System.out.println("for loop: " + i);
        }

        // while loop example
        int j = 0;
        while (j < 3) {
            System.out.println("while loop: " + j);
            j++;
        }

        // do-while loop example
        int k = 0;
        do {
            System.out.println("do-while loop: " + k);
            k++;
        } while (k < 3);

        // Enhanced for loop example
        int [ ] numbers = {10, 20, 30};
        for (int num : numbers) {
            System.out.println("Enhanced for loop: " + num);
        }
    }
}

These loops help you repeat tasks efficiently in Java, reducing redundancy and making your code more powerful and flexible.

Comments

Popular posts from this blog

digital marketing ppt-u1

SOFTWARE

cn lab

Computer Operations and Performing - D L Unit-1-1

DBMS Degree Lab Records

DS-Record-mca-04) write a program for evaluating a given postfix expression using stack.

Java unit-1 history , C++ vs Java , java environment

Access the Internet to Browse Infromation & E-Mail Operation- D L Unit-2-1

OAT-Record-01) Visiting card

java program structure,java Tokens, keywords,