GeeksforGeeks » Interview Questions

if else

(6 posts)
  • Started 3 months ago by vengatachalam
  • Latest reply from PINTUGUPTAJUIT

Tags:

  1. vengatachalam
    guest
    Posted 3 months ago #

    int a=10,b=10;
    if(a=b)
    cout<<"equal";
    else
    cout<<"Not Equal";

    output:
    Not equal

  2. kartik
    Moderator
    Posted 3 months ago #

    Use "==" instead of "=" in if condition.

    int a=10, b=10;
    if(a==b)
      cout<<"equal";
    else
      cout<<"Not Equal";  
  3. crazypro
    guest
    Posted 3 months ago #

    output should be "equal"..
    can any one explain why Not equal is given as o/p here

  4. kartik
    Moderator
    Posted 3 months ago #

    I think you are getting confused.

    The following program prints "equal"

    #include<iostream>
    using namespace std;
    
    int main()
    {
        int a=10, b=10;
        if(a==b)
            cout<<"equal";
        else
            cout<<"Not Equal";
        return 0;
    }
    

    In your original program, you were using = instead of ==

  5. vpshastry
    guest
    Posted 3 months ago #

    The question itself is wrong, whether you use '=' or '==', you get the same output for a=10, b=10(not zero values of a &b).

  6. PINTUGUPTAJUIT
    Member
    Posted 3 months ago #

    output will be 'equal' inside the if() condition assignment operation is happen it will take true condition.
    so it will print equal.


Reply

You must log in to post.

RSS feed for this topic