if statement in C++ implements conditionality:
Syntax:
if ( condition ) statement ;
OR
if ( condition ) statement ;
else
statement ;
COMMON MISTAKES:
leaving out "(" ")" around the "condition"
not putting the ";" at the end of statement
(either following the true path or
false path)
Semantics:
The condition is evaluated and it can be either
true or false. statement following the condition
is evaluated if the condition tests to true
statement following the "else" (if any) is executed
if the condition tests false
Condition:
Comparison operators: > < >= <= == !=
You can construct conditional expressions or
BOOLEAN type: evalutes to true or false
Examples include:
a > b
c != d
and so on
Compound conditional expressions use: logical operators
NOT : ! (negation)
if C is true !C is false and vice versa
AND : && (conjunction)
C && D is true if C is true and D is
true; false otherwise
OR : || (disjunction)
C || D is false if C is false and D is
false; true otherwise
Example of simple conditional statement
"return" statement
exits the function in which you place it:
return expression ;
OR
return;
"return" can also be useful with "if" stmt
avoids long complicated, compound statement
MAKE SURE YOU USE "&&" and "||"
"&" and "|" have a different meaning (bitwise)
if (a > b && c > d)
cout << "This" ;
else
cout << "That" ;
See example7.cpp and example7a.cpp
"bool" type:
values: true and false
operators: logical operators !, &&, ||
Connection between "bool" and "int"
value 0 : false
non zero : true
Use bool rather than int; it is better style
in all cases because it is easier to follow
TRUTH TABLES
C D !C C && D C || D
true true false true true
true false false false true
false true true false true
false false true false false
OPERATOR PRECEDENCE
! highest priority (this is also a UNARY
OPERATOR -- applies to a single OPERAND)
&& and || lower priority than !
Examples of compound logical expressions and
rules of precedence
Usefulness of NESTING if statements:
if (condition1)
if (condition2)
statement1;
else statement2;
else statement3;
Nested If statement example
Dangling else problem:
if (condition1)
if (condition2)
statement1;
else statement2;
Does statement2 associate with "condition2" being false
or with "condition1" being false?
Style hint: make the logic easier to understand
by using { } as needed...
// associate statement2 with condition1 being false
if (condition1)
{
if (condition2)
statement1;
}
else statement2;
// associate statement2 with condition2 being false
// this is the default!!
if (condition1)
if (condition2)
statement1;
else statement2;
OR
// if you want to be more careful:
if (condition1)
{
if (condition2)
statement1;
else statement2;
}
The University of Southern California does not screen or control the content on this website and thus does not guarantee the accuracy, integrity, or quality of such content. All content on this website is provided by and is the sole responsibility of the person from which such content originated, and such content does not necessarily reflect the opinions of the University administration or the Board of Trustees