如果语句和&&或|| [英] If statements and && or ||

查看:54
本文介绍了如果语句和&&或||的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我是C#的初学者,在理解下面的if语句时遇到了麻烦.

OK so I'm a beginner with C# and I am having trouble understanding the if statement below.

在此示例中,INumber声明为8,dPrice为0,dTAX为10.

For this example INumber is declared as 8, dPrice is 0 and dTAX is 10.

        if (iNumber != 8 || iNumber != 9)
        {
            dPrice = dPrice + dTAX;
        }

有人可以解释为什么要输入该语句并将dTAX中的10加到dPrice吗?

Can somebody explain why it is entering the statement and adding the 10 from dTAX to dPrice?

我知道将其更改为&&可以,但是为什么呢?

I know changing it to && works, but why?

据我了解,如果iNumber不等于8或9,那么它应该仅输入If语句(此处是),因此不应输入.

As I understand it, it should only enter the If statement, if iNumber does not equal 8 or 9, which here it does, so it should not enter.

通过||运行后,这是我的输出if语句.

Here are my outputs after running it through the || if statement.

        Inumber is: 8

        dPrice was: 0
        dPrice is now: 10

        dTAX is: 10

有人可以向我解释一下吗?

Can somebody please explain this to me?

推荐答案

它正在输入语句,因为该语句在计算iNumber!= 9时变为true

It is entering the statement because the statement becomes true when it calculates iNumber != 9

一个||如果任何语句为true,则if中的(或运算符)将为true.

An || (Or Operator) in an if will be true if any statement is true.

这样想吧.

8 != 8 is False
8 != 9 is True

if ( False || True )
{
    //Do Stuff
}

这篇关于如果语句和&&或||的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆