In c++ what use is there for && | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

In c++ what use is there for &&

?

14th Jul 2017, 4:16 PM
Binoy Babu
Binoy Babu - avatar
3 Answers
+ 5
It is the logical operator "and". So, if I were to say... int x = 5; int y = 9; if (x > 6 && y == 9) { cout << "That is true!" << endl; } else { cout << "This is the answer! I will be printed." << endl; } The else statement would be executed.
14th Jul 2017, 4:20 PM
Keto Z
Keto Z - avatar
+ 1
Also keep in mind, && will stop evaluating the conditions as soon as one of them is false. (Meanwhile, & will always evaluate both conditions.) Example/ int a = 4; if(++a < 5 && ++a == 6) a += 10; cout << a; Output: 5 (If I put & the output would be 6).
14th Jul 2017, 5:57 PM
Rrestoring faith
Rrestoring faith - avatar
0
Just complementing the other posts, it might also be used for rvalue references, necessary for move semantics. In short, it adds some efficiency to some situations by moving values insteading of copying.
14th Jul 2017, 6:20 PM
Denis Felipe
Denis Felipe - avatar