For Loop is a loop in programming languages like C# that repeats a block of statements until a condition provided is satisfied. Unlike other less structured loops like while and do..while, this loop contains initialization, condition and increment/decrement before the loop statement.
Syntax for For Loop:
for (initialization; condition; increment/decrement) { statements; }
Here, initialization, condition and increment/decrement are in same line which allows us to do three task in same line decreasing the length of code. These blocks in for are separated with a semicolon.
Initialization block is used to declare a variable for the loop. It is executed only once while entering the loop. Variable declared in this loop is only usable in the loop and it is possible to declare multiple variables in initialization block.
Condition block contains condition for the loop. The condition must be an expression that returns value in boolean result. This blocks evaluates the condition and runs the loop until the condition is satisfied and exits the loop otherwise.
Increment/decrement block is used to change the value of the variable. This block is accessed after loop body is executed.
Flowchart for For Loop:
Example 1: C# for Loop Example
C# Program to print first n natural numbers
using System; namespace loop { class Program { static void Main(string[] args) { Console.Write("Enter a number: "); int n = (int)Convert.ToInt32(Console.ReadLine()); for (int i = 1; i <= n; i++) { Console.WriteLine(i); } Console.ReadLine(); } } }
Output:
Enter a number: 9 1 2 3 4 5 6 7 8 9 10
Additional Information about for Loop:
-
Initialization block can be left empty but the semicolon before condition block is compulsory
for (int i = 1; condition; increment/decrement) //valid for ( ; condition; increment/decrement) //valid
- Multiple variables can be used in initialization block as well. They must be separated with a comma.
for (int i = 1, j = 5; condition; increment/decrement) //valid
-
Condition block can also be left empty but the semicolon should be present after initialization block and before increment/decrement block but without a condition, infinite loop is created. But, condition can be set in the loop body as well using statements like if.
for (int i = 1; i <= 5; increment/decrement) //valid for (int i = 1; ; increment/decrement) //valid
- We cannot use multiple conditions separately but we can if we combine them using logical keywords.
for (int i = 1; i < 5; i == 5; increment/decrement) //invalid for (int i = 1; i < 5, i == 5; increment/decrement) //invalid for (int i = 1; i < 5 || i == 5; increment/decrement) //valid
-
Increment/decrement block can also be left empty but the semicolon should be present after condition block but without a changing value of variable, infinite loop is created. But increment or decrement can be set in the loop body as well.
for (int i = 1; i <= 5; i++) //valid for (int i = 1; i <= 5; ) //valid
- Multiple expressions can be used in increment/decrement block as well. They must be separated with a comma.
for (int i = 1, j = 5; i <= 5; i++, j--) //valid