开发者

if statement, boolean value

This method is taken from Murach's C# 2010 book and is given as an example of an method that checks if a string contains a decimal value:

        // the new IsDecimal method
    public bool IsDecimal(TextBox textBox, string name)
    {
        //make sure the string only contains numbers and numeric formatting
        string s = textBox.Text;
        int decimalCount = 0;
        bool validDecimal = true;
        foreach (char c in s)
        {
            if (!(
                c == '0' || c == '1' || c == '2' ||   // numeric chars
                c == '3' || c == '4' || c == '5' ||
                c == '6' || c == '7' || c == '8' ||
          开发者_如何学Python      c == '9' || c == '.' ||
                c == '$' || c == '%' || c == ',' ||  // formatting chars
                c == ' '
                ))
            {
                validDecimal = false;
                break;
            }
            if (c == '.')
            {
                decimalCount++;
            }
        } // end loop

        if (validDecimal && decimalCount <= 1)
        {
            return true;
        }
        else
        {
            MessageBox.Show(name + " must be a decimal number.",
                "Entry Error");
            textBox.Focus();
            return false;
        }
    }

My question is about this if statement and boolean value:

            if (validDecimal && decimalCount <= 1)
        {
            return true;
        }

I understand that it should be checking if both validDecimal returns true from above loop and that there is only one decimal point present. I'm quite confused by this, my understanding of bool is that it can hold two values: 1 = true and 0 = false; in both of those cases that if statement would be satisfied (0 is smaller than 1 and 1 is equal). I'm thinking that correct if statement would look something like that: if (validDecimal == true && decimalCount <= 1) but since I'm beginner I'm not sure and this isn't in the errata for this book.


A bool value on its own is the same as value == true

If you have

bool trueThing = true;

then the following are all equivalent (and true)

trueThing;
trueThing == true;
(trueThing == true) == true;
((trueThing == true) == true) == true;

and so on. The simplest, least verbose form is the first: trueThing. It's not a 0 or 1, it's a true or false.


Operator Precedence. This:

if (validDecimal && decimalCount <= 1)

is interpreted as:

if (validDecimal &&  (decimalCount <= 1))

not

if ((validDecimal && decimalCount) <= 1)

In other words, the <= happens before the &&.


I think you're reading the statement wrong, if you placed parentheses, so that it's obvious what's going on, it would look like

(validDecimal) && (decimalCount <= 1)

not

(validDecimal && decimalCount) <= 1

In English: check that validDecimal is true and decimalCount is at most 1. The comparison against true is implicit, because it's not necessary.

In C#, variable of type bool can have one of two values, true or false, but they don't act as numbers, so you can't say they are 1 and 0 (although they are usually implemented that way).

Also, in most languages, if you mean “a and b are both at most x”, you can't write it as a && b <= x. That's just not how (most) computer languages work.


You can write

if (validDecimal == true && decimalCount <= 1)

but it is equivalent to the shorthand way

if (validDecimal && decimalCount <= 1)

When the type is a bool no need to again use

== true

as it is assumed


I don't really know in c#, but in Java, you can even do this:

boolean b = 1<2;

1<2 is true (remember, it's a boolean equation), so the value assigned to b is true. Everything you put in the if, is interpreted as

if(whateverLogicEquationHereIsTrue){do this}
else{means that the boolean input to the if, has come to be false}

now, what is the output of this?

if(!(1>2)){print "a"}
else{print "b"}

hint: ! is the logical operator NOT

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜