The while loop in C# is a control looping statement that repeatedly executes a block of code as long as a specified condition is true. In this example, the program uses a while loop to print the first n natural numbers and calculate their sum.
12 Noon Class
10 AM Class
Control looping statements are used to execute a block of statements repeatedly based on a condition. The while loop is one of the looping statements available in C#.
A while loop repeatedly executes its body as long as the specified condition evaluates to true. The condition is checked before each iteration.
The loop variable should be initialized before the while loop. In this example, int i = 1 initializes the variable so that counting starts from the first natural number.
The condition i <= n checks whether the current value of i is less than or equal to the number entered by the user. The loop continues while this condition is true.
The variable sum is initialized to 0 and stores the running total of the natural numbers. During every iteration, the current value of i is added using sum += i.
The statement i++ increases the loop variable by 1 after each iteration. This eventually makes the condition false and prevents the loop from running indefinitely.
In this program, natural numbers are generated starting from 1 and continuing up to the value n entered by the user.
> "Enter any number :5"> "1 2 3 4 5"> "Sum of natural numbers up to 5 is: 15"
The program imports System and other namespaces before defining the application code.
Console.Write() asks the user to enter a number.
Console.ReadLine() reads the user input and Convert.ToInt32() converts it to an integer stored in n.
The variable i is initialized to 1 because the program starts with the first natural number.
The variable sum is initialized to 0 to store the total of all natural numbers.
The condition i <= n is checked before every iteration. The loop executes only while i is less than or equal to n.
Console.Write(i + "\t") displays the current value of i followed by a tab space.
The statement sum += i adds the current natural number to the running total.
The statement i++ increases i by 1 so the next natural number is processed.
After the condition becomes false, Console.WriteLine() displays the sum of natural numbers up to n.
A while loop can process sales records one by one and add each sales amount to a running total until all records have been processed. The same accumulation concept is used in this example with natural numbers.
A while loop repeatedly executes a block of code as long as its condition is true.
The condition is checked before each iteration of the loop.
The loop variable must have an initial value before it is used in the loop condition.
It initializes the variable that stores the running total of the natural numbers.
It adds the current value of i to the existing value of sum.
It increases the loop variable so the loop progresses toward its stopping condition.
The condition i <= n becomes false and the while loop stops.
An infinite loop is a loop that never ends because its stopping condition never becomes false.
1. Which statement is used to repeat a block while a condition is true?
2. When is the condition checked in a while loop?
3. What is the initial value of i in the example?
4. What is the purpose of sum += i?
5. Which statement increments the loop variable by 1?
6. For n = 5, what is the sum of natural numbers from 1 to 5?
7. When does the loop while (i <= n) stop?
8. What can cause an infinite while loop?
Write a C# program that accepts a number n from the user, uses a while loop to print the natural numbers from 1 to n, and calculates their sum.
