×

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.


Related Topics

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...

6 minutes read.

C Program of Fencing the Ground

The college's ground is rectangular. Fencing the ground the management makes the decision to construct a fence around the ground. They planned to wrap a thick rope around the ground...

1 minute read.

Comma Operator in C

What is a comma operator? The comma operator in the C programming language has the least priority. The comma operator is essentially a binary operator that operates on the first operand...

4 minutes read.

How to convert a number to words in C

There are many ways available for converting an integer or a number to its word form. For example, consider a number as 12345. This number can be represented in two...

3 minutes read.

Double Specifier in C

What is a double specifier? The term double denotes the double data type in C. It more accurately depicts floating point numbers. The idea that it has twice the precision of...

3 minutes read.

C vs Java Strings

String in C In C, we can define a string as a bunch of characters. A character array is distinguished from a string by the presence of the special character '\0'...

5 minutes read.

Return array from function in C

Return array from function in C C programming does not require the return to a function of a whole array as an argument. However, you can return a pointer to an array without...

2 minutes read.

String in C

String is a collection of character or group of characters. In array, string of character is terminated by a null value “\0” and enclose between double quote. We can declare...

2 minutes read.

Loop Questions in C

Question 1: What is For loop syntax? Ans: The syntax that has been used for the ‘for’ loop in C programming language is: for (initialization statement /*for providing a value to variables*/; test...

19 minutes read.

strtok() and strtok_r() functions in C with examples

strtok() and strtok_r() functions in C with examples C offer various strtok() and strtok r() methods for a string to be separated by a certain delimiter. Dividing a string is a...

2 minutes read.

Caesar Cipher Program in C

What is Caesar Cipher? The Caesar Cipher is a type of substitution cipher that is named after Julius Caesar, who is said to have used it to encrypt messages sent to...

2 minutes read.

Derived Data Types in C

A variable in a program occupies some space in the computer's memory where some value is stored. Each variable in C has an associated data type. A value to be...

4 minutes read.

C Data type

Data Types in C with Examples Data types are used to define the type of data that a variable can store to perform a specific operation.ANSI C provides three types of...

2 minutes read.

C Pointers

Pointers in C A pointer is a variable, which contains the address of another variable, i.e., it’s a variable which has the address of another variable as its value. The utilization...

4 minutes read.

pow() function in C

pow() function is one of the in-built functions present in math.h header file Power function is used to calculate the powers of the given number. The syntax of the power...

3 minutes read.

Prime Number code in C

Prime Number Programs in C language In this tutorial, we will learn how to check whether the given number is a prime number or not, and how to print all the...

4 minutes read.

C Decision control statement

Decision Making C conditional statements are used to specify one or more conditions. The statements will be executed if the condition becomes true otherwise alternative statement will be executed if the...

3 minutes read.

Sum of digits in C++

Sum of digits in C++ There are various ways to find the sum of the digits of a number in C++. We can use containers like arrays or other simple cases...

4 minutes read.

Merge and Merge sort with example in C

Assume we have two ordered Integer arrays, a[] and b[]. If we wish to combine them into another ordered array, such as c[], we can use the following straightforward technique. Compare...

3 minutes read.

Difference between Scope and Lifetime in C

In this article, we will discuss about the key differences between scope and lifetime in C, including their definitions, applications, and how they relate to one another. By the end...

8 minutes read.