简体   繁体   中英

Simple and fine for loop doesn't work

I feel stupid to ask this question but i got headache trying to find out why this simple for loop didn't work.

private void button2_Click(object sender, EventArgs e)
    {

       for (int i = 1; i>=5; i++)

            label2.Text = "aaaa";
    }

you're using a greater than sign, use the less than

private void button2_Click(object sender, EventArgs e)
{

   for (int i = 1; i<=5; i++)

        label2.Text = "aaaa";
}

first iteration: 1==5 which evaluates to false, so it will exit

You should read how the for loop works. Statement 1 is executed before the loop (the code block) starts. Statement 2 defines the condition for running the loop (the code block), if it is true Statement 3 is executed after the loop (the code block) has been executed. And this repeats until statement 2 becomes false

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM