Page 40 - DCAP404 _Object Oriented Programming
P. 40
Unit 2: Beginning of OOP Language
balance >= cutoff Notes
ch1 < ‘A’
letter1 = ‘a’
The first four expressions involve numerical operands. Their meaning should be readily apparent.
In the fifth expression, ch1 is assumed to be a char type variable. This expression will be true if
the character represented by ch1 comes before T in the character set, i.e., if the numerical value
used to encode the character is less than the numerical value used to encode the letter T.
The last expression makes use of the char-type variable letter. This expression will be true if the
character represented by letter is something other than x.
In addition to the relational and equality operators, C++ contains two logical connectives (also
called logical operators), && (AND) || (OR), and the unary negative operator !. The logical
connectives are used to combine logical expressions, thus forming more complex expressions.
The negation operator is used to reverse the meaning of a logical expression (e.g., from true to
false).
Task Explain how control structures are a very useful programming idea.
2.4.1 The if-else Construct
The if conditional construct is followed by a logical expression in which data is compared and a
decision is made based on the result of the comparison. The syntax is:
if (boolean_expr)
{
statements;
}
else
{
statements;
}
The if-else statement is used to carry out a logical test and then take on of two possible actions,
depending on the outcome of the test (i.e., whether the outcome is true of false).
The else portion of the if-else statement is optional. Thus, in its simplest general form, the
statement can be written as:
if (expression) statement;
The expression must be placed in parentheses, as shown. In this form, the statement will be
executed only if the expression has a nonzero value (i.e., if expression is true).
Notes If the expression has a value of zero (i.e., if expression is false), then the statement
will be ignored.
LOVELY PROFESSIONAL UNIVERSITY 33