0% found this document useful (0 votes)
6 views16 pages

Do While Looping Presentation (2) (Read-Only)

The document explains the do-while loop, a control flow statement that guarantees at least one execution of a code block before checking a Boolean condition. It contrasts the do-while loop with the standard while loop, highlighting that the condition is evaluated after execution in the former. Key use cases include scenarios where code must run at least once, such as displaying menus or ensuring operations are performed regardless of initial conditions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views16 pages

Do While Looping Presentation (2) (Read-Only)

The document explains the do-while loop, a control flow statement that guarantees at least one execution of a code block before checking a Boolean condition. It contrasts the do-while loop with the standard while loop, highlighting that the condition is evaluated after execution in the former. Key use cases include scenarios where code must run at least once, such as displaying menus or ensuring operations are performed regardless of initial conditions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

DO….

WHILE
LOOPING
PRESENTATION BY:
DULULA H240209R
DZIMANO H240108Y
GAMBIZA H240701Y
GARAPO H240337N
HWENGWERE H240320E
DO..WHILE LOOPING

A do while loop is a control flow statement that executes a block of code at least once,
and then repeatedly executes the block, or not, depending on a given Boolean condition
at the end of the block.
•OR

A do-while loop in programming executes a block of code at least once,


regardless of the condition then repeatedly executes it as long as a specified
condition remains true. The key difference from a standard while loop is that the
condition check happens after the code block's execution, guaranteeing at least
one run.
HERE'S A BREAKDOWN: SYNTAX CODE

 do {
// Code to be
executed}
 while (condition);
Example (1):
#include <stdio.h>
int main(void)
{
int i=0;
do
{
printf("i: %d\n", i);
i++;
}while (i<5);
printf("loop completed!\n");

return 0;
}
EXAMPLE 2
#include <stdio.h>
int main ()
{
int i =1;
do
{
printf(“%d\n”,i);
i++;
}
while (i<=5);
return 0;
}
KEY POINTS
 The do-while loop is guaranteed to execute its code block at least
once ,unlike the while loop ,which might not execute at all if the
condition is initially false .
 The condition is checked after the code block is executed .
 In the example ,the loop will print the numbers 1 to 5.
 How it works:
1. The code block inside the do statement is
executed.
2. The condition in the while statement is
evaluated.
3. If the condition is true, the code block is
executed again, and the process repeats from
step 2.
4. If the condition is false, the loop terminates
 Use Cases:
o When you need to execute a block of code at
least once before checking a condition.
o For tasks like displaying a menu and allowing
the user to make a choice, where the menu
should always be shown at least once.
o When you need to ensure that a certain
operation is performed at least once,
regardless of the initial condition.
The difference between while command and do while
command is:

Condition Check Timing:


while: Condition is checked before the loop body executes.
do-while: Condition is checked after the loop body executes.
Guaranteed Execution:
while: The loop body may not execute at all if the condition is
false initially.
do-while: The loop body will execute at least once, regardless
of the condition.
When using while loop, the code won’t execute
 When using do…while loop

You might also like