逻辑与和逻辑或的优先级问题

sx666777888 2014-06-23 10:58:18
我在网上找到的说法几乎都是说与优先级高于或,我自己写了个测试代码
bool p1()
{
cout<<"p1"<<endl;
return false;
}
bool p2()
{
cout<<"p2"<<endl;
return true;
}
bool p3()
{
cout<<"p3"<<endl;
return true;
}

int main()
{

if(p1()||p2()&&p3());

return 0;
}
打印的的顺序是p1 p2 p3 ,也就是按顺序执行的啊,与并没有高于或,而且另外一个问题就是它们的结合性都是左到右,如果与的优先级高于或的话,那么在这个例子里与的先执行,将导致或的执行顺序是先右后左了,不符合理论啊??

希望大家指教,问题出在哪啊?网上的资料和我的代码测试和推论不符合。。。
...全文
1677 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
幻夢之葉 2014-06-24
  • 打赏
  • 举报
回复
你计算表达式前后顺序,跟结合顺序没什么关联吧?! 你这个结构只能说明该编译器 求 //输出的效果,相当于表达式的效果 f1() = true; f2() = true; f3() = true; 然后 true || (true && true) //完全没有违反结合性
lin5161678 2014-06-24
  • 打赏
  • 举报
回复
楼主错误的理解的优先级的意义了 优先级没有确定求值顺序的作用 所以 不管 && 优先级高 还是 || 优先级高 求值顺序在这里都是从左向右[&& || 是有限的几个确定求值顺序的运算符] 优先级真正作用仅用于确定运算符和哪些子表达式结合 而已
buyong 2014-06-24
  • 打赏
  • 举报
回复
分析汇编也能看出来,就是费劲:
0x40055b	push   rbp
0x40055c	mov    rbp,rsp
0x40055f	sub    rsp,0x10
0x400563	mov    eax,0x0
0x400568	call   0x40051c <p1>
0x40056d	test   eax,eax
0x40056f	jne    0x40058d <main+50>
0x400571	mov    eax,0x0
0x400576	call   0x400531 <p2>
0x40057b	test   eax,eax
0x40057d	je     0x400594 <main+57>
0x40057f	mov    eax,0x0
0x400584	call   0x400546 <p3>
0x400589	test   eax,eax
0x40058b	je     0x400594 <main+57>
0x40058d	mov    eax,0x1
0x400592	jmp    0x400599 <main+62>
0x400594	mov    eax,0x0
0x400599	mov    DWORD PTR [rbp-0x4],eax
0x40059c	mov    eax,0x0
0x4005a1	leave
0x4005a2	ret
sx666777888 2014-06-24
  • 打赏
  • 举报
回复
引用 5 楼 buyong 的回复:
分析汇编也能看出来,就是费劲:
0x40055b	push   rbp
0x40055c	mov    rbp,rsp
0x40055f	sub    rsp,0x10
0x400563	mov    eax,0x0
0x400568	call   0x40051c <p1>
0x40056d	test   eax,eax
0x40056f	jne    0x40058d <main+50>
0x400571	mov    eax,0x0
0x400576	call   0x400531 <p2>
0x40057b	test   eax,eax
0x40057d	je     0x400594 <main+57>
0x40057f	mov    eax,0x0
0x400584	call   0x400546 <p3>
0x400589	test   eax,eax
0x40058b	je     0x400594 <main+57>
0x40058d	mov    eax,0x1
0x400592	jmp    0x400599 <main+62>
0x400594	mov    eax,0x0
0x400599	mov    DWORD PTR [rbp-0x4],eax
0x40059c	mov    eax,0x0
0x4005a1	leave
0x4005a2	ret
汇编看不懂啊
sx666777888 2014-06-24
  • 打赏
  • 举报
回复
引用 6 楼 lin5161678 的回复:
楼主错误的理解的优先级的意义了 优先级没有确定求值顺序的作用 所以 不管 && 优先级高 还是 || 优先级高 求值顺序在这里都是从左向右[&& || 是有限的几个确定求值顺序的运算符] 优先级真正作用仅用于确定运算符和哪些子表达式结合 而已
觉得你的回答最精辟!!
赵4老师 2014-06-24
  • 打赏
  • 举报
回复
//C++ Operators
//  Operators specify an evaluation to be performed on one of the following:
//    One operand (unary operator)
//    Two operands (binary operator)
//    Three operands (ternary operator)
//  The C++ language includes all C operators and adds several new operators.
//  Table 1.1 lists the operators available in Microsoft C++.
//  Operators follow a strict precedence which defines the evaluation order of
//expressions containing these operators.  Operators associate with either the
//expression on their left or the expression on their right;    this is called
//“associativity.” Operators in the same group have equal precedence and are
//evaluated left to right in an expression unless explicitly forced by a pair of
//parentheses, ( ).
//  Table 1.1 shows the precedence and associativity of C++ operators
//  (from highest to lowest precedence).
//
//Table 1.1   C++ Operator Precedence and Associativity
// The highest precedence level is at the top of the table.
//+------------------+-----------------------------------------+---------------+
//| Operator         | Name or Meaning                         | Associativity |
//+------------------+-----------------------------------------+---------------+
//| ::               | Scope resolution                        | None          |
//| ::               | Global                                  | None          |
//| [ ]              | Array subscript                         | Left to right |
//| ( )              | Function call                           | Left to right |
//| ( )              | Conversion                              | None          |
//| .                | Member selection (object)               | Left to right |
//| ->               | Member selection (pointer)              | Left to right |
//| ++               | Postfix increment                       | None          |
//| --               | Postfix decrement                       | None          |
//| new              | Allocate object                         | None          |
//| delete           | Deallocate object                       | None          |
//| delete[ ]        | Deallocate object                       | None          |
//| ++               | Prefix increment                        | None          |
//| --               | Prefix decrement                        | None          |
//| *                | Dereference                             | None          |
//| &                | Address-of                              | None          |
//| +                | Unary plus                              | None          |
//| -                | Arithmetic negation (unary)             | None          |
//| !                | Logical NOT                             | None          |
//| ~                | Bitwise complement                      | None          |
//| sizeof           | Size of object                          | None          |
//| sizeof ( )       | Size of type                            | None          |
//| typeid( )        | type name                               | None          |
//| (type)           | Type cast (conversion)                  | Right to left |
//| const_cast       | Type cast (conversion)                  | None          |
//| dynamic_cast     | Type cast (conversion)                  | None          |
//| reinterpret_cast | Type cast (conversion)                  | None          |
//| static_cast      | Type cast (conversion)                  | None          |
//| .*               | Apply pointer to class member (objects) | Left to right |
//| ->*              | Dereference pointer to class member     | Left to right |
//| *                | Multiplication                          | Left to right |
//| /                | Division                                | Left to right |
//| %                | Remainder (modulus)                     | Left to right |
//| +                | Addition                                | Left to right |
//| -                | Subtraction                             | Left to right |
//| <<               | Left shift                              | Left to right |
//| >>               | Right shift                             | Left to right |
//| <                | Less than                               | Left to right |
//| >                | Greater than                            | Left to right |
//| <=               | Less than or equal to                   | Left to right |
//| >=               | Greater than or equal to                | Left to right |
//| ==               | Equality                                | Left to right |
//| !=               | Inequality                              | Left to right |
//| &                | Bitwise AND                             | Left to right |
//| ^                | Bitwise exclusive OR                    | Left to right |
//| |                | Bitwise OR                              | Left to right |
//| &&               | Logical AND                             | Left to right |
//| ||               | Logical OR                              | Left to right |
//| e1?e2:e3         | Conditional                             | Right to left |
//| =                | Assignment                              | Right to left |
//| *=               | Multiplication assignment               | Right to left |
//| /=               | Division assignment                     | Right to left |
//| %=               | Modulus assignment                      | Right to left |
//| +=               | Addition assignment                     | Right to left |
//| -=               | Subtraction assignment                  | Right to left |
//| <<=              | Left-shift assignment                   | Right to left |
//| >>=              | Right-shift assignment                  | Right to left |
//| &=               | Bitwise AND assignment                  | Right to left |
//| |=               | Bitwise inclusive OR assignment         | Right to left |
//| ^=               | Bitwise exclusive OR assignment         | Right to left |
//| ,                | Comma                                   | Left to right |
//+------------------+-----------------------------------------+---------------+
带头大哥_ 2014-06-23
  • 打赏
  • 举报
回复 2
其实你这个问题说复杂也复杂,讲不咋个情况,说简单也简单,关键看你理解思维 你看这个两个 (a||b)&&c和a||(b&&c) 你觉得这个2个效果一样不? 答案肯定是不!你能从中看出点什么不?a||b&&c这个出来的效果可以看做是这样的a||(b&&c),&&优先结合b和c,b和c两个发生作用,而没有去和a发生作用,a是和b&&c这个整体发生作用!而(a||b)&&c恰恰相反!但是计算顺序还是从左到友的!而不是从右到左,我也不清楚我到到底说清楚没有 多看几遍,你应该可以明白
sx666777888 2014-06-23
  • 打赏
  • 举报
回复
引用 2 楼 sx666777888 的回复:
[quote=引用 1 楼 taodm 的回复:] 楼主是糊涂在了优先级和执行顺序的关系上了。 你自己反复调整&&和||的位置,就可能发现点有意思的东西了。
我调换了&&和||顺序后,输出的是 p1,p3 看不懂了,求指教啊[/quote] 这个我知道了,
sx666777888 2014-06-23
  • 打赏
  • 举报
回复
引用 1 楼 taodm 的回复:
楼主是糊涂在了优先级和执行顺序的关系上了。 你自己反复调整&&和||的位置,就可能发现点有意思的东西了。
我调换了&&和||顺序后,输出的是 p1,p3 看不懂了,求指教啊
taodm 2014-06-23
  • 打赏
  • 举报
回复
楼主是糊涂在了优先级和执行顺序的关系上了。 你自己反复调整&&和||的位置,就可能发现点有意思的东西了。

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧