Sobes.tech
Junior — Middle

Can you name types of loops used in programming besides the For loop?

sobes.tech AI

Answer from AI

In addition to the for loop, the following types of loops are widely used in programming:

  • while — executes the loop body as long as the condition is true.

    int i = 0;
    while (i < 5) {
        System.out.println(i);
        i++;
    }
    
  • do-while — executes the loop body first, then checks the condition; guarantees at least one execution.

    int i = 0;
    do {
        System.out.println(i);
        i++;
    } while (i < 5);
    
  • foreach (enhanced for) — used for iterating over elements of collections or arrays.

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

These loops allow flexible organization of repetitive actions in code.