Difference between && and & | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Difference between && and &

Why do we have two AND operators: & and &&?

22nd Mar 2017, 8:12 PM
Thomas Zenglein
Thomas Zenglein - avatar
2 Answers
22nd Mar 2017, 8:13 PM
Shamima Yasmin
Shamima Yasmin - avatar
+ 1
additional to the above mentioned reference to bitwise operators, probably more often you need these functional differences: and = && is a logical operator, together with or = || aswell as ! = not. &&, ||, ! are three logical operators: for example: if (int a < int b && int b < int c){} // and condition, needs true true if (int a < int b || int b < int c){} // or condition, needs only 1 true if (! (age > 16)) /* not-condition turns condition around, if NOT age is greater than 16, meaning: if age is smaller than 16. The single & is used with pointers and is the adres-of-operator: int score = 5; cout << &score; //output for example: "0x29fee8" int var = 50; int *p; p = &var; cout << var; // outputs 50 (value of var, stored at the adres the pointer is pointing to)
22nd Mar 2017, 9:38 PM
Stefan