0
#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?

3
Contributors
3
Replies
4
Views
10 Years
Discussion Span
Last Post by abhinav.sharma
0

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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.