For loop c programming

In Programming languages, these looping statements are called iterative statements which are used to repeat the operations for a certain number of times. In C we have three iterative statements i.e for loop, while loop,do-while loop

Page Contents:

What is for loop C Programming?
Entry control loop in C
For loop flowchart
How For loop works?
For loop syntax in C
Simple for loop program in C with Algorithm
Break in for loop
Continue in for loop
Increment and decrement for in C
NESTED FOR LOOP
Nested for loop in C examples with output

What is for loop C Programming?

For is an iterative statement. It is used to repeat a set of statement numbers of times for loop is the most popular looping instruction. The for allows us to specify three things about a loop in a single line
a)Setting a loop counter to an initial value
b)Testing the loop counter to determine whether its value has reached the number of repetitions desired
c)Increasing the value of loop counter each time the program segment within the loop has been executed
d)For loop is also called as an entry controlled loop, as the entry inside the loop is possible only if the condition is true

Entry control loop in C

The entry controlled loop, as the entry inside the loop is possible only if the condition is true

For loop flowchart

for loop in C
for loop in C

How For loop works?

1 -Step

  • They are an iterative statement
  • Especially they used to repeat a set of statement number of times
  • The sequence is such that the initialization statements executed first
  • These initialization statements executed only once
  • The expression initial is evaluated and is usually an assignment statement that sets a variable to a particular value
  • The expression condition is evaluated, the condition is typically a relational expression
  • Also, they have used to initialize the values of the variables

2-Step

  • The second step for checking the condition specified
  • There can be only one condition If more than one condition is required they can combine using the logical AND, OR operator
  • Also, the condition improper the execution of the for loop further terminated So then the execution will go outside the braces if the condition is not true

3-Step

  • The third step is to execute all the statements inside the curly braces
  • These statements will be executed sequentially
  • The number of statements can be of any count there can be another control statement if required inside one control statement

4-Step

  • The fourth step is the increment/decrement or updating operations. These operations are not necessarily incremented or decremented operations, but mostly these are increment decrement and hence called so.
  • We can update the variables over here before starting the next iteration of the iterative statements.
  • And finally, the control goes back to the second step. As consider that the first step is executed only once, the steps that are repeated continuously are the second, third and fourth steps. After the fourth step, the condition is checked again. If the condition is true the execution continues, else the control goes outside the for loop

For loop syntax in C :

for(initializations;condition;increment/decrement/updating)
{
-
statements;
-
-
}

Simple for loop program in C with Algorithm

1. Print 1to10 Using For Loop In C

Algorithm
Step 1  :START
Step 2  :i=1
Step 3  :IF i>10 THEN GOTO step 7
Step 4  :PRINT i
Step 5  :i=i+1
Step 6  :GOTO step III
Step 7  :STOP
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
clrscr();
for(i=1;i<=10;i++)
{
printf("%d\n",i);
}
getch();
}

OUTPUT:

1
2
3
4
5
6
7
8
9
10

2. Write a program to find the factorial of a number

flow chart of factorial

Algorithm
Step 1    :START
Step 2    :PRINT "Enter a number"
Step 3    :INPUT n
Step 4    :i=1,fact=1
Step 5    :IF i>n THEN GOTO step 9
Step 6    :fact=fact*i
Step 7    :i=i+1
Step 8    :GOTO step 5
Step 9    :PRINT fact
Step 10   :STOP
#include<stdio.h>
#include<conio.h>
 void main()
 {
 int i,n,fact;
 clrscr();
 printf("Enter a number:");
 scanf("%d",&n);
 for(i=1;fact=1;i<=n;i++)
 {
 fact=fact*i;
 }
 printf("Factorial of this number is:%d\n",fact);
 getch();
 }

OUTPUT:

Enter a number:4
Factorial of this number is:24

3. Write a program to display first n odd numbers

Algorithm
Step 1    :START
Step 2    :PRINT "Enter a number"
Step 3    :INPUT n
Step 4    :i=1
Step 5    :IF i>n THEN GOTO step 9
Step 6    :PRINT i
Step 7    :i=i+2
Step 8    :GOTO step 5
Step 9    :STOP
#include<stdio.h>
#include<conio.h>
 void main()
 {
 int i,n;
 clrscr();
 printf("Enter a number");
 scanf("%d",&n);
 for(i=0;i<=n-1;i++)
 {
 printf("%d\n",2*i+1);
 }
 getch();
 }

OUTPUT:

Enter a number:6
1
3
5
7
9
11

4. print table of 2 using for loop in C

 Algorithm 
 Step 1    :START
 Step 2    :PRINT "Enter a number"
 Step 3    :INPUT n
 Step 4    :i=1
 Step 5    :IF i>10 THEN GOTO step 9
 Step 6    :PRINT n "X"i"="n*i
 Step 7    :i=i+1
 Step 8    :GOTO Step 5
 Step 9    :STOP
#include<stdio.h>
#include<conio.h>
 void main()
 {
 int i,n;
 clrscr();
 printf("Enter a number");
 scanf("%d",&n);
 for(i=1;i<=10;i++)
 {
 printf("%d X %d=%d\n",n,i,(n*i));
 }
 getch();
 }

OUTPUT:

Enter a number:2 
2X1=2 
2X2=4 
2X3=6 
2X4=8 
2X5=10 
2X6=12 
2X7=14 
2X8=16 
2X9=18 
2X10=20

5. Write a program to calculate and display the sum of first n natural number

Algorithm
 Step 1    :START
 Step 2    :PRINT "Enter a number"
 Step 3    :INPUT n
 Step 4    :i=1,sum=0
 Step 5    :IF i>n THEN GOTO step 9
 Step 6    :sum=sum+i
 Step 7    :i=i+1
 Step 8    :GOTO step 5
 Step 9    :PRINT sum
 Step 10   :STOP
#include<stdio.h>
#include<conio.h>
 void main()
 {
 int i,n,sum=0;
 clrscr();
 printf("Enter a number");
 scanf("%d",&n);
 for(i=1;i<=n;i++)
 {
 sum=sum+i;
 }
 printf("Sum =%d\n",sum);
 getch();
 }

OUTPUT:

Enter a number:10
Sum=55

break in for loop

break:- The break statement neglects the statements after it in the loop and transfers the control outside the loop.

for(initialization;condition;inc/dec)
 {
 -
 break;
 -
 } 

Write a program to demonstrate the use of break statement in for loop

#include<stdio.h>
#include<conio.h>
void main()
{
int n,total=0,i;
clrscr();
for(i=0;i<=10;i++) { printf("Enter a number:"); scanf("%d",&n); 
if(n>99)
break;
total=total+n;
}
printf("Total=%d",total);
getch();
}

OUTPUT:

Enter a number:45
Enter a number:54
Enter a number:22
Enter a number:121
Total=180

Continue in For loop

continue:-The continue statement also neglects the statements after it in the loop and transfers the control back to the starting of the loop for the next iteration.

for(initialization;condition;inc/dec)
{
-
continue;
-
}
#include<stdio.h>
#include<conio.h>
 void main()
 {
 int n,total=0,i;
 clrscr();
 for(i=1;i<=5;i++) { printf("Enter a number:"); scanf("%d",&n); if(n>99)
 {
 printf("Number is greater than 99\n");
 i--;
 continue;
 }
 total=total+n;
 }
 printf("Total=%d",total);
 getch();
 }

OUTPUT:

Enter a number:10
Enter a number:121
Number is greater than 99
Enter a number:22
Enter a number:11
Enter a number:88
Enter a number:99
Total=230

Increment and decrement for in C

Increment:

1.It returns the value of the variable added with one and stores the result in the variable itself
2.For example if x=5 then x++; will give you result x=6
3.This “x++;” (also called as post increment operator) can be written as “++x;” (also called as pre increment operator)
4.It can also be used to store the result in another variable.But in this case the post increment and pre increment statements will have different behavior as explained below with example In post increment case:if x=5, then y=x++; will make the value y=5 and x=6 In pre increment case: if x=5,then y=++x; will make the value y=6 and x=6

decrement:

1.It return the value of the variable subtracted with one and stores the result in the variable itself
2.For example if x=5 then x–; will make the value of x=4
3.This x– (also called as post decrement operator) can be written as –x (also called as pre decrement operator)
4.It can also be used to store the result in another variable.But in this case the post decrement and pre decrement statements will have different behaviour as explained below with examples
In post decrement case if x=5 then y=x–; will make the value of y=5 and x=4
In pre decrement case if x=5, then y=-x; will make the value of y=4 and x=4

NESTED FOR LOOP:

for(initializations;condition;increment/decrement/updating)
{
 for(initializations;condition;increment/decrement/updating)
{
-
statements;
-
-
} 
} 
 
  • In programming languages we can use for loop inside another for loop is called as nested for loop
  • When a particular operation has two references, we require nested for loop. For example, if we want to keep a reference of the row and column number, then we can use a nested for loop
  • Many programs based on nested for loop are given below. In this case, the variable i keeps a track of row number and j keeps a track of column number

Nested for loop flowchart

Nested for loop flowchart
Nested for loop flowchart

Nested for loop in C examples with output

1. Write a program to display “Hi” twice in a line and five such lines

#include<stdio.h>
#include<conio.h>
 void main()
 {
 int i,j;
 clrscr();
 for(i=1;i<=5;i++)
 {
 for(j=1;j<=2;j++)
 {
 printf("Hello world");
 }
 printf("\n");
 }
 getch();
 }

OUTPUT:

Hello World Hello World
Hello World Hello World
Hello World Hello World
Hello World Hello World
Hello World Hello World

2. Write a program to display the following

Hello Hello Hello
Hello Hello Hello

#include<stdio.h>
#include<conio.h>
 void main()
 {
 int i,j;
 clrscr();
 for(i=1;i<=2;i++)
 {
 for(j=1;j<=3;j++)
 {
 printf("Hello");
 }
 printf("\n");
 }
 getch();
 }

OUTPUT:

Hello Hello Hello
Hello Hello Hello

3.Display the following pattern

1
11
111
1111
11111
#include<stdio.h>
#include<conio.h>
 void main()
 {
 int i,j;
 clrscr();
 for(i=1;i<=5;i++)
 {
 for(j=1;j<=i;j++)
 {
 printf("1");
 }
 printf("\n");
 }
 getch();
 }

OUTPUT:

1
11
111
1111
11111

4. Write a program to display the following pattern

       *
      **
     ***
    ****
   *****
  ******
 *******
********
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n;
clrscr();
printf("Enter the number of * in the center line:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n-i;j++)
{
printf(" ");
}
for(j=1;j<=i;j++)
{
printf("*");
}
printf("\n");
}
getch();
}

OUTPUT:

Enter the number of lines:8
       *
      **
     ***
    ****
   *****
  ******
 *******
******** 

5. Write a program to display the following pattern

      *
     **
    ***
   ****
  *****
 ******
  *****
   ****
    ***
     **
      *
#include<stdio.h>
#include<conio.h>
void main()
{
 int i,j;
 clrscr();
 printf("Enter the number of * in the center line:");
 scanf("%d",&n);
 for(i=1;i<=n;i++)
{
 for(j=1;j<=n-i;j++)
{
 printf(" ");
}
 for(j=1;j<=i;j++)
{
 printf(""); 
} 
printf("\n"); 
} 
for(i=n-1;i>=1;i--) 
{ 
for(j=1;j<=n-i;j++) 
{ 
printf(" "); 
}
for(j=1;j<=i;j++) 
{ 
printf("");
}
printf("\n");
}
getch();
}

OUTPUT:

Enter the number of * in the center line:6
      *
     **
    ***
   ****
  *****
 ******
  *****
   ****
    ***
     **
    

Related article:

What is the use of for loop?
When for loop is terminated in c?
How does for loop work?
What is initialization of for loop?
What is logical expression in for loop ?
How many times the for loop will be executed ?
How do I initialize multiple variables in for loop in c?
What does “for(;;)” mean in programming?
When to use which loop?

Download PDF

for loop in C programming example pdf:

Download

Nested For Loop In C Programming Example With Explanation Pdf:

Download

Recommended Post:

Difference between While and do While loop in C with Example

What is the escape sequence in C and Examples of an escape sequence

What are data types in C? and Types of data types and example

What are two dimensional(2 D) arrays explain with example?

Translate »