且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

什么时候应该在if语句的条件中使用括号?

更新时间:2023-02-16 23:06:55

如果需要检查多个条件,可以使用括号。

Brackets can be used, if multiple conditions needs to be checked.

例如:如果全名为***,则用户也是堆栈器,然后使用或运算符添加另一个条件。

For ex: User is also a Stacker if full name is '***' then add another condition with 'Or' operator.

if(((name == 'Stack') && (lastname == 'Overflow')) || FullName =='***')

如您所见,名称和姓氏放在一个括号内意味着它给出一个结果为true或false,然后它使用FullName条件进行OR运算。

As you can see, name and last name are placed inside one bracket meaning that it gives a result either true or false and then it does OR operation with FullName condition.

并且在名称,姓氏和全名字段周围括号是可选的,因为它不是对条件没有任何影响。但是,如果您使用FullName检查其他条件,则将它们分组到括号中。

And having brackets around name, lastname and fullname fields are optional since it doesn't make any difference to the condition. But if you are checking other condition with FullName then group them into bracket.