nested for

for loop in C programming language with example

for loop in C programming language with example

In this article, we will discuss for loop in C programming language with example

Looping structure is a control structure used in many programming languages for repetition tasks.

As we known, there is three looping structure in C, including for loop.

 

For loopfor loop in C programming language

While loopwhile loop in C Programming language

Do-while loopdo -while loop in C Programming language

For loop in C

for loop in C programming language with example

For loop in C Programming language performs the specified number of times.

For-Loop allows repetition of flow of control until the conditional statement becomes false.

Declaration

The syntax of a for loop in C Programming language

for(initialization; test_Expression; updating_statement){
//code here
}

Ex:

for(i=1; i<=10; i++){
System.out.print(i)
}

Explanation of for loop in C

Description of for loop in C language

 For loop always has three parts of control structure to perform the significant task

Initiation –

First, it always starts and executed from the variable initialization statement. The initialization statement is executed only once. Initialization is an entry point for the loop.

condition –

Next, the for loop moves to the conditional statement to test condition. The condition is evaluated and if it is true, the statements in body part are executed.  But, if the condition is false, the body of the loop does not execute.

updating statement

Finally, loop goes updating statement part and it allows you to update any loop control variables until the condition becomes false. This statement can be updated until a semicolon follows the condition.

 

For loop flow diagram

For loop flow diagram

How for loop works

for loop in C programming language is used to execute a block of statements repeatedly until a given condition returns false.

Initially, initialization statements are executed only once then text expression is evaluated by for loop.  If the test expression is true(Boolean expression makes only two output true or false) flow of control enters inside the body to execute the code. Continuously, test expression is evaluated and executed until the test expression is false.

This process repeats until the test expression is false.

When the test expression is false. for loop stops, the execution and flow of control immediately exit from the loop.

 

Various form of for loop in C language

form 1

int i;
for(i=0; i<=10; i++){
//statement(s);
}

form 2

int i=0;
for(;  i<=10; i++){
//statement(s);
}

 

form 3

int i;
for(i=0;  i<=10;){
//statement(s);
 i++
}

Example of for loop

Program 1

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int a;
    for(a=1; a<=10; a++){
    printf("Value is %d\n",a);
            }
            getch();
            return 0;
    }

 

When the above code compiled and executed, it produced the following result

value  is 1
value  is 2
value  is 3
value  is 4
value  is 5
value  is 6
value  is 7
value  is 8
value  is 9
value  is 10

 

Program 2

print sum number using for loop

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int num, count,sum=0;
    printf("Enter a positive integer\n");
    scanf("%d",&num); // input value for num
    for(count=1; count<=num; count++)
    {
        printf("count of numbers:%d\n",count);
    }
    for(count=1; count<=num; count++)
    {
        sum+=count;
    }
    printf("Sum of number:%d\n",sum);
    getch();
    return 0;
}

 

When the above code compiled and executed, it produced the following result

Enter a positive integer
10
count of number:1
count of number:2
count of number:3
count of number:4
count of number:5
count of number:6
count of number:7
count of number:8
count of number:9
count of number:10
sum of numbers:55

 

 

At the above program, the value entered by the user is stored in variable num as an int. Suppose, the value is 5

First for loop executes  5 times and displays stored value on the count.

Finally, second for loop calculates total value.

 

Suggested for you

For loop in Java

For loop in C++

For loop in Python

While loop in Java

While loop in C++

While loop in Python

While loop in C language

Do-while loop in Java

Do-while loop in C++

Do-while loop in C language

Nested for in C++ language

Nested for in Python language

Nested for in C language

Nested for in Java language

 

Nested for in Java programming language
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago

Java program to check odd or even using recursion

Java program to check odd or even using recursion In this tutorial, we discuss a…

2 months ago