简体   繁体   中英

evaluating else if after an if condition is true

Consider this snippet:

private void button1_Click(object sender, EventArgs e)
{
    if (textBox1.MaxLength > 0)
    {
        textBox2.Text = Convert.ToBoolean(Math.Sqrt(textBox1.MaxLength)).ToString();
    }
    else if (textBox1.MaxLength < 32768)
    {
        textBox2.Text = Convert.ToBoolean(Math.Sqrt(textBox1.MaxLength)).ToString();
    }                
}

Why is it not evaluating the second condition (the lesser than condition)? It's also true, isn't it?

If I have to make the second one to work in the same condition, what minor change must be made?

The second condition isn't being checked because an else statement will only be evaluated when the preceding conditions are evaluated to be false . There are several ways to fix this, some of which are shown in other answers. One such way is to use a single if statement, which combines both conditions:

if (textBox1.MaxLength > 0 && textBox1.MaxLength < 32768)
{
    textBox2.Text = Convert.ToBoolean(Math.Sqrt(textBox1.MaxLength)).ToString();
}

else if is only checked if the first condition isn't satisfied.

If you want both of them to be true, you should do

if(textBox1.MaxLength > 0 && textBox1.MaxLength < 32768)
{
   // do stuff here
}

You want the second if statement to run even if the first was true? Get rid of the else

因为你正在使用else而不是另一个if。

It will only reach the second part if the first one is false. If you want to check against both you should do this:

 if (textBox1.MaxLength > 0 && textBox1.MaxLength < 32768)

You can look at && like this:

if (A && B) {
    //do something
} 

is the same as

if (A) { 
    if (B) { 
        //do something 
    } 
}

I'm not quite sure if I understand your question correctly, but if you want both if-then statements to execute, you have to remove the "else". Otherwise it will only execute the firt if (if it's true).

You'll never reach the second if , because any number larger than 0 will satisfy the first if before the second, more restrictive condition. To put it in simpler terms, the first if will match all numbers n where n > 0 , whereas the second if matches numbers 32768 > n > 0 .

The reason your "else if" statement is not happening is because

if (textBox1.MaxLength > 0) 

is always true, since MaxLength obviously will always be greater then 0, unless it was changed to be 0.

I would like to add that

textBox1.MaxLength < 32768

Likely also will always be true.

Additionally

When this property is set to 0, the maximum length of the text that can be entered in the control is limited only by available memory.

Think of else if as actually being written this way:

private void button1_Click(object sender, EventArgs e)
{
    //textBox2.Text += Convert.ToString (textBox1.Text + "garment");
    if (textBox1.MaxLength > 0)
    {
        textBox2.Text = Convert.ToBoolean(Math.Sqrt(textBox1.MaxLength)).ToString();
    }
    else
    {
        if (textBox1.MaxLength < 32768)
        {
            textBox2.Text = Convert.ToBoolean(Math.Sqrt(textBox1.MaxLength)).ToString();
        }
    }
}

It's the same thing. Note that the second if sstatement is only executed when the first is false.

I would do this, if I would like to say "if MaxLength is between 0 and 32768":

if (0 < textBox1.MaxLength && textBox1.MaxLength < 32768)
    textBox2.Text = Convert.ToBoolean(Math.Sqrt(textBox1.MaxLength)).ToString();

Since you're doing the same in this interval, it is more appropriate to make both in the same condition statement.

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