#include <iostream>
using namespace std;

int main()
{
     int a;
     cout<<"Enter a number";
     cin>>a;
     [B]if(a=5)[/B]
     {
          cout<<"Five";
     }
     else
     {
     cout<<"Not Five";
     }
}

When I use the assignment, the result is always "five". I understand that a is assiged 5, but there is no statement to check for that. Does the "if" condition execute simply because it has an invalid argument?

The if-statement will check the variable a, to see if it's false or !false. When a is assigned with 5 it's no longer false, but true. That's the reason why it will output Five. Try for example to assign a to 0, in the if-statement. Then it will jump to the else, because it's false, then.

The expression a = 5 is evaluated as bool. Non zero is true. Thus the error.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.