Exercise
While + Counter
Objetive
Write a C# program to display the numbers 1 to 10 on the screen using "while".
Example Code
using System; // Importing the System namespace to use Console functionalities
class Program
{
// Main method where the program execution begins
static void Main()
{
int counter = 1; // Initializing the counter variable to 1
// Using a while loop to display numbers from 1 to 10
while (counter <= 10) // The loop will continue as long as counter is less than or equal to 10
{
// Displaying the current value of counter
Console.WriteLine(counter); // Printing the current number
counter++; // Incrementing the counter by 1 after each iteration
}
}
}