Copyright © 2014 by tutorialspoint
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; } } }
Hello 1 Hello 2 Hello 3 Hello 4 Hello 5
Advertisements
Get certified by completing the course