We use a switch statement to check the value of the number variable.
We use a while loop to execute the block of code inside the loop as long as the condition (count < 5)
is true.
using System;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter a number between 1 and 5:");
int number = int.Parse(Console.ReadLine());
switch (number)
{
case 1:
Console.WriteLine("You entered
one.");
break;
case 2:
Console.WriteLine("You entered
two.");
break;
case 3:
Console.WriteLine("You entered
three.");
break;
case 4:
Console.WriteLine("You entered
four.");
break;
case 5:
Console.WriteLine("You entered
five.");
break;
default:
Console.WriteLine("Invalid number
entered.");
break;
}
}
}
using System;
class Program
{
static void Main(string[] args)
{
int count = 0; // Initialize a counter variable
Console.WriteLine("Counting from 1 to 5 using a while loop:");
// While loop to count from 1 to 5
while (count < 5)
{
count++; // Increment the counter
Console.WriteLine("Count: " + count); // Display the current count
}
Console.WriteLine("End of while loop.");
}
}
using System;
class Program
{
static void Main(string[] args)
{
int count = 0; // Initialize a counter
variable
Console.WriteLine("Counting from 1 to 5 using a do-while loop:");
// Do-while loop to count from 1 to 5
do
{
count++; // Increment the counter
Console.WriteLine("Count: " + count); // Display the current count
}
while (count < 5); // Check the condition after the first iteration
Console.WriteLine("End of do-while loop.");
}
}
using System;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Counting from 1 to 5 using a for loop:");
// For loop to count from 1 to 5
for (int count = 1; count <= 5;
count++)
{
Console.WriteLine("Count: " + count); // Display the current count
}
Console.WriteLine("End of for loop.");
}
}
using System;
class Program
{
static void Main(string[] args)
{
// Example using break statement
Console.WriteLine("Example using break statement:");
for (int i = 1; i <= 10; i++)
{
if (i == 6)
{
Console.WriteLine("Breaking the loop at
i = 6");
break; // Exit the loop
}
Console.WriteLine("Value of i: " + i);
}
// Example using continue statement
Console.WriteLine("\nExample using continue statement:");
for (int j = 1; j <= 5; j++)
{
if (j == 3)
{
Console.WriteLine("Skipping the
iteration at j = 3");
continue; // Skip this iteration and continue to the next one
}
Console.WriteLine("Value of j: " + j);
}
}
}