Ask your own question, for FREE!
Computer Science 23 Online
OpenStudy (anonymous):

C++ What does =! mean I wrote that #include using namespace std; int main() { int i , s; for (i=0 ; i <= (1);i++) { if (i =! 2); s=1; s=0; cout << s << endl; } return 0; } And that appears listing 00000 ... for ever which isn't terminating!! Why does this happen?? Why didn't it give me a bug as it isn't terminate algorithm ?

OpenStudy (anonymous):

@e.mccormick

OpenStudy (e.mccormick):

Do you mean != ?

OpenStudy (e.mccormick):

Also, the way it is written, that if will do nothing. Look at the example here: http://www.cplusplus.com/doc/tutorial/control/ Note the lack of a ; after the if.

OpenStudy (anonymous):

no != it means doesn't equal. I just wrote =! and the program blows up .You can try it it is giving zero after another without terminating.

OpenStudy (e.mccormick):

Of course it blows up. You are saying !2, that means not two. Because two is not zero, that it is true and with the not it is false. That means you are trying to assign an integer i with a boolean result. That does not work.

OpenStudy (e.mccormick):

Even if you fix that, you still have a problem with the for statment. For a single statement: ``` if(condition) statement; ``` For a block of statements: ``` if(condition){ block; of; statements; } ```

OpenStudy (anonymous):

I don't think so as it should be == not just = Can you try it on C++ program.

OpenStudy (e.mccormick):

== is to test. = is to assign. If assignment works, it can return a true. So there are valid constructions with = inside an if condition. It is just you are trying to set an integer value to a boolean.

OpenStudy (anonymous):

OK i tried to just right = and it worked. Can simplify what does i =! 2 mean??

OpenStudy (e.mccormick):

I don't think you want i =! 2 or i = 2. The forst is a constructiont at is odd and would not work.

OpenStudy (e.mccormick):

The way it is written it is very unclear what you are trying to do. I can not say exactly what is right or wrong without knowing what it is trying to do.

OpenStudy (anonymous):

I just wanna see what does it mean ? i was playing with this code :D

OpenStudy (anonymous):

more precisely why =! gave non terminating result in the command?

OpenStudy (e.mccormick):

Well, the !2 part means false: ``` #include <iostream> using namespace std; int main() { cout << "!2 gets " << !2 << " hmm.\n"; return 0; } ``` Try running that and see what you get.

OpenStudy (e.mccormick):

See, ! 2 has a valid meaning.

OpenStudy (e.mccormick):

Becase ! 2 is valid, the fact there is an = in front of it makes it an assignment.

OpenStudy (e.mccormick):

Another test case to show what is happening: ``` #include <iostream> using namespace std; int main() { bool temp; temp =! 2; cout << "Temp is: " << temp << endl; return 0; } ```

OpenStudy (anonymous):

aha so every loop i restart its value to 0 ( which is !2) thus it continue for ever

OpenStudy (e.mccormick):

Yes.

OpenStudy (e.mccormick):

See, in C++, the value of false is also seen as the integer 0. So some automatic type casting goes on and you end up with the integer version of false, or 0.

OpenStudy (anonymous):

last thing if you won't mind. I took at the very beginning that the algorithm should be terminating so why can't C++ know that?

OpenStudy (e.mccormick):

I am not sure what you mean by: "the very beginning that the algorithm should be terminating"

OpenStudy (anonymous):

I mean basics of an algorithm is that it should come to end point

OpenStudy (anonymous):

Anyway thanks a lot man. Have a good time:D

OpenStudy (e.mccormick):

An algorithm is a step by step set of procedures to perform a task. Do you mean the loop? `for (i=0 ; i <= (1);i++)`

OpenStudy (anonymous):

yea C++ should make sure that it would come to an end

OpenStudy (e.mccormick):

No. There are reasons for an infinite loop. It is up to the programmer that they do not do one without a need. For example, a game usually syart with something like: ``` alive = true; while(alive}{ play(); } ``` That is an infinite loop. It is up to something else to terminate the loop, like a person dying in the game. A computer only does what it is told to do. If a person tels it to do the wrong thing, it will not know or care.

OpenStudy (anonymous):

OK that helps much man. Thanks again ^_^

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!