For loop is useful to execute block of statement until the specified condition evaluates to true.
For loop is used to iterate and execute the block of statement when we know the number of times the block of statement will execute.
Syntax
for(initialization;condition;iteration)
{
//statement of body to execute
}
In above syntax, we observed initialization, condition, iteration separated by semicolon (;).
In initialization, variable will be initialized with some value and it will execute only one time at the starting of for loop.
In condition part condition will be checked and it will return either true or false. If condition evaluates true then body of for loop will be executed otherwise not.
If condition evaluates to true, body of for loop will be executed. Then iteration part will be evaluated and it will increase or decrease the variable according to the requirement. And condition will be checked again.
using System;
namespace for_loop
{
class Program
{
static void Main(string[] args)
{
for (int i = 1; i <= 4; i++)
{
Console.WriteLine("i value: {0}", i);
}
Console.WriteLine("Press Enter Key to Exit..");
Console.ReadLine();
}
}
}
Output:-
i value: 1
i value: 2
i value: 3
i value: 4
Press Enter Key to Exit..
Flow chart of For Loop
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
For loop is useful to execute block of statement until the specified condition evaluates to true.
For loop is used to iterate and execute the block of statement when we know the number of times the block of statement will execute.
Syntax
In above syntax, we observed initialization, condition, iteration separated by semicolon (;).
In initialization, variable will be initialized with some value and it will execute only one time at the starting of for loop.
In condition part condition will be checked and it will return either true or false. If condition evaluates true then body of for loop will be executed otherwise not.
If condition evaluates to true, body of for loop will be executed. Then iteration part will be evaluated and it will increase or decrease the variable according to the requirement. And condition will be checked again.
If condition evaluates to false then for loop will stops its execution and control comes out of loop.
Output:-
Flow chart of For Loop