if...

• 03-29-2004
LegendsEnd
if...
its not that I dont get how to use it but i'm just wondering why is it that for example if I had abc as a variable that
Code:

`if (abc==!4)`
wont work(well it does but if I entered in anything it assumes its false and goes onto the else statement)? since ! takes precedence shouldnt it be read
not 4 > == > abc and when reconstructed isnt it saying that if abc is equal to not 4 then?
I tried
Code:

`if (!(abc==4))`
and it worked but isnt it sortve the same as the if statement before just reworded to if its not equal to 4?
• 03-29-2004
Hammer
!4 equates to 0.
so the first test is abc == 0

the second test is completely different.
First abc == 4
then "not" the result.
• 03-29-2004
LegendsEnd
oh, how does !4 equate to 0 though, i still dont really get it
Now I have another problem with the loops, is FOR the same as WHILE except you can do more things variable wise with FOR? It seems pretty much the same and I tried adding a loop to:
Code:

```#include <iostream> int main() {     int a;     std::cout<<"What is 2+2?"<<std::endl;     std::cin>>a;     if(!(a==4)) {         std::cout<<"That would be correct my fellow mathematician."<<std::endl;         system ("PAUSE"); }     else {         std::cout<<"wrong!"<<std::endl;         system ("PAUSE"); }     return 0; }```
so i put
Code:

`while(!(a==4))`
before cout so it can ask the question again but the question just keeps getting asked and loops over and over, is there a way to make it not do that or would I have to create another variable and add a AND statement to the while code?
• 03-29-2004
alphaoide
I don't know if the following is your confusion (just wondering),
if not then nevermind...
Code:

```expression (abc==!4)  is equal (!(abc==4))  // eek, wrong expression (abc!= 4)  is equal (!(abc==4))  // eek, right```
• 03-29-2004
Zach L.
Make sure that you capture the input in the while loop as well:
Code:

```while(a != 4) {   std::cout << "...";   std::cin >> a; }```
• 03-30-2004
DougDbug
!4
Quote:

oh, how does !4 equate to 0 though, i still dont really get it
Well, think about how if(x) works... if x is non-zero the code following will execute. If x is zero, then the code following will not execute.

if(4); //Always "true"
if(!4); // Always "flase" , so the stuff inside the parenthesis must be zero!

A logical NOT !X is different from a bitwise NOT ~X. This: ~4 does not evaluate to zero.
• 03-31-2004
major_small
here's another way to think about it... when you just put if(constant), it kinda acts like this: if(constant==constant) which it always will, in your case, it would be if(4==4), and in your implementation if(!a), it would be similar to if(!a==a), which it doesn't...
• 03-31-2004
!4
I like DougDbug's answer. AFAIK, all conditional statements need to be phrased in a true/not-true dichotomy. In C++ zero equates to false (not-true), and any non-zero values equate to true. Therefore if you say:

if(4)

then 4 is non-zero so it is interpretted by the compiler as saying if(true). If you say:

if(!4)

then the compiler probably first converts the statement to this:

if(!true)

and then to this:

if(false)

which is the same as this

if(0)

so in the end !4 == 0 when used as a conditional. Logically !4 could mean the value could be anything but 4 like -100 or 0 or 3, but when used as a conditional, !4 means 0, given the need for results that equate to either true or not-true.
• 03-31-2004
LegendsEnd
I still cant get the loop working but I understand the !4 thing equating to 0 now, thanks, and btw the program is supposed to be a joke type thing where 2+2 equals anything except 4 :P
• 03-31-2004
alphaoide
Post the code that you have so far, please