How many times the loop below will be executed What is the v
How many times the loop below will be executed? What is the value of counter after the loop?
int counter = 0;
while (counter<5)
{
++counter; }
MessageBox.Show(counter.ToString());
Solution
The loop will execute 5 times
Why?
when counter is 0 . it enters the loop and runs the code after counter incremented to 1.
counter is 1 . it enters the loop and runs the code after counter incremented to 2.
counter is 2 . it enters the loop and runs the code after counter incremented to 3.
counter is 3 . it enters the loop and runs the code after counter incremented to 4.
counter is 4 . it enters the loop and runs the code after counter incremented to 5
counter is 5 . it breaks the loop and exist the counter values is 5
