I want to learn c++ through practicing and i am researching on operators of c++. Kindly tell me that ! has higher precedence than relational operators or relational operators has higher precedence than ! operator?
#include <iostream>
using namespace std;
int main()
{
for(int i=0;i<=6;i++)
{
if(!(i>3||i<5))
cout << i << "\t";
}
}
I created the above program and when i compile and run this program nothing is displayed in the console window. Below is the code of my another program.
#include <iostream>
using namespace std;
int main()
{
for(int i=0;i<=6;i++)
{
if(!(i>3||i<5))
cout << i << "\t";
}
}
The conditional statement of my first program is if(!(i>3||i<5)) and when compile and run this program nothing is displayed in the console window. The conditional statement of my second program is if(!(i>3||i<5)) and the output of this program is 0 1 2 3 5 6. I was expecting that i will see the same output from these two conditional statements. Why i am getting the different output from both conditional statements?
Kindly tell me that ! has higher precedence than relational operators
And for completeness, the literal answer to your question is that logical NOT has higher precedence than logical AND and logical OR. (&& has higher precedence than ||, although using parentheses for clarity can still be recommended).
it works just like english ... write the condition you want in english, then write out what values satisfy it in the range (0-6, here). Now convert that into code (or just the math expressions).
Think about the condition and the range of possible values for i. With these values for i, the if statement is always true so the else is never executed so no output.
To paraphrase the question - what's the use of the condition in this case???
you still have not done what I asked, or you would see it.
what does it say?
I will do this one..
if i is between 0 and 6 and greater than three and less than 5 do nothing.
this begs the question: what values between 0 and 6 is greater than 3?
4,5,6 are.
what values are less than 5?
0,1,2,3,4 are.
put the two groups together (because of the OR, they work together. AND is different!)
0,1,2,3,4,5,6
what value between 0 and 6 is missing, such that it would execute the cout statement?
none are! it will NEVER get to the cout with this logic.
now, that is clearly not clicking for you, so we need to see (in your own words) the english YOU think you SEE when you read the logic. What value do you think it SHOULD print, and why do you think that?