C language for statement with example

C language for statement with example

In this tutorial, we will discuss the C language for statement with example

generally in programming languages, looping statements used to perform of a condition of the block of code.

C program has 3 looping statement

  • for loop
  • while loop
  • do-while loop

here we can clearly understand for loop in C language

Syntax

for ( initializationstatement; condition; increment/dicrement ) {
statement(s);
}

 


Flow diagram for loop in C

 

Flow diagram

How for loops works

first the initialization statements are executed only once

Then the test expression is evaluated when the test expression becomes false. The control comes out from the loop and for loop is terminated.
However, when the test expression evaluated to true, statements inside the body of for loop are executed.
finally, the updating statement(increment/decrement) is updated

then again the test expression is evaluated
This process happening on until the test expression becomes false.

Example
Syntax

For loop starts from initialization statements.

The test expression is evaluated. if the test expression is false (boolean check), the flow of control skips from the loop. for loop is terminated, but if the test expression is true codes inside in the body of for loop is executed and then update statement is updated.

this process repeats until the test expression is false. when test expression false, The for loop is terminated



program 1

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

int main()
{
int a;

/* for loop execution*/
for(a=10; a<20; a=a+1){

printf(“value of a is %dn”,a);
}

return 0;
}

program 2
//program to calculate the sum of first n natural numbers
//Enter a positive integer and calculate sum
#include <stdio.h>
#include <stdlib.h>
int main()
{
  int n,count,sum=0;
  printf(“Enter a positive integer:”);
  scanf(“%d”, &n);
  //for loop terminates when n is less then count
  for(count=1; count<=n; ++count)
  {
      sum+=count;
  }
  printf(“sum = %d”, sum);
  return 0;
}

Find factorial of the number

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

int main()
{
    int n,i;
long factorial=1;

printf(“Enter an integer number..”);
scanf(“%d”,&n);

// show error if user enter a negetive integer
if(n<=0){
    printf(“Error you must enter positive numbern”);
}
else{
     for(i=1; i<=n; ++i)   // single  for loop
{
    factorial*=i;
}
printf(“factorial of %d=%11u”,n,factorial);
    }
    return 0;

}

 

Nested for loop

 

for (initialization; condition; increment/decrement)
{
  statement(s);
  for (initialization; condition; increment/decrement)
  {
      statement(s);
      … … …
  }
  … … …
}

program 1


#include <stdio.h>

#include <stdlib.h>

int main()

{
  int row,coloum,n;
  printf(“Enter the rectabgle sizen”);
  scanf(“%d”,&n);

  for(row=1; row<=n; row++){

      for(coloum=1; coloum<=n; coloum++){
       printf(“*”);
      }
      printf(“n”);
  }

  return 0;

}

 

 

 

Karmehavannan

Recent Posts

Subtract two numbers using method overriding

Subtract two numbers using method overriding   Program 1

3 months ago

PHP Star triangle Pattern program

PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…

3 months ago

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

1 year ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

1 year ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

1 year ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

1 year ago

This website uses cookies.