while loop examples

« Previous

Try following example to understand while loop. You can put the following code into a test.c file and then compile it and then run it.

#include <stdio.h>
main()
{
    int i = 10;
    while ( i > 0 )
    {
       printf("NewFile. %d\n", i );
       i = i -1;
    }
}
                                        

Output:

NewFile. 10
NewFile. 9
NewFile. 8
NewFile. 7
NewFile. 6
NewFile. 5
NewFile. 4
NewFile. 3
NewFile. 2
NewFile. 1
                            

You can make use of break to come out of while loop at any time.

#include <stdio.h>
main()
{
    int i = 10;
    while ( i > 0 )
    {
       printf("NewFile. %d\n", i );
       i = i -1;
       if( i == 6 )
       {
          break;
       }
    }
}
                            

Output:

NewFile. 10
NewFile. 9
NewFile. 8
NewFile. 7
                            


« Previous
Have Any Suggestion? We Are Waiting To Hear from YOU!

Your Query was successfully sent!