Loops are structures that control repeated executions of a block of statements.
• C# provides a powerful control structure called a loop, which controls how many times an operation or a sequence of operation is performed in succession.
• C# provides three types of loop statements while loops, do-while loops, and for loops.
C# for loop examples
Example 1:
1 2 3 4 5 6 |
for (int i = 0; i < 10; i++) { Console.WriteLine("Value of i: {0}", i); } |
Example 2: letter increment in for loop
1 2 3 4 5 6 |
for (char i = 'A'; i < 'Z'; i++) { Console.WriteLine(i); } |
Example 3: infinite “for loop” example in C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
int i = 0; for(;;) { if (i < 10) { Console.WriteLine("Value of i: {0}", i); i++; } else break; } |
Example 4:
1 2 3 4 5 6 |
for(int i = 10; i> 0;i--) { Console.WriteLine("Value of i: {0}", i); } |
Example 5: Sum of all numbers divisible by 3 in a given range
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
static void Main(string[] args) { int sum = 0; Console.Write("Number 1 : "); int num1 = Convert.ToInt32(Console.ReadLine()); Console.Write("Number 2 : "); int num2 = Convert.ToInt32(Console.ReadLine()); for(int i=num1;i<=num2;i++) { if(i%3==0) { sum += i; } } Console.WriteLine("Sum of Numbers "+sum); Console.ReadKey(); } |
Example 6: Write a C# program to print numbers between 1 to 100 which are divisible by 3, 5
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
static void Main(string[] args) { Console.WriteLine("Divided by 3 & 5: "); for (int i=1;i<=100;i++) { if(i%3==0 && i%5==0) { Console.WriteLine(i); } } Console.ReadKey(); } |
Example 7: C# program reverses every word of a string and display the reversed string as an output.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
class Program { static string reversString(string str) { char[] letters = str.ToCharArray(); Array.Reverse(letters); return new string(letters); } static void Main(string[] args) { string str; Console.WriteLine(" \n \n *** www.csharp-console-examples.com *** \n\n"); Console.Write("Enter string :"); str = Console.ReadLine(); Console.WriteLine("***************************"); Console.Write(reversString(str)); Console.ReadLine(); } } |
Example 8: Reversing A String Using For Loop In C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
class Program { static void Main() { string name, Revname = ""; Console.Write("Entert Your Name : "); name = Console.ReadLine(); for (int i = name.Length - 1; i >= 0; i--) { Revname = Revname + name[i]; } Console.WriteLine("Reverse String Is {0}", Revname); Console.ReadLine(); } } |
Example 9: For-Loop Array In C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
class Program { static void Main(string[] args) { int[] array = { 100, 300, 500, 700, 900, 1000 }; // Use for loop. for (int i = 0; i < array.Length; i++) { Console.WriteLine(array[i]); } Console.ReadKey(); } } |
C# while loop examples
Example 1: while loop in C#
1 2 3 4 5 6 7 8 9 10 |
int i = 0; while (i < 10) { Console.WriteLine("Value of i: {0}", i); i++; } |
Example 2: using break in while loop in C#
1 2 3 4 5 6 7 8 9 10 11 12 13 |
int i = 0; while (true) { Console.WriteLine("Value of i: {0}", i); i++; if (i > 10) break; } |
Example 3: Example: Nested while loop in C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
int i = 0; while (i < 2) { Console.WriteLine("Value of i: {0}", i); int j = 1; i++; while (j < 2) { Console.WriteLine("Value of j: {0}", j); j++; } } |
Example 4:Write a program to find all Armstrong number in the entered range
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
static void Main(string[] args) { int num1,num2, n, sum, r; Console.Write("Enter positive number1 :"); num1 = Convert.ToInt32(Console.ReadLine()); Console.Write("Enter positive number2 :"); num2 = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Armstrong Number from {0} to {1}",num1,num2); for (int i = num1; i <= num2; i++) { sum = 0; n = i; while (n != 0) { r = n % 10; sum = sum + (r * r * r); n = n / 10; } if (i == sum) Console.WriteLine(i); } Console.ReadKey(); } |
Example 5: C# Program to Calculate the Power of a Number Without Using Math.Pow
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
class Program { static void Main(string[] args) { int baseNumber, expNumber, result = 1; Console.Write("Base Number : "); baseNumber = Convert.ToInt32(Console.ReadLine()); Console.Write("exponent Number : "); expNumber = Convert.ToInt32(Console.ReadLine()); while (expNumber != 0) { result *= baseNumber; --expNumber; } Console.WriteLine("Result = {0}", result); Console.ReadLine(); } } |
Example 6: C# program of Nested while Loop
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
class Program { static void Main(string[] args) { int i = 5; while (i >= 1) { int j = 5; while (j >= i) { Console.Write(j); j--; } i--; Console.WriteLine(); } Console.Read(); } } |
C# do while loop Examples
Example 1:
1 2 3 4 5 6 7 8 9 10 11 |
int i = 0; do { Console.WriteLine("Value of i: {0}", i); i++; } while (i < 10); |
Example 2: using break keyword
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
int i = 0; do { Console.WriteLine("Value of i: {0}", i); i++; if (i > 5) break; } while (true); |
Example 3: nested loop example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
int i = 0; do { Console.WriteLine("Value of i: {0}", i); int j = i; i++; do { Console.WriteLine("Value of j: {0}", j); j++; } while (j < 2); } while (i < 2); |
Example 4: Enter Only Numbers in C# Console Application
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
static void Main(string[] args) { double val = 0; string num = ""; Console.Write("Enter Number: "); ConsoleKeyInfo chr; do { chr = Console.ReadKey(true); if (chr.Key != ConsoleKey.Backspace) { bool control = double.TryParse(chr.KeyChar.ToString(), out val); if (control) { num += chr.KeyChar; Console.Write(chr.KeyChar); } } else { if (chr.Key == ConsoleKey.Backspace && num.Length > 0) { num = num.Substring(0, (num.Length - 1)); Console.Write("\b \b"); } } } while (chr.Key != ConsoleKey.Enter); Console.ReadKey(); } |
Example 5: C# program for Nested 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 24 25 26 27 |
class Program { static void Main(string[] args) { int i = 5; do { int space = i; do { Console.Write(' '); space--; }while (space >= 1); int j = 5; do { Console.Write("* "); j--; }while(j >= i); Console.WriteLine(); i--; }while(i >= 1); Console.Read(); } } |