Objective #1: Understand how true and false is represented in
C++.
- When decisions are made in a computer program, they are simply the
result of a computation in which the final result is either true or
false. Computer programs do not have intuition or "gut feelings";
they simply make thousands of yes-or-no-like decisions at blinding speeds.
The computer programmer must set up these decisions within the algorithm
of a computer program to achieve the desired results.
- The value zero (0) is considered to be false by C++. Any positive
or negative value is considered to be true. Many people
assume that only positive one is considered to be true but technically
C++ evaluates any non-zero value to be true.
- Boolean expressions are used in certain kinds of C++ statements.
A Boolean expression evaluates to either true or false. A simple example
of a Boolean expression is (price < 10.00)
where price is a variable. If
price is equal to the value 8.34, then
the Boolean expression as a whole would be considered to be true since
8.34 is indeed less than 10.00.
Objective #2: Use relational operators.
- Relational operators provide the tools with which programs
make decisions with true and false evaluations.
Relational operators:
== equal to
NOTE: this is two equals symbols next to each
other, not to be confused with the assignment operator, =
> greater than
< less than
>= greater than or equal to
<= less than or equal to
!= not equal to
Objective #3: Use logical operators.
- Use this truth table to determine the results of the logical
operators. In this table, 1 represents true and 0 represents false.
| AND |
|
OR |
|
NOT |
| A |
B |
A && B |
|
A |
B |
A || B |
|
A |
!A |
| 0 |
0 |
0 |
|
0 |
0 |
0 |
|
0 |
1 |
| 0 |
1 |
0 |
|
0 |
1 |
1 |
|
1 |
0 |
| 1 |
0 |
0 |
|
1 |
0 |
1 |
| 1 |
1 |
1 |
|
1 |
1 |
1 |
- Logical operators may be mixed within evaluation statements but you
must abide by their precedence rules (aka order of operations):
NOT operator (!), AND operator (&&), OR operator (||)
You must remember that the && operation is always performed
before the || operation because && is similar to multiplication
in normal arithmetic while || is similar to addition.
- Short-circuit evaluation in C++ allows the compiler to quickly
evaluate a logical expression in some cases without having to completely
evaluate each part of the expression.
Objective #4: Be able to use the if statement.
- Practically all computer languages have some sort of if structure.
In C++, the if structure (can be called an "if statement" as well) is a one-way selection structure.
Example:
if (number ==
3)
{
cout << "The
value of number is 3";
}
- All if structures use a control expression (or sometimes called
controlling expression) to determine if the code in the braces
is to be executed. In the example above,
(number == 3) is the control expression.
Note the use of the "double equals" relational operator ==
and not the assignment operator =.
- Be sure to place the semicolon at the end of each statement within
the braces, which can contain many executable statements.
- Realize that you must use the AND operator (&&) to form a
compound relational expression. The the following syntax is invalid:
if (0 < number < 10)
{
cout << number << "
is greater than 0 but less than 10." << endl;
}
Rather the following syntax must be used:
if (0 < number && number <
10)
{
cout << number << "
is greater than 0 but less than 10." << endl;
}
- According to our Coding Standards,
you must use curly braces around the body of an if
statement even if there is only one statement. If you use the following
statement in a program assignment for this class, you will lose points
for not including curly braces even though the statement would not cause
compile errors.
if (num > 10)
cout << num << " is greater than
10";
This is stressed so that beginner programmers develop safe habits and
good style. In the following code segment
if (num > 0)
cout << "num is positive" <<
endl;
cout << "num is not zero"
<< endl;
the body of the if statement is only considered to be the first cout
statement according to the C++ compiler. The second statement would
execute even if num is equal to zero.
The indentation of the second statement has no effect on the way the
compiler interprets this code.
- According to our Coding Standards,
you must not write one line if statements. If you use the following
statement in a program assignment for this class, you will lose points
for not breaking it up over multiple lines even though it would not
cause compile errors.
if (num > 10) cout << num <<
" is greater than 10";
- Avoid the following common mistakes that are made with if
statements:
Always use the "double equals" symbol (i.e. comparison
operator) rather than the assignment operator in control expressions:
int num = 0:
if (num == 5)
{
cout << "hello world" << endl;
}
which would not display "hello world"
rather than:
int num = 0;
if (num = 5)
{
cout << "hello world" << endl;
}
which would assign the value 5 to the variable num and then display
"hello world"
Avoid using an unnecessary semicolon after a control expression:
if (num > 0);
{
cout << "num is positive" <<
endl;
}
which would cause the phrase "num is positive" to be displayed
even if num is negative.
- An interesting use of the mod operator ( % ) is to determine whether a variable is even or odd. Since any even number leaves a remainder of 0 when divided by two and any odd number leaves a remainder of 1 when divided by two, we can use the following if statement
if (num % 2 == 0)
{
cout << num << " is an even number" << endl;
}
else if (num % 2 == 1)
{
cout << num << " is an odd number" << endl;
}
Objective #6: Be able to use the if/else statement.
Objective #7: Be able to use and interpret nested if statements.
- if statements and if
else statements can be nested within one another in order
to model complex decision structures. Be sure to use the braces and
semicolons properly when coding such structures. Also, be sure to rigorously
check the logic of your algorithm since it is quite easy to overlook
possible errors.
- Example:
The if statement that tests for divisibility
by 5 is located inside of the if statement
that tests for divisibility by 3 therefore it is considered to be a
nested if statement.
if (number % 3 == 0)
{
cout
<< number << " is divisible by 3." << endl;
if
(number % 5 == 0)
{
cout << number << " is divisible by 3 and 5."
<< endl;
}
}
Notice the blank lines around the inner if
statement for good style.
Objective #8: Be able to use the switch statement. (This topic may not be covered this semester. Ask the instructor if you are responsible for studying switch statements.)