Participation | Looping Examples

Looping examples with diffrent fruits. Click the buttons to run the code and check the console log for output.

Example 1: For Loop

let fruits = ["Apple", "Banana", "Cherry", "Mango", "Peach"];

function forLoopExample() {
    for (let i = 0; i < fruits.length; i++) {
        console.log(fruits[i]);
    }
}

Example 2: For...of Loop

function forOfLoopExample() {
    for (let fruit of fruits) {
        console.log(fruit);
    }
}

Example 3: forEach Loop

function forEachLoopExample() {
    fruits.forEach(function(fruit) {
        console.log(fruit);
    });
}

Example 4: While Loop

function whileLoopExample() {
    let i = 0;
    while (i < fruits.length) {
        console.log(fruits[i]);
        i++;
    }
}

Example 5: Do...while Loop

function doWhileLoopExample() {
    let i = 0;
    do {
        console.log(fruits[i]);
        i++;
    } while (i < fruits.length);
}