C# Loops

Here, you will learn how to execute a statement or code block multiple times using the Loops in C#. Loops are the instructions that repeat until a specified condition is reached. It’s used when we have to execute a block of code several times. Like other programming languages, C# also provides various control structures that allow for more complicated execution paths.

In simple terms, it enables us to run a particular sequence of statements repeatedly based on a certain condition.

Generally a looping process involves:

  • Control variable initialization
  • Condition Evaluation
  • Loop body execution
  • Control Variable Updation

There are mainly three types of 

Graphical representation: Loops in C#

 Loops in C#
Loops in C#
Loops in C# are divided into 2 categories given below:

1- Entry Controlled Loops: 

The loops in which the testing condition is given at the beginning of the loop body are known as Entry Controlled Loops in C#

For Example:

  • While Loop
  • For Loop
 Loops in C#

2- Exit Controlled Loops: 

The loops in which the testing condition is given at the end of the loop body are known as Exit Controlled Loops in C#.

For Example:

  • do-while Loop

while loopRepeats a statement or a group of statements while a given condition is true.
It tests the given condition before executing the loop body.
do-while loopSimilar to a while statement, except that it tests the given
condition at the end of the loop body.
for loopExecutes a sequence of statements multiple times and runs
the code that manages the loop variable.

Difference between entry controlled loop and exit controlled loop in Tabular form ?

Execution FlowA loop in which the given condition is checked first, before entering the loop bodyA loop in which the loop body is executed first and then after the given condition is checked
Condition EvaluationThe loop body would be executed, only if the given condition is trueThe loop body would be executed at least once, even if the given condition is evaluated as false
ExampleFor Loop and While Loop are examples of this type of loopDo While Loop is an example of exit controlled loop
UseIt is used when condition evaluation is mandatory before executing loop bodyIt is used when one requires to iterate through loop body at least once before condition evaluation

C# – Break Statement

The break statement in C# has following two usage −

  • When the break statement is encountered inside Loops in C#, the loop is immediately terminated and program control resumes at the next statement following the loop.
  • It can be used to terminate a case in the switch statement.

If you are using nested loops (i.e., one loop inside another loop), the break statement will stop the execution of the innermost loop and start executing the next line of code after the block.

C# – Continue Statement

In C#, the continue statement is similar to the break statement. Continue, on the other hand, instead of forcing termination, compels the next iteration of the Loops in C# to run, bypassing any code in between.

The conditional test and increment components of the for loop are executed when the continue statement is used. The continue statement causes the programme control to pass to the conditional tests in the while and do…while loops.