In: Computer Science
Suppose that the double variables a, b and c contain the values -10, 0 1 and2 1 respectively, and the boolean variables b1, b2 and b3 contain the values true, false and false respectively Is each of the following expressions legal or illegal and what will the result be?
a) a> b || b>c
b) b1 | | b2 && b3
The expressions are legal and the result or output will be:
For a) a>b||b>c
In the first expression there are relational operators which have a higher precedence than( || operator know as Logical OR Operator ) so first the logical operators evaluate
a>b is false or we can say it as 0 (default numeric value of false is 0)
b>c is false or we can say it is 0( default numeric value of false is 0)
After that false || false is false or we can say it is 0(default numeric value of false is 0)
So the result for expression a) will be 0(i.e false)
Now
b) b1||b2&&b3
Here in this expression there are two operators Logical OR(||) and Logical AND(&&)
Logical AND has a higher precedence than Logical OR
So b2 && b3 will calculate first
b2= false
b3= false
So false && false is false
Now the Logical OR operation will be done
Previous result false(b2&& b3)
b1= true
So true || false is true or we can say it is 1(default numeric value of true is 1)
So the result for expression b) will be 1(i.e true)
----------------------------------------------------------------------------------------------------
Now i am giving the c++ language code for further clarification:
#include <iostream>
using namespace std;
int main()
{
double a = -10,b=01,c=21;
bool b1=true,b2= false,b3= false;
cout<<"Expression a) a>b||b>c Result :
"<<(a>b||b>c)<<endl;
cout<<"Expression b) b1||b2&&b3 Result :
"<<(b1||b2&&b3);
return 0;
}
Output: