0
Why put double & in c++
example &&
7 Respostas
+ 1
If a user enters say the number of apples available for sale, and it must be both positive AND is actually an even number, u can use && as a tool to make sure that these two conditions are fulfilled.
Look at the code below. If the user enters 1 or 3, the if statement will not run the print (cout ...) line, but if 2, 4, 10, etc, was entered, you will see "wow! 2 apples." Or "wow! 4 apples" etc.
This is bcos the number entered is both positive and even.
void main()
{
int num=0;
cout << "enter the number of apples avalable: "<< endl;
cin >> num;
//here we test if input is valid
If (num>0)&&(num%2==0)
cout <<"wow! " << num << "apples!"<< endl;
}
0
Double & or (&&) is c++ syntax for the logical operator "AND". Example:
If (1<2)&&(2<3)
Cout <<"yes";
This implies: If both sides of the operator && tests as true, then and only then will the output be "yes".
And In this case ofcourse the output is "yes"
0
Just so you know: You can't confuse & with && operator. Single & is used as bitwise operator, it works same as "AND" logic gate but only on integer values.
0
can you give more example i dont really understand
0
@ahmad
int a = 5;
int b = 3;
int c = a & b; // c equals 1
0
if(i>0&&i<5)
it means that "i" must be higher than 0 and lower than 5 at the same time. "if" will start only when these two conditions are true. (opposite of "||")
if(i>0||i<5)
in this case we are checking if i>0 or i<5, so if one of these conditions is true "if" will start. In this case "if" will be true for every i number.
------------------------------------------------------------------------------------------------------
int i=5;
if(i>0&&i<5) //It will not work becouse only one "i>0" condition is true. "i<5" is false
{
cout<<"You can't see me";
}
if(i>0&&i<6) //Now both conditions are true
{
cout<<"It works!";
}
0
& is used to receive the memory address of a variable (which would be called a pointer). An example:
int *hi = &52;
&& is used as the "and" operator. An example:
if ((5+5==0) && (2+2=4)) {
}
5+5 obviously doesn't equal 0. 2+2 does equal 4. Since it's using &&, it's false. If it was using || which is the OR operator, it'd be true.
So, essentially, all statements included with a && operator must be true, otherwise it'll return false for the entire statement.