What is for loop in C programming language ?


for loop in c

In this article we will talk about for loop which is also know as for statement in C language this article included following topics as,

  • For loop in C
  • Syntax of for loop
  • Example of for loop

For loop in C

A for loop is also know as for statement in C language, the 'for' statement could be very beneficial while programming in C. It has three expressions and semicolons are used for separating these expressions.

Syntax of for loop


For(initialization ; condition ; increment/decrement)
{
    Statement 1;
    Statement 2;
}
  
Or

For(expression1 ; expression2 ; expression3)
{
    Statement 1;
    Statement 2;
}

The loop frame(body) can be a single or block of statements.Expression1 is an initialization expression, expression2 is a check expression or situation(conditional expression) and expression3 is an update expression. Expression2 is a condition
and is examined before every iteration of the loop. This condition commonly makes use of relational and logical operators. Expression3 is an replace expression and is executed whenever after the frame(body) of the loop is executed.

Now allow us to see how this loop works. Firstly the initialization expression is executed and the loop variables are initialized, and then the situation is checked, if the situation is real then the body of loop is executed.After, executing the loop body, manage transfers to expression3 (update expression) and it modifies the loop variables and however the situation is checked, and if it's miles true, the frame(body) of loop is completed. This method maintains until the condition is authentic and when the condition becomes false the loop is terminated and control is transferred to the statement following the loop.

Example of for loop


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

example of for loop

___________________________________________
//Multiply two positive numbers without using (*) operator
#include<stdio.h>
int main( )
{
int a,b,i;
int result=O;
printf("Enter two numbers to be multiplied ");
scanf("%d%d",&a,&b) ;
for(i=l;i<=b;i++)
result=result+a;
printf("%d * %d %d\n",a,b,result);
return 0;
}

example of for loop
___________________________________________

Post a Comment (0)
Previous Post Next Post