operators in c++

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?
Because i>3||i<5 is always true.

i > 3 catches all of 4,5,6
i < 5 catches all of 0,1,2,3,4


So the ! of that is always false, so you never see anything.
Instead of OR || ... you should use AND &&
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).

https://en.cppreference.com/w/cpp/language/operator_precedence
Last edited on
Why i should use AND && instead of OR || in this program?
> Why i should use AND && instead of OR || in this program?
No reason at all.

It's really what you want to happen.

Apparently, you want to see some output, so you need a condition that evaluates to true at some point.

If ! is confusing you that much, write the code like this instead.
1
2
3
4
5
6
if( (i>3||i<5) ) {
  ; // do nothing
} else {
  cout << i << "\t";
}

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).
if( (i>3||i<5) ) {
; // do nothing
} else {
cout << i << "\t";
}

i compiled and run this code and nothing is displayed in the console window. what is the purpose of using else statement with if statement in my case?

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???
> i compiled and run this code and nothing is displayed in the console window.
I never said it would.
It's functionally the SAME as if(!(i>3||i<5))

> what is the purpose of using else statement with if statement in my case?
Maybe to try and teach you something.

Here's another.
https://en.wikipedia.org/wiki/Boolean_algebra
if( (i>3||i<5) ) {
; // do nothing

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?
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>

using namespace std;

int main()
{
    cout << "Test 1\n";
    for(int i=0; i<=6; i++)
    {
        if( !( i>3 || i<5 ) )
            cout << i << "\t";
    }
    cout << '\n';
    
    cout << "Test 2\n";
    for(int i=0; i<=6; i++)
    {
        if( ( i>3 || i<5 ) )
            cout << i << "\t";
    }
    cout << '\n';
    
    cout << "Test 3\n";
    for(int i=0; i<=6; i++)
    {
        if( !( i>3 && i<5 ) )
            cout << i << "\t";
    }
    cout << '\n';
    
    cout << "Test 4\n";
    for(int i=0; i<=6; i++)
    {
        if( ( i>3 && i<5 ) )
            cout << i << "\t";
    }
    cout << '\n';
}



Test 1

Test 2
0 1 2 3 4 5 6
Test 3
0 1 2 3 5 6
Test 4
4
Program ended with exit code: 0
Topic archived. No new replies allowed.