Curriculum
Course: C basic
Login

Curriculum

C basic

C Introduction

0/1

C Get Started

0/1

C Comments

0/1

C Constants

0/1

C Operators

0/1

C Break and Continue

0/1

C User Input

0/1

C Memory Address

0/1

C Structures

0/1
Text lesson

Do/While Loop

The Do/While Loop

The do/while loop, a variant of the while loop, executes the code block once, then proceeds to check if the condition is true. If the condition holds true, it repeats the loop until the condition evaluates to false.

Syntax

do {
  // code block to be executed
}
while (condition);

In the following example employing a do/while loop, the loop will be executed at least once, regardless of whether the condition is false. This occurs because the code block is executed before the condition is evaluated:

Example

int i = 0;

do {
  printf(“%d\n”, i);
  i++;
}
while (i < 5);

 

Remember to increment the variable utilized in the condition, or else the loop may never terminate!