Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c

C language for statement with example

Posted on August 21, 2016January 29, 2020

Table of Contents

  • C language for statement with example
    • How for loops works
      • Nested for loop

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

 

C language for statement with example
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
C language for statement with 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;

}

 

 

 

 
While Loop in Java      while Loop in C++     While Loop In C

Do-while Loop in Java    Do-while Loop C++      Do-while loop in C 

For Loop in C++       For Loop in C         For loop in java

If condition C++       If condition in Java   If condition in C

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes