back


The Relational Operators

<	less than
<=	less than or equal to
==	equal to
>	greater than
>=	greater than or equal to
!=	not equal to
The reason they are called "relational" is that they evaluate how the two terms on either side relate to each other. For example, 8 < 10 evaluates to TRUE. The expression 8 < 7 would evaluate to FALSE. Much of the programming you will do is simply testing to see if certain conditions are TRUE or FALSE.

It works the same way we think. Whenever we make decisions, we are testing conditions. When you are hungry, you eat. If your brain were a computer program using C++, it would say:

IF (HUNGRY equals TRUE)
	EAT
ELSE
	DON"T EAT

However, C++ doesn't really say "TRUE" or "FALSE." Instead it uses 0 to equal FALSE and 1 to equal TRUE. To make matters more confusing, it doesn't just use the equals sign to mean equals. It uses two equal signs together. So, C++ would really say something more like:

IF (HUNGRY==1)
	cout<< "Make a snack, Jack.";
ELSE
	cout<< "No food for you, Lou!";
Try this program to test out how the relational operators work with the Modulus operator.

Copy and paste this piece of code into your compiler and fun the program.

Check out the =>[code]

Now, do the same with this revised code. Run this program to compare the results using ELSE.

Check out the =>[code]

Remember that in C++ a statement like (num%denum ==0) is evaluated by the program as being either TRUE or FALSE. C++ translates FALSE to mean the integer 0. The integer 1 (or any other integer besides zero) is equivalent to TRUE.

Now, let's try a program to find the larger of two numbers.

Check out the =>[code]

Finally, copy and paste this code to find the largest of three numbers:

Check out the =>[code]

Assigment

Do Project 5-1 on page 87 of Knowlton

The project is that you have a group of people who need to be transported on buses and vans. You can charter a bus only if you can fill it. Each bus holds 50 people. You must provide vans for the 49 or fewer people left over after you have filled the buses. Write a program that accepts a number of people and determines how many buses must be chartered and reports the number of people left over that must ride in the vans. Note: use the modulus operator to determine the number of people left over. This project to be signed off by me.