C# FOR BEGINNERS CRASH COURSE (2014)
Chapter 7 Loops in C#
Loop structures are used for executing one or more lines again and again. Various control structures are provided for complicated execution.
7.1 While loop
A while loop consists of a conditional expression that must be matched before the logical statements inside the while loop can be executed. If a statement matches the expression then it will run the code inside the loop and then return back to the conditional expression to re-check whether the statement still matches the expression. If it matches again then the loop will execute again otherwise it will skip the loop completely.
Let us visit the syntax of a while loop.
Syntax:
while (expression)
{
statements;
}
Flow Diagram
Example of while loop
Example 13: |
using System; namespace whileloop { class Program { static void Main(string[] args) { int var; var=50; while(var<150) { Console.WriteLine("Value of variable is:{0}",var); var=var+10; } } } } |
In the preceding code, the variable var is initialized with value 50. The while loop checks for the condition. If the condition is less than 150, the statements inside the loop are executed. If false, the control does not enter the while loop.
7.2 For loop
A for loop is used to execute a block of statements for a specific number of times.
Syntax:
for (initialization; condition; increment/decrement)
{
statement(s);
}
Where,
· initialization is used to declare and initialize the loop control variables. It is executed at the beginning of the loop
· condition checks for the expression. If the value is true, the body of the loop is executed. If false, the control jumps out from the loop
· increment/decrement is used for increasing or decreasing the variable value.
Flow Diagram
Example of for loop
Example 14: |
using System; namespace forloop { class Program { static void Main(string[] args) { int i; for(int i=1;i<=10;i++) { Console.WriteLine("Value of variable is:{0}",i); } } } } |
In the above code, value for variable i is declared. For loop contains assignment, condition, increment. If the condition is true, the value is incremented. The statement inside for loop is executed. Once the condition is false, the control moves out of the loop.
7.3 Do while loop
The do…while loop construct is similar to the while loop. Both the loops will continue until the condition is false. The difference is that the statements in a do…while loop are executed at least once, as the statements in the block are executed before the condition is checked.
Syntax:
do
{
Statements;
}while(expression);
Flow Diagram
Example of do while loop
Example 15: |
using System; namespace dowhile { class Program { static void Main(string[] args) { int x=1; do { Console.WriteLine("Value of x is:{0}",x); x=x+1; }while(i<10); Console.ReadLine(); } } } |
In the above code, the variable x is assigned with value 1. The do loop prints the statements inside the block. Once the condition in the while loop is false, the statements in the do while loop will not be executed.
7.4 Break statement
The break statement is used to exit from the loop. When this statement is encountered, the loop is terminated, the control resumes to the next statement following loop.
Syntax:
break;
Flow Diagram
Example of break statement
Example 16: |
using System; namespace break; { class Program { static void Main(string[] args) { int i=11; while(i<15) { Console.WriteLine("value of i:{0}",i); i++; if(i>13) { break; } } Console.ReadLine(); } } } |
In the code example, value of variable i is assigned to 11. The while loop checks for the condition of i. If the condition is true, the statements in the while loop are executed. The value for i is incremented.
If loop checks for value i. Once the value of i is greater than 13, the break statement is executed and the control is moves out of the loop.
7.5 Continue statement
The continue statement is similar to the break statement, but instead of breaking out of the loop the continue forces the next iteration of the loop to take place, skipping any code in between.
Syntax:
continue;
Flow Chart
Example of Continue statement
Example 17: |
using System; namespace continue { class Program { int i=45; do { if(i==50) { i=i+1; continue; } Console.WriteLine("Value of i is:{0}",i); i++; } while(i<60); Console.ReadLine(); } } |
So in the code example above we can see a combination of the do-while loop and continue in action. We can see thati is initially set as a 45 when it enters the statements inside the do. At this pointichecked to see if it matches 50, it doesn’t so the value ofiis outputted in a message andi is incremented by one.
This process happens untiliis 50, at which pointi will be incremented by one and hit the continue keyword which will skip the rest of the code in the do section and move straight to the while conditional expression.
Oncei reaches 61 the while conditional statement will not match and the execution will leave the loop.