Boolean expressions are used in a program to decide if something should or should not be done. There are two main sets of operators that work with Boolean expressions:
The relational operators take two values and evaluate to true or false based on the relation of the two values. There are six relational operators.
Name | Symbol | C++ Example | Meaning |
---|---|---|---|
Equal to | == | val1 == 10 | true if val1 is equal to 10, false otherwise |
Not equal to | != | val1 != 10 | true if val1 is not equal to 10, false otherwise |
Greater than | > | val1 > 10 | true if val1 is greater than 10, false otherwise |
Greater than or equal to | >= | val1 >= 10 | true if val1 is greater than or equal to 10, false otherwise |
Less than | < | val1 < 10 | true if val1 is less than 10, false otherwise |
Less than or equal to | <= | val1 <= 10 | true if val1 is less than or equal to 10, false otherwise |
The Boolean operators take two Boolean values and evaluates to ture or false based on the definition of the operator. There are three Boolean operators.
Symbol: &&
An AND operation evaulates to true if both of the operands are true; otherwise it evaluates to false. The following truth table shows how AND is defined:
val1 | val2 | val1 && val2 |
---|---|---|
true | true | |
true | false | |
false | true | |
false | false |
Symbol: ||
An OR operation evaulates to true if at least one of the operands is true; otherwise it evaluates to false (when both of the operands are false). The following truth table shows how OR is defined:
val1 | val2 | val1 || val2 |
---|---|---|
true | true | |
true | false | |
false | true | |
false | false |
Symbol: !
A NOT operation evaluates to the opposite Boolean value of the operand. If the operand is true, the operation evaulates to false. If the operand is false, the operation evaluates to true. NOT is a unary operator (takes only one operand) instead of a binary operator (takes two operands) like AND and OR. The following truth table shows how NOT is defined:
val1 | ! val1 |
---|---|
true | |
false |
The general form of an IF-THEN statement is:
if ( cond ) statement;where:
if | is the C++ keyword to specify the IF statement |
cond | is an Boolean expression |
statement | is a C++ statement that will be executed if the Boolean expression cond evaluates to true |
The general form of an IF-THEN-ELSE statement is:
if ( cond ) statementA; else statementB;where:
if | is the C++ keyword to specify the IF statement |
cond | is an Boolean expression |
statementA | is a C++ statement that will be executed if the Boolean expression cond evaluates to true |
else | is the C++ keyword to seperate statementA from statementB |
statementB | is a C++ statement that will be executed if the Boolean expression cond evaluates to false |
A NESTED-IF statement is simply an IF-THEN-ELSE statement that has another IF statment for its statementB. This can be repeated as many times as desired. The general form of an NESTED-IF statement is:
if ( cond1 ) statementA; else if ( cond2 ) statementB; else if ( cond3 ) statementC; else if ( cond4 ) statementD; else statementE;where:
if | is the C++ keyword to specify the IF statement |
cond1, cond2, cond3, cond4 | is an Boolean expression |
statementA | is a C++ statement that will be executed if the Boolean expression cond evaluates to true |
else | is the C++ keyword to seperate statementA from statementB |
statementB | is a C++ statement that will be executed if the Boolean expression cond1 evaluates to false and cond2 evaluates to true |
statementC | is a C++ statement that will be executed if the Boolean expressions cond1 and cond2 evaluate to false and cond3 evaluates to true |
statementD | is a C++ statement that will be executed if the Boolean expressions cond1, cond2 and cond3 evaluate to false and cond4 evaluates to true |
statementE | is a C++ statement that will be executed if the Boolean expressions cond1, cond2, cond3 and cond4 evaluate to false |
You may turn in the assignment to your TA during lab or place it in his mailbox in 905 SEO. It is suggested that you place it in his mailbox just in case you are unable to attend lab. Hand in a print out of your program to do the folliwng to your TA.
Create a c++ program on your UIC icarus account that will calculate the income taxes owed based on the following table.
Income Range | Income Tax Amount |
---|---|
$0 - $2,000 | Nothing |
$2,001 - $20,000 | 4% * Each dollar earned over $2,000 |
$20,001 - $40,000 | $720 + 8% * Each dollar earned over $20,000 |
$40,001 - $80,000 | $2,320 + 16% * Each dollar earned over $20,000 |
$80,001 - $120,000 | $8,720 + 24% * Each dollar earned over $20,000 |
$120,001 + | $18,320 + 32% * Each dollar earned over $20,000 |
Hand in a print out of your program to your TA.