C for loop with Example
The most interesting and most difficult of all loops is the C for loop. The name for loop comes from the typical description of classic for loop: For all values of variable from value1 to value2 in steps of value3, repeat the following sequence of commands.
You typically use the for loop to execute a block of statements a given number of times. Let’s suppose you want to display the numbers from 1 to 10. Instead of writing ten statements that call printf(), you could write this:
for(int count = 1 ; count <= 10 ; ++count) { printf(" %d", count); }
The C for loop operation is controlled by what appears between the parentheses that follow the keyword for. The execution that you want to repeat each time the loop repeats is the block containing the statement that calls printf(). Because you have just a single statement here, you could avoid and above example shows the three control expressions that are separated by semicolons. These expressions control the operation of the loop.
The syntax of for C for Loop statement is given by for statement: = for (expr ; expr ; expr) statement
Example,
for (i = 1; i <= n; ++i) factorial ~'= i; for (j = 2; k % j == 0; ++j) { } printf(lf%d is a divisor of %d\nlf, j, k); sum += j; }
The first control expression, int count = 1, is executed only once, when the loop starts. For example, this creates variable, count, with the initial value 1. This variable is local to the loop and does not exist outside the loop.
The second control expression must belogical expression that ultimately can result in true or false. In this case, it’s the expression count <= 10, which will evaluate to true as long as count is not greater than ten. The second expression is evaluated at the beginning of each loop iteration. If the expression evaluates to true, the loop continues, and if it’s false, the loop ends and execution of the program continues with first statement following the loop block or loop statement.
The third control expression, ++count in this case, is executed at the end of each loop iteration. Here you use the increment operator to add 1 to the value of count. On the first iteration, count will be 1, so the printf() will output 1. On the second iteration, count will have been incremented to 2, so the printf() will output the value 2. This will continue until the value 10 has been displayed.
C for loop with example
This example program prints out all the primes numbers between 1 and the macro value maxint. Prime numbers are numbers which cannot be divided by any number except 1 without leaving a remainder. This example explain how C For loop works with program,
/************************************************/ /* Prime Number Generator #1 */ /************************************************/ #include <stdio.h> #define MAXINT 500 #define TRUE 1 #define FALSE 0 /*************************************************/ /* Level 0 */ /*************************************************/ main () { int i; for (i = 2; i <= MAXINT; i++) { if (prime(i)) { printf ("%5d",i); } } } /*************************************************/ /* Level 1 */ /*************************************************/ prime (i) /* check for a prime number */ int i; { int j; for (j = 2; j <= i/2; j++) { if (i % j == 0) { return FALSE; } } return TRUE; }