izaak_pyzaak
izaak_pyzaak

Reputation: 960

Is whitespace between the two ='s in an equality operator legal in C++?

I am currently learning C++ from 'Problem solving with C++' (9th, W. Savitch). The book shows an example of a while loop. The while loop looks as follows.

while (ans = = 'Y' || ans = = 'y')
{
    //compound statement
}

ans is of type char. The boolean expression appears to be trying to use the equality operator, and in the context of the //compound statement this makes sense. However, I always thought whitespace was illegal within the equality operator. i.e == is legal, but = = is illegal.

When I copy the code and compile it, my compiler throws the error 'expected expression' when it hits = = as if I am trying to assign an expression to a variable. I am almost certain this is a typo within the book. However, just in case the book is trying to throw a curveball I thought I would ask...

Many thanks!

Upvotes: 4

Views: 85

Answers (1)

T.C.
T.C.

Reputation: 137315

Is whitespace between the two ='s in an equality operator legal in C++?

No. = = is two = tokens. == is one == token. You can't use the former when you mean the latter.

Upvotes: 3

Related Questions