Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
These two simple code:

//This work
for (int x = 1; x < 10; x++)
{
MessageBox.Show(x.ToString());
}

//This does not work
for ( int x=9; x ==0; x--)
{
MessageBox.Show(x.ToString());
}

What I have tried:

I can not see the problem. I know it is a stupid question
Posted
Updated 18-May-21 9:27am

You're saying "while x == 0" (which it isn't). You meant "while x != 0".
 
Share this answer
 
Because a for loop only executes the loop body while the condition is true: since x starts at 9, the first time the loop condition is checked it fails because it isn't zero, so the loop terminates without the loop body ever being executed.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900