Question:
Novice C++ question: In while loops, why does "!=" not mean "not equals"?
Paul
2016-03-07 10:58:15 UTC
For example, when writing a simple loop that keeps asking for the password until you type in the correct one, like -

#include
#include

using namespace std;

int main()

{
string pass;
do
{
cout << "Enter your pw" << '\n';
cin >> pass;
} while (pass != "fudge");
cout << "Correct";
}

Why do you get an error if you type pass = "fudge" instead of pass != "fudge"? In my understanding, I thought it was pass = "fudge" because pass != meant "not = fudge".

Thanks!
Four answers:
Chris
2016-03-07 11:12:06 UTC
Independent of the loop logic, pass = "fudge" is an assignment, not a comparison.



a = 3; // set variable a to new value 3



if (a == 3) // check if value stored in a is equal to 3



You can in fact use an assignment inside a test, like this: if (a = 3) ...

This will set a to 3, then convert a into a Boolean value (true / false) to perform the test. This works for numbers, because C++ will interpret a zero as "false", and everything else as "true".

However a string cannot be implicitly converted into a Boolean value, which is what causes the error.



Once you "fix" it to say

... while (string == "fudge")

the loop logic will no longer work. The loop continues to run as long as the while condition is true, so entering anything other than "fudge" will now exit the loop.



Edit:

Regarding your update, you have it exactly backwards.



The while(pass != "fudge") part belongs to the do { } loop, not the cout line after it.



Here's pseudocode:



do {



EatMoreFood();



} while (you are hungry);



As soon as the while condition *fails*, the execution leaves the loop.



Back to the code, the loop keeps going while the entered password *doesn't* match "fudge".
mark_poc
2016-03-07 22:33:32 UTC
Look at it this way: When you have, while( pass != "fudge") you are simply making a statement. You are saying pass is not equal to fudge. Now that may be a true statement or maybe a false statement, but it is just a simple statement. Just like I can say that today is Sunday. That's a simple statement. It's either a true or false statement.



So in a while loop if the statement is true the loop continues but if the statement is false the loop ends. So don't read it like this:



while pass is not equal to fudge (keep looping)

instead read it like this:

while said statement is true (keep looping)



"Why does pass=="fudge" make the program loop when you type in "fudge"?



Well, is said statement true or false? If it's true it loops but if that statement is a lie then it stops.



Remember, it's ----> while(this statement is true and not a lie) keep looping.



It's while(true).



The same with an if statement. It's ----> if(this statement is true and not a lie).



OK, so you simply make a statement and it will be evaluated as to whether it is true or a lie. True it keeps looping and a lie it stops. That is how you look at it.
?
2016-03-07 11:02:30 UTC
Yes, != means not equal
sachin
2016-03-08 01:05:55 UTC
every programming language have their own syntax


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...