if statement in C++


We now introduce a simple version of C++’s if statement that allows a program to take
alternative action based on whether a
condition is true or false. If the condition is true, the
statement in the body of the
if statement is executed. If the condition is false, the body
statement is not executed. We’ll see an example shortly.
Conditions in
if statements can be formed by using the equality operators and relational operators. The relational operators all have the same level of
precedence and associate left to right. The equality operators both have the same level of precedence, which is lower than that of the relational operators, and associate left to right.

Common Operators Errors in C++

Common Programming Error 1.0
Reversing the order of the pair of symbols in the operators !=, >= and <= (by writing them as
=!, => and =<, respectively) is normally a syntax error. In some cases, writing != as =! will
not be a syntax error, but almost certainly will be a
logic error that has an effect at execution
time. A
fatal logic error causes a program to fail and terminate prematurely. A nonfatal logic error allows a program to continue executing, but usually produces incorrect results.

Common Programming Error 1.1
Confusing the equality operator == with the assignment operator = results in logic errors.
Read the equality operator should be read “is equal to” or “double equals,” and the assignment operator should be read “gets” or “gets the value of” or “is assigned the value of, confusing these operators may not necessarily cause an easy-torecognize syntax error, but may cause extremely subtle logic errors.


Example 1:

 
 // Comparing integers using if statements, relational operators
 
// and equality operators.
 #include <iostream> // allows program to perform input and output

using std::cout; // program uses cout
using std::cin; // program uses cin
using std::endl; // program uses endl
// function main begins program execution
 int main()
 {
 int number1; // first integer to compare
 int number2; // second integer to compare
cout << "Enter two integers to compare: "; // prompt user for data
cin >> number1 >> number2; // read two integers from user
if ( number1 == number2 )
cout << number1 << " == " << number2 << endl;
if number1 != number2)
cout << number1 << " != " << number2 << endl;
if (number1 < number2)
cout << number1 << " < " << number2 << endl;
if (number1 > number2 )
cout << number1 << " > " << number2 << endl;
if (number1 <= number2)
cout << number1 << " <= " << number2 << endl;
if (number1 >= number2 )
cout << number1 << " >= " << number2 << endl;
 } // end function main

Output:

Enter two integers to compare: 3 7
3 != 7
3 < 7
3 <= 7
Enter two integers to compare: 22 12
22 != 12
22 > 12
22 >= 12
Enter two integers to compare: 7 7
7 == 7
7 <= 7
7 >= 7


Example 2:

1 // Calculate the product of three integers
2 #include <iostream> // allows program to perform input and output
3 using namespace std; // program uses names from the std namespace
45
// function main begins program execution
6 int main()
7 {
8 int x; // first integer to multiply
9 int y; // second integer to multiply
10 int z; // third integer to multiply
11 int result; // the product of the three integers
12
13
cout << "Enter three integers: "; // prompt user for data
14 cin >> x >> y >> z; // read three integers from user
15 result = x * y * z; // multiply the three integers; store result
16 cout << "The product is " << result << endl; // print result; end line
17 } // end function main