顯示廣告
隱藏 ✕
看板 rikaka
作者 rikaka (rikaka)
標題 C++ &&, ||
時間 2012年03月27日 Tue. PM 02:39:45


http://answers.yahoo.com/question/index?qid=20070927145356AAjwuaU

! means logical not.
&& means logical and.
1 && 1 得1
1 && 0 得0
0 && 1 得0
0 && 0 得0

|| means logical or.


if (statement1 && statement2)  // 如果statement1不成立, 就不會check statement2
                                                  // 如果statement1成立, 就會 check statement2
                                                 //  如果statement1成立->check statement2, 但是statement2不成立-->



The following code is correct.
It will evaluate x.
If you need further explanation of these, look at truth tables.
 If x equals 0, then it will evaluate y. If y also equals 0 then it will output blah blah, otherwise it will not.


If (x == 0 && y == 0)
{
cout << "blah blah";
}

If you were to replace the && with ||, then it would evaluate x. If x equals 0, then it will output blah blah. Then it will evaluate y. If y equals 0, then it will output blah blah, otherwise it will not.
If (x == 0 || y == 0)
{
cout << "blah blah";
}

To use the ! symbol, this will only output blah blah if the expression evaluates to false, that is both x and y equal 0.
If (!(x == 0 || y == 0))
{
cout << "blah blah";
}

--
※ 作者: rikaka 時間: 2012-03-27 14:39:45
※ 編輯: rikaka 時間: 2012-03-27 15:39:46
※ 看板: rikaka 文章推薦值: 0 目前人氣: 0 累積人氣: 172 
guest
x)推文 r)回覆 e)編輯 d)刪除 M)收藏 ^x)轉錄 同主題: =)首篇 [)上篇 ])下篇