//Filename booleanandequal.cpp /* Author: Lee Devlin Date: Jan 16, 2011 This program demonstrates two concepts. The first is testing a boolean condition to be either true or false by examining an integer value of 'a' which is input by the user. If the boolean is true (i.e., a's value is any integer but 0) the first if condition will execute. If the value for a is false, i.e., a is 0, then it will not execute. The second if statement inverts that logic. The third statement if ( a == 0) is the correct conditional statement and will work as expected. The last two if statements show the common error of using a single = sign in testing a condition. Instead of producing the desired result, an assignment occurs. If a is assigned to 0, the statement following it will never execute since 0 is equivalent to FALSE. If a is assigned to anything other than 0, i.e., a value of 3, the condition is always TRUE since 3 is not zero. In addition, the value for a is now changed to 3. */ #include using namespace std; int main() { int a = 0; cout << "Enter an integer value for a: "; cin >> a; if (a) cout << "A test \"If (a)\" is successful. The value of a is " << a << endl; if (!a) cout << "A test \"If (!a)\" is successful. The value of a is " << a << endl; if (a == 0) cout << "A test of \"If (a == 0)\" is successful. The value of a is " << a << endl; if (a = 0) cout << "A test of \"If (a = 0)\" is successful. The value of a is " << a << endl; if (a = 3) cout << "A test of \"If (a = 3)\" is successful. The value of a is " << a << endl; system("pause"); }