Flylib.com

Books Software

 
 
 

6.1 if Statement

I l @ ve RuBoard

6.1 if Statement

The if statement allows you to put some decision-making into your programs. The general form of the if statement is:

if (


condition


)


statement;


If the expression is true (nonzero), the statement is executed. If the expression is false (zero), the statement is not executed. For example, suppose you are writing a billing program. At the end, if the customer owes nothing or if he has credit (owes a negative amount), you want to print a message. In C++ this is written:

if (total_owed <= 0) 
    std::cout << "You owe nothing.\n";

The operator <= is a relational operator that represents less than or equal to. This statement reads, "If the total_owed is less than or equal to zero, print the message." The complete list of relational operators is found in Table 6-1.

Table 6-1. Relational operators

Operator

Meaning

<=

Less than or equal to

<

Less than

>

Greater than

>=

Greater than or equal to

==

Equal

!=

Not equal

Multiple relational expressions may be grouped together with logical operators . For example, the statement:

if ((oper_char == 'Q')  (oper_char == 'q'))
    std::cout << "Quit\n";

uses the logical OR operator ( ) to cause the if statement to print "Quit" if oper_char is either a lowercase "q" or an uppercase "Q". Table 6-2 lists the logical operators.

Table 6-2. Logical operators

Operator

Usage

Meaning

Logical OR ( )

( expr1 ) ( expr2 )

True if expr1 or expr2 is true

Logical AND ( && )

( expr1 ) && ( expr2 )

True if expr1 and expr2 are true

Logical NOT ( ! )

! ( expr )

Returns false if expr is true; returns true if expr is false

Multiple statements after the if may be grouped by putting them inside curly braces ({ }). For example:

if (total_owed <= 0) { 
    ++zero_count; 
    std::cout << "You owe nothing.\n";
}

For readability, the statements enclosed in curly braces are usually indented. This allows the programmer to quickly tell which statements are to be conditionally executed. As you will see later, mistakes in indentation can result in programs that are misleading and hard to read.

I l @ ve RuBoard
I l @ ve RuBoard

6.2 else Statement

An alternative form of the if statement is:

if (


condition


)


statement


; 
else


statement


;

If the condition is true, the first statement is executed. If it is false, the second statement is executed. In our accounting example, we wrote out a message only if nothing was owed. In real life, we probably want to tell the customer how much he owes if there is a balance due.

if (total_owed <= 0) 
    std::cout << "You owe nothing.\n"; 
else 
    std::cout << "You owe " << total_owed << " dollars\n";

Note to Pascal programmers: Unlike Pascal, C++ requires you to put a semicolon at the end of the statement before the else .

Now consider this program fragment:

if (count < 10)     // If #1 
    if ((count % 4) == 2)   // If #2
        std::cout << "Condition:White\n"; 
  else   // (Indentation is wrong)
      std::cout << "Condition:Tan\n";

There are two if statements and one else . To which if does the else belong? Pick one:

  1. It belongs to if #1.

  2. It belongs to if #2.

  3. You don't have to worry about this situation if you never write code like this.

The correct answer is 3. According to the C++ syntax rules, the else goes with the nearest if , so 2 is syntactically correct. But writing code like this violates the KISS principle (Keep It Simple, Stupid). It is best to write your code as clearly and simply as possible. This code fragment should be written as follows :

if (count < 10) {       // If #1
    if ((count % 4) == 2)   // If #2 
        std::cout << "Condition:White\n"; 
    else 
        std::cout << "Condition:Tan\n"; 
}

From our original example, it was not clear which if statement had the else clause; however, adding an extra set of braces improves readability, understanding, and clarity.

I l @ ve RuBoard