Abstract

The do statement provides an iterative structure to condidionally repeat execution of a section of code sy least once.

Usage

The do statement is used as an iteration, or looping, structure.  Loop control occurs after the body of the loop. The body of the loop will be executed one or more times.

Syntax

Syntax Diagrams

do Statement Syntax Diagram

BNF

do-statement
::= 'do' <statement> 'while' '(' <expression> ')' ';'

EBNF

do-statement
::= 'do' <statement> 'while' '(' <expression> ')' ';'

Semantics

do-statement
The do-while statement provides iteration, or looping, control.
The body of a do-while is always executed at least once.
The expression is evaluated after the statement has been executed.
If the expression evaluates to true, the process is repeated.
If the expression evaluates to false, execution continues with the next statement after the if statement.
expression
A boolean false, an arithmetic 0, or a null pointer are all interpreted as false.
A boolean true, an arithmetic expression not equal to 0, or a non-null pointer are all interpreted as true.

Flowchart

156

Remarks

Example

#include <stdio.h>

int main () {
  unsigned i;

  i = 0;
  do {
    if (i % 2 == 1) {
      printf ("%u is an odd number\n", i);
    }
    ++i;
  } while (i < 10);
  
  return 0;
}

Output


    

Reference Links

Statement <statement>
Compound Statement <compound-statement>
for Statement <for-statement>
while Statement <while-statement>

External Links

MSDN The do-while Statement (C) Visual Studio
MSDN The do-while Statement (C) Visual Studio 8.5
MSDN The do-while Statement (C) Visual Studio 8.0
MSDN The do-while Statement (C) Visual Studio 7.1
MSDN The do-while Statement (C) Visual Studio 6

Other Languages

C Now
C do Statement
C++ Now C++ do Statement
Java Now Java do Statement
Pascal Now Pascal Repeat Statement
PHP Now PHP do Statement