Loops in programming languages are utilized to execute one or a set of statements repeatedly based on the evaluation result of a condition. The condition must be evaluated to true to execute the statements inside the loop. In the following article, Mytour will provide detailed insights into loops in C#.
Loops in C#
1. Loops in C#.
2. Input-Controlled Loops in C#.
2.1. While Loop in C#.
2.2. For Loop in C#.
3. Output-Controlled Loops.
3.1. do-while Loop.
3.2. Infinite Loop.
3.3. Nested Loops in C#.
3.4. Continue Statement in C#.
1. Loops in C#
Loops are divided into 2 main types:
- Input-Controlled Loops: These are loops where the condition to be checked is at the beginning of the loop. While and For loops are condition-controlled loops.
- Exit-Controlled Loops: These are loops where the condition to be checked is placed at the end of the loop. The do-while loop is an exit-controlled loop.
2. Input-Controlled Loops in C#
2.1. While Loop in C#
The controlling condition is placed at the beginning of the loop, and all statements are executed until the boolean condition is satisfied. When the condition evaluates to false, the while loop ceases to be controlled.
Syntax of While Loop in C#:
while (boolean condition)
{
loop statements...
}
Illustrative Diagram of How While Loop Works in C#:
- Example of While Loop in C#:
// C# program demonstrating while loop
using System;
class whileLoopDemo
{
public static void Main()
{
int x = 1;
// Exit when x becomes greater than 4
while (x <=>
{
Console.WriteLine('Mytour');
// Increment the value of x for
// next iteration
x++;
}
}
}
The Returned Result Appears as Follows:
Mytour
Mytour
Mytour
Mytour
2.2. For Loop in C#
For Loop in C# functions similarly to While Loop, with syntax differences. Initializing loop variables, controlling conditions, and incrementing/decrementing loop variables are all done in a single line within the loop, making the loop structure shorter and easier to debug.
Structure:
for (initialize loop variable ; control condition ;
increment/decrement)
{
// executed statements
}
Below is a diagram illustrating how For Loop works in C#:
Within which:
- Initialization of Loop Variable: Expression / control variable is initialized here. This marks the start of the for loop. A declared variable can be used, or a variable can be declared within the local loop.
- Testing Condition: The condition tested to execute the loop statements. It is used to check the exit condition of the for loop, and must return either True or False. When the condition is False, the control exits the loop and terminates the for loop.
- Increment / Decrement: The loop variable is incremented / decremented as required, then control moves to testing condition again.
Note: The initialization part is evaluated only once when the for loop begins.
- Example:
// C# program illustrating for loop.
using System;
class forLoopDemo
{
public static void Main()
{
// for loop begins when x=1
// and runs till x <>
for (int x = 1; x <= 4;=''>
Console.WriteLine('Mytour');
}
}
The returned result appears as follows:
Mytour
Mytour
Mytour
Mytour
3. Output-Controlled Loops
Note: In output-controlled loops, the loop body will be evaluated at least once because the control condition is placed at the end of the loop body.
3.1. The do-while Loop
The do-while loop is similar to the while loop except that it checks the condition after executing the statements. This means that the loop body will be executed at least once because the control condition is evaluated after the statements are executed.
Syntax:
do
{
statement..
}while (condition);
Illustration of the do-while loop in C#:
- Example:
// C# program demonstrating do-while loop
using System;
class dowhileloopDemo
{
public static void Main()
{
int x = 21;
do
{
// This line will be printed even
// if the condition is false
Console.WriteLine('Mytour');
x++;
}
while (x <>
}
}
Expected output:
Mytour
3.2. Infinite Loop
Loops in which the control condition is never evaluated as False, and tend to execute statements indefinitely until an external intervention terminates the loop, are termed as Infinite Loops.
- Example of an Infinite Loop in C#:
// C# program illustrating an infinite loop
using System;
class infiniteLoop
{
public static void Main()
{
// The statement will be printed
// infinitely
for(;;)
Console.WriteLine('Mytour');
}
}
Expected output:
Mytour
Mytour
Mytour
Mytour
Mytour
.............
3.3. Nested Loops in C#
When loops are placed inside other loops, they are called nested loops.
- Example of nested loops in C#:
// C# program demonstrating nested loops
using System;
class nestedLoops
{
public static void Main()
{
// loop within loop printing Mytour
for(int i = 2; i < 3; i++)
for(int j = 1; j < i; j++)
Console.WriteLine('Mytour');}
}
Expected output:
Mytour
3.4. Continue Statement in C#
In C#, the Continue statement is used to skip the execution of certain parts of a loop under specific conditions and move the flow to the next iteration.
Diagram illustrating how the Continue statement works in C#:
- Example of the Continue statement in C#:
// C# program demonstrating the continue statement
using System;
class demoContinue
{
public static void Main()
{
// Mytour is printed only 2 times
// because of the continue statement
for(int i = 1; i < 3; i++)
{
if(i == 2)
continue;
Console.WriteLine('Mytour');
}
}
}
Expected output:
Mytour
This article above introduces you to loops in C#. Additionally, readers can explore other articles on Mytour to gain a deeper understanding of C# program structure and constants in C# as well.
