ページ 11

論理演算の条件式

Posted: 2020年9月13日(日) 15:48
by h1j1k1
gccコンパイラにおいて、論理演算のorで、一つでも真値が出た場合はそれ以降の条件式は評価されますか?

Re: 論理演算の条件式

Posted: 2020年9月13日(日) 22:05
by box
10個くらいのorでつながり、1億回くらいループするif文を書いて、
1)1個目で真
2)5個目くらいで真
3)最後で真
で、どれが速いかを比べてみては?

Re: 論理演算の条件式

Posted: 2020年9月14日(月) 09:55
by YuO
||の第1引数が真ならば、第2引数は評価されない、となっています。
ref) https://www.gnu.org/software/gnu-c-manu ... -Operators の第2文
The GNU C Reference Manual さんが書きました: The logical disjunction operator || tests if at least one of two expressions it true. If the first expression is true, then the second expression is not evaluated.
このため、
h1j1k1 さんが書きました:
3年前
gccコンパイラにおいて、論理演算のorで、一つでも真値が出た場合はそれ以降の条件式は評価されますか?
に関しては「評価されません」。
オフトピック
遅くともC89以降、この動作が標準に定められた動作になります。