The Do-While Loop

Learn about the do-while loop, which is another way to repeat a code segment.

We'll cover the following...

The do-while loop executes the loop body at least once and checks the condition before the end.

Press + to interact
let numbers = [19, 65, 9, 17, 4, 1, 2, 6, 1, 9, 9, 2, 1];
function sumArray( values ) {
if ( values.length == 0 ) return 0;
let sum = 0;
let i = 0;
do {
sum += values[i];
i += 1;
} while ( i < values.length );
console.log( 'The loop was executed ' + i + ' times' );
return sum;
}
sumArray( numbers );

Notice the first line in the function. We have to exit the sumArray function before reaching the ...