Explain the difference between a while loop and a do-while loop.

Both while and do-while are used for repeating a block of code, but they differ in how they check the loop condition. In a while loop, the condition is checked before the code block is executed, meaning the loop may not execute at all if the condition is false initially. In contrast, a do-while loop checks the condition after the code block has executed, ensuring that the code block runs at least once. Here’s an example:

    • while (condition) { /* code */ }
    • do { /* code */ } while (condition);