C - For Loop Example
Tutorials Point


  Learning C
  C Function References
  C Useful Resources
  Selected Reading

Copyright © 2014 by tutorialspoint



  Home     References     About TP     Advertising  
Latest C Tutorial

for loop examples

previous

Try following example to understand for 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;
    int j = 10;

    for( i = 0; i <= j; i ++ )
    {
       printf("Hello %d\n", i );
    }
}

This will produce following output:

Hello 0
Hello 1
Hello 2
Hello 3
Hello 4
Hello 5
Hello 6
Hello 7
Hello 8
Hello 9
Hello 10

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

#include <stdio.h>

main()
{
    int i;

    for( i = 0; i <= j; i ++ )
    {
       printf("Hello %d\n", i );
       if( i == 6 )
       {
          break;
       }
    }
}

This will produce following output:

Hello 1
Hello 2
Hello 3
Hello 4
Hello 5


previous Printer Friendly

Advertisements


  

Advertisements



Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements