Med. A.
Med. A.

Reputation: 139

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";
    }

Upvotes: 1

Views: 1219

Answers (2)

Ravjit Singh
Ravjit Singh

Reputation: 838

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

Upvotes: 1

NubbyMcNuberson
NubbyMcNuberson

Reputation: 122

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";
}

Upvotes: 3

Related Questions