logical operators in c++

Discussion in 'C++' started by hassan ali, Jun 24, 2021.

Tags:
  1. hassan ali

    hassan ali New Member

    Joined:
    Mar 19, 2021
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    1
    Gender:
    Male
    i am a novice programmer and i want to learn through practicing.
    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
    for(int i=0;i<=6;i++)
    {
    if((i<=3)&&(i==5))
    continue;
    cout << i << "\t";
    
    }
    }
    I created the above code and my required output of this code is 4 6. when i compile and run this code the output is 0 1 2 3 4 5 6 which is not my required output. I can get my required output through || operator.

    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
    for(int i=0;i<=6;i++)
    {
    if((i<=3)||(i==5))
    continue;
    cout << i << "\t";
    
    }
    }
    
    In the above code the output is 4 6 which is my required output. why i can't get my required output through this conditional statement if((i<=3)&&(i==5)) ?
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    You need both the conditions to be true and it is never the case.

    When i is less than or equal to 3 it is not equal to 5 and when i is equal to 5 it is not less than or equal to 3.
     
  3. sakshijn

    sakshijn New Member

    Joined:
    Jun 25, 2022
    Messages:
    2
    Likes Received:
    1
    Trophy Points:
    3
    Gender:
    Female
    Basically && operator is AND operator return true when both the conditions are true
    but || operator is OR operator return true when either 1st condition or 2nd condition is true.
    and in the given question for the output 4 6 you have to use || operator.
     
    shabbir likes this.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice