Looping Statements (for, while, do-while) in c#
The for, while, and do-while looping statements in the C# programming language allow C# programmers to repeat or execute a particular block of program code multiple times based on a specific user-defined program condition expression. The most popular loops in the C# programming language are the for, while, and do-while loops.

So, let’s better understand the behavior of the for, while, and do-while loops in C# programming.
For Loop in C# Programming.
The for loop is used in C# programs when the C# programmer knows in advance the number of statements that will be repeated in the program, from the start to the end of the program iteration. The for loop is a simple, straight-forward loop, in which the assignment value at the start, the condition, the increment, and the decrement are manually defined in the specific condition for the loop expression.
Syntax of a For Loop in C#.
for (initialization; condition; increment/decrement)
{
// Create the for loop code you want to execute here
}
For Loop example in C#.
using System;
class ForLoop
{
static void Main()
{
for (int p = 0; p <= 9; p++)
{
Console.WriteLine($”Number of for loop series – {p}”);
}
}
}
}
While Loop in C# Programming.
A while loop is used in C# programs when the C# programmer does not know a specific number of value iterations in the current program. But the user wants to iterate or repeat a block of program source code as long as a user-defined program condition in the while loop is true.
Syntax of a While Loop in C#.
while (condition)
{
// Here you write the program source code to execute until a user-defined condition is true
}
Example of a while loop in C#.
using System;
class WhileLoop
{
static void Main()
{
int value = 1;
while (value <= 7)
{
Console.WriteLine($”the while loop value is – {value}”);
value++;
}
}
}
Do-While Loop in C# Programming.
The do-while loop is a program loop similar to the while loop, but unlike the while loop, it guarantees to execute a block of program source code at least once before checking a given program condition in the current program. This means that your do-while loop program code will run at least once.
Do-While Loop Syntax in C#.
do
{
// Write a do-while program source code here you want to execute
} while (condition);
Example of a Do-While Loop in C#.
using System;
class DoWhileLoop
{
static void Main()
{
int integer = 0;
do
{
Console.WriteLine($”the integer value is – {integer}”);
integer++;
} while (integer <= 8);
}
}
Common errors with for, while, and do-while loops in C# programs.
To avoid infinite loop conditions in C# programs, a user-defined program condition in the current program should automatically return false at the end. This will cause the program to end the loop iteration or repeat process and exit the loop program condition automatically.
C# Infinite Loop Condition Example.
using System;
class InfiniteWhileLoop
{
static void Main()
{
int p = 1;
while (true) // Here we define infinite loop behavior
{
if (p == 7)
break;
Console.WriteLine(“p = ” + p);
p++;
}
Console.WriteLine(“While Loop automatically terminates when p reaches 6.”);
}
}
Properly define the array data type boundary condition in an off-by-one error for loop in a C# program.
Off-by-one error for loop example in C#.
using System;
class OffByOneError
{
static void Main()
{
int[] integers = { 7, 1, 8, 3, 4, 5 };
// Here we define an off-by-one error example in a for loop
for (int p = 0; p <= integers.Length; p++)
{
Console.WriteLine(integers[p]);
}
}
}
Main Differences between for, while, and do-while in c#.
| Each Feature | For loop | While loop | do-while loop |
| Condition Check | It checks condition Before loop begin | It checks condition Before loop begin | It checks condition After loop |
| Execution Guarantee | For loop there is No Execution Guarantee | while loop there is No Execution Guarantee | do-while loop at least 1 time Execution Guarantee |
| Best Use Case | Used when you need Fixed iterations in series | Used when you don’t know about Unknown iterations series | Used when you want At least one time execution |
Use multiple nested loops in C# programs carefully to avoid deep program complexity.
Multiple nested loop example in C#.
using System;
class MultipleNestedLoops
{
static void Main()
{
Console.WriteLine(“\nLet’s see a Nested Loop example”);
// Here we define an outer loop
for (int p = 1; p <= 7; p++)
{
Console.WriteLine($”\t Element of – p = {p}”);
// here we define a middle loop
for (int q = 1; q <= 7; q++)
{
Console.WriteLine($”\t Element of – q = {q}”);
// here we define a Inner loop
for (int r = 1; r <= 7; r++)
{
Console.WriteLine($”\t\t\t Element of – r = {r}”);
}
}
Console.WriteLine();
}
Console.WriteLine(“Nested loop terminated”);
}
}
