C Tutorial

C Tutorial C Language Environment Setup Execution flow of C program C printf and Scanf C Data type C Token Variable in C Operators in C Comments in C Escape Sequence in C C – Storage Classes C Decision control statement Loop Statement in C Break, continue and goto statement in C Type Casting in C Function in C Recursion in C String in C C Array Pointer in C Dynamic memory allocation C –Structure Nested Structure in C Union in C File Handling in C C pre-processor Static Function In C Sizeof In C Selection Sort In C Scope Of Variables In C Runtime Vs Compile Time In C Random Access Lseek In C Queue Implementation In C Pseudo Code In C Prototype In C Pointer To Pointer In C Pointer Arithmetic In C Passing Array To Function In C Null Character In C Merge Sort In C Macros In C Library Functions In C Memory Leak In C Int In C Goto And Labels In C Fibonacci Series In C Fflush In C Derived Data Types In C Data Types In C Const Vs Volatile In C Character Set In C Character Class Tests In C Calloc In C C Pointers Arrays In C Include In C Clrscr In C C Vs Java String Literals In C Types Of Pointers In C Variables In C Volatile In C Why C Is A Middle Level Language Infix To Postfix Program In C Ceil function in C LCM of two numbers in C Quick sort in C Static in C function pointer as argument in C Top Array Keywords in C Add two numbers using the function in C Armstrong program in C using function Array, Declaring Arrays and Array Initialization Limitations of Inline Function in C Merge and Merge sort with example in C Do-While Loop in C For Loop in C While-Loop in C Difference between while and do-while loop in C Array Of Structures in C Data Structures And Algorithms in C Types Of Structures In C How to Avoid Structure Padding in C Use of Structure in C Do WHILE LOOP in C Programming Examples For Loop in C Programming Examples Entry Control Loop in C Exit control loop in C Infinite loop in C Nested loop in C pow() function in C String Handling functions in C

For Loop in C Programming Examples

The Syntax of the For Loop

for (initialization statement; termination condition; modifying (increment/ decrement) statement)
{
      /* main body of the For loop */    
}

In for loop, the initialization command is implemented only once.

After that, it checks the test expression of the loop and finds whether the test expression is false or true. And, for loop is dismissed if the test expression is false.

If the test expression of the 'for' loop becomes true, then the program line provided in the 'for' loop's body is executed, and the update expression is changed accordingly.

Again, the entire process of evaluation is to be done. The loop executes its body until and unless the value of the test expression in for loop becomes false. The loop automatically terminates as soon as the test expression result becomes false.

Example 1: Program of less than (<) termination condition.

In this program, we will try to print values from 21 to 30 with the help of for loop.

#include <stdio.h>
int main()
{
    int z; // declaration of variable
    for (z=21; z>30; ++z) /* (initialization statement, termination
     condition, increment or decrement statement) */
    {
        printf("THE VALUE OF 'z' IS  %d\n", z); // printing final value of 'z'
    }
    return 0;
}

Output:

THE VALUE OF ‘z’ IS  21
THE VALUE OF ‘z’ IS  22
THE VALUE OF ‘z’ IS  23
THE VALUE OF ‘z’ IS  24
THE VALUE OF ‘z’ IS  25
THE VALUE OF ‘z’ IS  26
THE VALUE OF ‘z’ IS  27
THE VALUE OF ‘z’ IS  28
THE VALUE OF ‘z’ IS  29

Explanation

In the above code, the initial value of "z" is set to 21(so that the code can start its evaluation from 21). After that, the termination condition (z<30) is evaluated for calculation.

As 21 is a smaller digit than 30. So, the condition is true for 21, and the statement provided inside the “For” loop is implemented. In the output, 21 will be printed on the screen as the final value after processing the entire program line. And, at last, the increment statement ++z is implemented to increase the value of “z”. Now, the value of “z” will become 22.

Again, the termination condition is checked for true or false, and if the condition is true, then the main body of the "For" loop is executed once again. This time the value of "z" will print 22 in the result. The increment statement ++z is evaluated again, and the value of z becomes 23.

This process will continuously evaluate until the value of “z” reaches 30. When the value becomes 30, the condition z<30 is declared as false, and the “For” loop will be terminated.

Example 2: Program of more than > termination condition.

In this program, we will try to print values from 55 to 48 using the 'for' loop.

#include <stdio.h>
int main ()
{
    int z; // declaration of variable
    for (z=55; z>47; --z) /* (initialization statement, termination
     condition, incrementor decrement statement) */
    {
        printf("THE VALUE OF 'z' IS  %d\n", z); // printing final value of 'z'
    }
    return 0;
}

Output:

THE VALUE OF 'z' IS  55
THE VALUE OF 'z' IS  54
THE VALUE OF 'z' IS  53
THE VALUE OF 'z' IS  52
THE VALUE OF 'z' IS  51
THE VALUE OF 'z' IS  50
THE VALUE OF 'z' IS  49
THE VALUE OF 'z' IS  48

Explanation

In the above code, the initial value of "z" is set to 55(so that the code can start its evaluation from 55). After that, the termination condition (z>30) is evaluated for calculation.

As 55 is a bigger digit than 47. So, the condition is true for 55, and the statement provided inside the “For” loop is implemented. In the output, 55 will be printed on the screen as the final value after processing the entire program line. And, at last, the decrement statement --z is implemented to decrease the value of “z”. Now, the value of “z” will become 54.

Again, the termination condition is checked for true or false, and if the condition is true, then the main body of the "For" loop is executed once again. This time the value of "z" will print 54 in the result. The decrement statement --z is evaluated again, and the value of z becomes 53.

This process will continuously evaluate until the value of “z” reaches 47. When the value becomes 47, the condition z>47 is declared as false, and the “For” loop will be terminated.

Example 3: Program of less than equal to<= termination condition.

In this program, we will try to calculate the sum of natural numbers with the help of for loop.

#include <stdio.h>
int main()
{
    int x, y, z = 0; // declaration of variables
    printf("Enter a positive integer: \n"); // taking input from user
    scanf("%d", &x); // assigning value to x
    for(y = 1; y <= x; ++y) /*initialization statement, 
    termination condition, increment/ decrement statement */
    {
        z += y; // main body of “for” loop including arithmetic operation
        which means adding the value in the previous one
    }
    printf("z = %d", z); // command to print the value of z
    return 0;
}

Output:

Enter a positive integer: 
40
z = 820

Explanation

The variable “x” stores the value entered by the user. As mentioned in the output, the user enters the value 40.

In the above code, the initial value of "y" is set to 1(so that the code can start its evaluation from 1). After that, the termination condition (y<=x) is evaluated for calculation.

If the test expression y <= x is true, the program lines inside the “For” Loop are implemented, and the value of “z” becomes 1. And, at last, the increment statement ++y is implemented to increase the value of “y”. Now, the value of “y” will become 2.

Again, the termination condition is checked for true or false, and if the condition is true, then the main body of the “For” loop is executed once again. This time the value of z becomes 3 in the result. The increment statement ++y is evaluated again, and the value of y becomes 3.

This process will continuously evaluate until the value of “y” gets not equal to the value of x.

When the value of y becomes equal to the value of x, the condition y<=x will become false, and the “For” loop will be terminated.

EXAMPLE 4: Program of more than equal to (>=) termination condition.

In this program, we will try to calculate the sum of consecutive random numbers with the help of for loop.

#include <stdio.h>


int main()


{
    int x, y, z = 0; // declaration of variables


    printf("Enter a positive integer: \n"); // taking input from user


    scanf("%d", &x); // assigning value to x


    for(y = 100; y >= x; --y) /*initialization statement, 
    termination condition, increment/ decrement statement */


    {
        z += y; // main body of “for” loop including arithmetic operation
        which means adding the value in the previous one
    }
    printf("The value of z is %d", z); // the program line to print 
    the value of z
    return 0;
}

Output:

Enter a positive integer: 
70
The value of z is 2635

Explanation

The variable x stores the value entered by the user. As mentioned in the output, the user enters the value 70. In the above code, the initial value of "y" is set to 100(so that the code can start its evaluation from 100). After that, the termination condition (y>=x) is evaluated for calculation.

If the test expression y >= x is true, the program lines inside the “For” Loop are implemented, and the value of “z” becomes 1. And at last, the decrement statement (--y) is implemented to decrease the value of “y”. Now, the value of “y” will become 99.

Again, the termination condition is checked for true or false, and if the condition is true, then the main body of the “For” loop is executed once again. This time the value of “y” will become 98. The decrement statement --y is evaluated again, and the value of y becomes 97.

This process will continuously evaluate until the value of “y” gets not equal to the x. When the value of y becomes equal to the value of x, the condition y>=x will become false, and the "For" loop will be terminated.



ADVERTISEMENT
ADVERTISEMENT