Loops in C# Assignment Help
LOOPS
It is used to execute particular part of code repeatedly. The loops supported in c# are
- while loop
- for loop
- foreach loop
- do … while loop
- Nested loops
Loops in C# Assignment Help By Online Tutoring and Guided Sessions from assignmenthippo.com
While loop:
- It repeats the statement until the conditions get false. Its an entry controlled loop
Syntax:
while (condition)
{
// code block to be executed
}
Example:
![loops image 1 loops image 1](https://www.assignmenthelp.net/webimg/ahp/c-sharp-assignment-help/loops-image-1.jpg)
for loop:
- It executes the set of statement multiple times until condition get false.
Syntax:
for ( initialization; condition; increment/decrement)
{Body of statement}
Example:
![loops image 2 loops image 2](https://www.assignmenthelp.net/webimg/ahp/c-sharp-assignment-help/loops-image-2.jpg)
foreach loop
- foreach loop is used to iterate through an array variable.
Syntax
foreach (datatype variable_name in array_name ) {}
Example
foreach(int i in n){}
do ..while loop:
- Its an exit controlled loop.
- Here, the statement executes at least once and then the condition is checked for true or false value.
Syntax:
do
{
Statement
}
while(condition);
Example:
![loops image 3 loops image 3](https://www.assignmenthelp.net/webimg/ahp/c-sharp-assignment-help/loops-image-3.jpg)
Nested Loop
- One loop inside another loop is called nested loop.
Syntax
for ( initialization; condition; increment/decrement)
{
for ( initialization; condition; increment/decrement)
{ Body of statement }
Body of statement
}
Note: Similarly while and do---while loop can be nested
Example:
![loops image 4 loops image 4](https://www.assignmenthelp.net/webimg/ahp/c-sharp-assignment-help/loops-image-4.jpg)
Loop control statements:
It is used to jump or control the execution from normal process. Two control statements are supported in c#.
- break – when it is found the loop immediately terminate. It is used by default in switch case statements.
Syntax:
break;
Example:
![loops image 5 loops image 5](https://www.assignmenthelp.net/webimg/ahp/c-sharp-assignment-help/loops-image-5.jpg)
- continue – it is used to skip a few statements when it reaches the particular condition.
Syntax:
continue:
Example:
![loops image 6 loops image 6](https://www.assignmenthelp.net/webimg/ahp/c-sharp-assignment-help/loops-image-6.jpg)