Logical Operators

See pages 118-123 in A Computer Science Tapestry for more coverage of this information.

Why use logical operators?

So far we have used if statements with single expressions inside of them, such as if (x>10). It is often useful to have more than one condition inside of your if statement. For example, if you wanted to know if a number x was between 10 and 20, you would need to check if x was greater than 10 and x was less than 20. This is not difficult to implement in C++, but before we illustrate this it is important to understand logical AND and OR.

AND and OR

The format of AND and OR statements is the same. They are used between two statements that can be either TRUE or FALSE. That is:

Statement 1 and/or Statement 2

Together, the two statements and the operator result in another expression which can be true or false. With an AND statement, the combined statement is true if both statements are true. With an OR statement, the combined statement is true if at least one of the statements is true. A table summarizing the combinations of true and false statements with AND/OR operators is found on page 123.

Using AND and OR Statements in C++

In Basic using AND and OR statements was straightforward. In C++, symbols replace the convenient words of Basic. The word AND is replaced by the && operator, and the word OR is replaced by the || operator. For example, referring back to the example above, to see if x is greater than 10 and x is less than 20, we would use the statement, "if (x>10 && x<20)". If, however, you wanted to check if x is less than -10 or greater than +10, we would use the statement, "if (x<-10 || x>10)"

NOT, Another Operator

It is sometimes convenient to know the opposite truth of a statement. That is, you may want to know if a number x is not less than 10. The not operator is simply "!" Therefore, to know if x is not less than 10 you use the statement "if (!(x<10))".

Putting it All Together- Assignment 3

Logical statements are an integral part of computer science. They can even be used to develop primitive versions of artificial intelligence. In this case we want to use logical operators to differentiate between different objects. First you must pick a category of objects. As an example I will pick animals. Then you need to come up with a series of questions which when answered with yes/no questions can uniquely identify any one object. It is easiest to come up with the questions if you first make a table of the yes/no answers. Here is the table associated with my animals

Animal Is it a mammal? Can it swim? Is it a pet?
Dog Yes Yes Yes
Otter Yes Yes No
Cat Yes No Yes
Goldfish No Yes Yes
Shark No Yes No
Canary No No Yes

Given this information I can ask three yes/no questions and based on their answers I can uniquely identify an animal. Here is a sample run of the program.

Think of an animal, and answer these questions.
Is it a mammal? Yes
Can it swim? Yes
Is it a pet? No
You are thinking of an otter.

Click Here to Download a DOS Version of the Program comptr9.gif (546 bytes)

Your assignment is to write a program that can differentiate among at least 6 different objects of your choice based on at least 3 different questions. You must use logical expressions that include AND and OR statements. You should also use apstrings in this assignment.  As in the other assignments you should include only what is necessary in your main function, and everything else in other functions.

This program should have comments on almost every line explaining what the program is doing, especially where calculations are made

This program will be graded on the following factors:

  1. How well it works.
  2. How well you use logical operators.
  3. How easy your program is to follow, including your use of functions. This will include things like comments and variables that have meaningful names.
  4. How well you understand your program based on your comments.
  5. How creative your were in your approach to solving the problem.

This assignment will be due Monday, March 2. You must deposit the program in the class folder on the network by this date.

Back to Home Page House3.wmf (25540 bytes)