CMPSC 201 Chapter Notes - Chapter 4: Boolean Expression
Relational operators: Determines whether a relationship exists between two values
>= : Greater than or equal to
<= : Less than or equal to
== : Equal to
!= : Not equal to
Relational operators are binary
Relational expressions are Boolean expression (true or false only)
== Determines whether a variable is equal to another value
= Assigns operator’s left value to the right value
Relational expressions have a higher precedence than assignment operators
The if statement can cause other programs to be executed only under certain conditions
if (condition)
cout << “statement” ;
To prevent round off errors, it’s better to stick with less than or greater than sign
For if statements, anything other than 0 is true
Always use braces. Ex-
if (condition)
{statement
statement
statment}
if the if statement is not true, the the program follows the else statement
Nested if. If two statements exist then,
if ( condition 1)
{ if (condition 2)
{ cout <<statement
}
else //for statement 2
{
}
}
else (statement 1)
{
cout <<
}
if/ else if method:
If (statement 1 is true)
{ steps
}
else if (statement 2 is true) - it is called the trailing else
{steps
}
else (if none is true)
The disadvantages of if/ else if statements:
find more resources at oneclass.com
find more resources at oneclass.com
Document Summary
Relational operators: determines whether a relationship exists between two values. Relational expressions are boolean expression (true or false only) == determines whether a variable is equal to another value. = assigns operator"s left value to the right value. Relational expressions have a higher precedence than assignment operators. The if statement can cause other programs to be executed only under certain conditions if (condition) cout << statement ; To prevent round off errors, it"s better to stick with less than or greater than sign. For if statements, anything other than 0 is true. {statement statement statment} if the if statement is not true, the the program follows the else statement. If two statements exist then, if ( condition 1) { cout <