C for loop
Description
The for loop is usually used when the number of iterations is predetermined.
C for loop syntax
The format of the for loop is
for (starting_condition; continuation_condition ; action_per_iteration){
statement1;
statement2 ;
}
starting_condition
is executed only once before looping.
continuation_condition
is a Boolean expression. If not given, it is assumed to be true.
If continuation_condition
is false, the loop is terminated.
After execution of the repeat section, action_per_iteration
is executed.
The expressions starting_condition
, continuation_condition
and action_per_iteration are all optional.
Example
#include <stdio.h>
//from w w w . j a v a 2 s. c o m
main(){
int i,n = 5;
for(i = 0; i < n; i = i+1){
printf("the numbers are %d \n",i);
}
}
The code above generates the following result.
Omit all three parts in for loop
#include <stdio.h>
//from ww w .java2 s. co m
int main()
{
printf("~ to exit");
for(;;)
{
char ch=getchar();
if(ch=='~')
{
break;
}
}
}
The code above generates the following result.
for loop with a comma operator
#include <stdio.h>
/*from w ww .jav a2s .c o m*/
main(){
int i, j;
for (i = 0, j = 10; i < 3 && j > 8; i++, j--){
printf (" the value of i and j %d %d\n",i, j);
}
}
The code above generates the following result.
Initialize loop control variable outside the for statement
#include <stdio.h>
/* ww w .ja v a 2 s. c om*/
int main(void)
{
int count = 1;
for( ; count <= 10 ; ++count)
printf("\n%d", count);
printf("\nWe have finished.\n");
return 0;
}
The code above generates the following result.
Example - Use for loop to print a rectangle
Use for loop to print a rectangle
#include <stdio.h>
/* w ww. j ava 2s .c om*/
int main(void)
{
printf("\n**************");
for(int count = 1 ; count <= 8 ; ++count)
printf("\n* *");
printf("\n**************\n");
return 0;
}
The code above generates the following result.
Example - Sum integers in for statement
Sum integers in for statement
#include <stdio.h>
/*www . jav a 2 s .com*/
int main(void)
{
long sum = 0L;
int count = 0;
printf("\nEnter the number of integers you want to sum: ");
scanf(" %d", &count);
for (int i = 1 ; i<= count ; sum += i++ );
printf("\nTotal of the first %d numbers is %ld\n", count, sum);
return 0;
}
The code above generates the following result.
Example - for loop backward
for loop backward
//from ww w. j ava 2 s . c o m
#include <stdio.h>
int main(void)
{
long sum = 0L;
int count = 10;
int i;
for (i = count ; i >= 1 ; sum += i-- );
printf("\nTotal of the first %d numbers is %ld\n", count, sum);
return 0;
}
The code above generates the following result.
Example - infinite for loop
infinite for loop
#include <stdio.h>
#include <ctype.h> /* For tolower() function */
//w w w. j ava 2 s .co m
int main(void)
{
char answer = 'N';
printf("\nThis program calculates the average of any number of values.");
for( ;; ) /* Indefinite loop */
{
printf("Do you want to enter another value? (Y or N): ");
scanf(" %c", &answer );
if( tolower(answer) == 'n' ){
break;
}
}
return 0;
}
The code above generates the following result.
Example - Nest for loop with two different control variables
Nest for loop with two different control variables
#include <stdio.h>
/*www . j a va2s .co m*/
int main(void)
{
long sum = 0L;
int count = 10;
int i,j;
for(i = 1 ; i <= count ; i++ ){
sum = 0L;
for(j = 1 ; j <= i ; j++ )
sum += j;
printf("\n%d\t%ld", i, sum);
}
return 0;
}
The code above generates the following result.
Example - Use char variable to control for loop
Use char variable to control for loop
#include <stdio.h>
//from w ww .ja v a 2s . com
int main()
{
char b,a='a' ;
for(b='A';b<'K';b++) {
printf("%d - %c ",a,b);
}
putchar('\n'); /* end of line */
return(0);
}
The code above generates the following result.