Skip to content
The Programming School

The Programming School

  • Home
  • About
  • Contact Us
  • Toggle search form

Loops examples in C# Language

Here are some Loops examples in C# Language

Working with For Loops

Counting Up:

using System;

class Program
{

static void Main()
{

for (int i = 1; i<=5; i++)
{

Console.WriteLine(i);

}
}
}

• The ' for ' loop initializes a loop variable (' i ') to 1 .

• The loop continues as long as the condition (' i <= 5 ') is true .

• After each iteration , the loop increments ' i ' by 1 (' i++ ') .

• Inside the loop , it prints the value of ' i ' .

Counting Down:

using System;

class Program
{

static void Main()
{

for(int i = 5; i >= 1; i--)
{

Console.WriteLine(i);

}
}
}

• Similar to the first example , this ' for ' loop initializes ' i ' to 5 .

• The loop continues as long as the condition (' i >= 1 ') is true .

• After each iteration , the loop decrements ' i ' by 1 (' i-- ') .

• Inside the loop , it prints the value of ' i ' .




Custom Iterations:

using System;

class Program
{

static void Main()
{

for (int i = 3; i <= 15; i += 3)
{

Console.WriteLine(i);

}
}
}

• This ' for ' loop initializes ' i ' to 3 .

• It continues as long as the condition (' i <= 15 ') is true .

• After each iteration , it increments ' i ' by 3 (' i += 3 ') .

• Inside the loop , it prints the value of ' i ' , which results in multiples of 3 frmo 3 to 15 .

Working with Do While Loops

Sum of Squares:

using System;

class Program
{

static void Main()
{

int n = 4;
int sumOfSquares = 0;
int i = 1;

while (i <= n)
{

sumOfSquares += i * i;
i++;

}

Console.WriteLine($" Sum of squares from 1 to {n} : {sumOfSquares} ");

}
}

• This program uses a ' while ' loop to calculates the sum of squares from 1 to 4 .

• It initializes variables ' n ' , ' sumOfSquares ' , and ' i ' .

• The loop continues as long as the condition (' i <= n ') is true .

• Inside the loop , it adds the square of ' i ' to ' sumOfSquares ' and increments ' i ' .

• After the loop , it prints the sum of squares .

Factorial Calculation:

using System;

class Program
{

static void Main()
{

int n = 5;
int factorial = 1;
int i = 1;

while (i <= n)
{

factorial *= i;
i++;

}

Console.WriteLine($" Factorial of {n} : {factorial} ");

}
}

• This program uses a ' while ' loop to calculate the factorial of 5 .

• It initializes variables ' n ' , ' factorial ' , and ' i ' .

• The loop continues as long as the condition (' i <= n ') is true .

• Inside the loop , it multiplies ' factorial ' by ' i ' and increments ' i ' .

• After the loop , it prints the factorial .

Printing a Triangle:

using System;

class Program
{

static void Main()
{

int rows = 4;
int i = 1;

while (i <= rows)
{

int j = 1;

while (j <= i)
{

Console.Write("* ");

j++;

}

Console.WriteLine();

i++;

}
}
}

• This Program uses a nested ' while ' loop to print a triangle pattern .

• It initializes variables ' rows ' and ' i ' .

• The outer loop continues as long as the condition (' i <= rows ') is true .

• The inner loop prints asterisks based on the current value of ' i ' .

• After the inner loop , a newline is added to create a triangle pattern .

Working with Do While Loops

Sum of Positive Numbers:

using System;

class Program
{

static void Main()
{

int sum = 0;
int userInput;

do
{

Console.Write("Enter a positive number (enter 0 to finish) : ");

userInput = Convert.ToInt32(Console.ReadLine());

if (userInput > 0)
{

sum += userInput;

}
} while (userInput != 0);

Console.WriteLine($" Sum of positive numbers : {sum} ");

}
}

• This program uses a ' do-while ' loop to calculate the sum of positive numbers entered by the user .

• Is continues as long as the condition (' userInput != 0 ') is true .

• Inside the loop , it prompts the user to enter a positive number and adds it to the ' sum ' if it's positive .

Checking Password:

using System;

class Program
{

static void Main()
{

string password;

do
{

Console.Write("Enter the password: ");

password = Console.ReadLine();

} while (password != "secret");

Console.WriteLine("Access granted!");

}
}

• This program uses a ' do-while ' loop to prompts the user for a password until the correct one ("secret") is provided .

• It continues as long as the condition (' password != "secret" ') is true .

• Inside the loop , it prompts the user to enter the password using ' Console.ReadLie ' .

Fibonacci Series:

using System;

class Program
{

static void Main()
{

Console.Write("Enter the limit for Fibonacci series: ");

int limit = Convert.ToInt32(Console.ReadLine());

int first = 0;

int second = 1;

do
{

Console.Write($" {first} ");

int temp = first + second;

first = second;

second = temp;

} while (first <= limit);

}
}

• This program uses a ' do-while ' loop to generate the Fibonacci series up to a specified limit .

• It prompts the user to enter the limit using ' Console.ReadLine ' and ' Convert.ToInt32 ' .

• The loop continues as long as the condition (' first <= limit ') is true .

• Inside the loop , it prints the current value of ' first ' and updates the Fibonacci sequence .

  • Home
  • About
  • Contact Us

Copyright © 2025 The Programming School.

Powered by PressBook Masonry Dark