05 Feb C# – Loops
C# loops execute a block of code and this code executes while the condition is true. Here are the types of loops in C#:
- while loop
- do…while loop
- for…loop
Let us learn about the loops one by one,
while loop
If the condition is true, a statement or a group of statements execute in the while loop. As long as the condition/ expression is true, the statement in the body executes. If the condition is false, the body of the loop will not execute.
Syntax
Here, the expression is a condition or a Boolean Expression,
1 2 3 4 5 |
while(expression) { // statements execute if the expression is true } |
Example
Let us see an example, wherein we learn about the usage of the while loop,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
using System; namespace Demo { class Studyopedia { static void Main(string[] args) { int i = 50; Console.WriteLine("While Loop in C#"); Console.WriteLine("Displaying Values!"); // while loop condition while( i < 55 ) { Console.WriteLine(i); i++; } } } } |
Output
1 2 3 4 5 6 7 8 9 |
While Loop in C# Displaying Values! 50 51 52 53 54 |
do…while loop
The do-while loop executes at least once, even if the condition/ expression is false, unlike the while loop. If the condition is true, a statement or a group of statements execute in do-while loop
If the condition is false, the body of the loop executes at least once.
Syntax
Here, expression is a boolean expression or condition,
1 2 3 4 5 |
do { // statement to execute } while(expression); |
Example
Let us see an example, wherein we learn about the usage of do…while loop,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
using System; namespace Demo { class Studyopedia { static void Main(string[] args) { int i = 50; Console.WriteLine("do-while Loop in C#"); Console.WriteLine("Displaying Values!"); // do-while loop condition do { Console.WriteLine(i); i++; }while( i < 55 ); } } } |
Output
1 2 3 4 5 6 7 8 9 |
do-while Loop in C# Displaying Values! 50 51 52 53 54 |
Difference between while and do-while loop
If the condition is false in a while loop, the body of the loop never executes.
If the condition is false in a do-while loop, the body of the loop executes at least once.
The conditional expression is at the top of the while loop.
The conditional expression is at the bottom of the do-while loop.
Now let us learn about for loop in Java.
for loop
The for loop is a loop that executes a specified number of times. Under this, initialize, test, and increment loop counter in a single line, unlike while and do-while loop.
Syntax
1 2 3 4 5 |
for(initialization; expression; inc/ dec) { // statements execute } |
Here,
- initialization: declare and initialize any loop control variable
- expression: a Boolean expression if true, the statement is executed
- inc/ dec: increment/ decrement loop
Example
Let us see an example, wherein we learn about the usage of for loop,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
using System; namespace Demo { class Studyopedia { static void Main(string[] args) { int i; Console.WriteLine("for Loop in C#"); Console.WriteLine("Displaying Values!"); // do-while loop condition for (i = 5; i <= 10; i = i + 1) { Console.WriteLine(i); } } } } |
Output
1 2 3 4 5 6 7 8 9 10 |
for Loop in C# Displaying Values! 5 6 7 8 9 10 |
No Comments