C - Using the do-while loop

Introduction

The do-while loop has the following structure:

do 
{ 
     statement(s); 
} while (condition); 

Because of its inverse structure of while loop, do-while loop is always executed at least one time.

The following code shows a Fibonacci Sequence.

Demo

#include <stdio.h> 

int main() /*w  w w. j  a v a2  s .c  om*/
{ 
     int fibo,nacci; 

     fibo=0; 
     nacci=1; 

     do 
     { 
         printf("%d ",fibo); 
         fibo=fibo+nacci; 
         printf("%d ",nacci); 
         nacci=nacci+fibo; 
     } while( nacci < 300 ); 

     putchar('\n'); 
     return(0); 
}

Result

Related Topic