Relational Operators and If Statements


- Using if -
Remember that = is the assignment operator and does not mean equals. In If Statements use = = to mean equals. For example:

	if(num == 0)
	{
		cout << "\nYou failed!" << flush;
	}

(Note: The flush command makes sure everything waiting in buffer gets display.)

- Using && -
More than and less than are indicated just as they are in Math class. They can be used with the AND (&&) logical operator as in the code segment below.

	int age;
	cin >> age;
	if(age > 12 && age < 20)
	   cout << "\nYou are a teenager." << endl:
(Note: The endl command acts like flush terminating with a new line.)

- Using || -
If you ask for a yes/no response and the user has the Caps Lock on, the OR (||) operator can be useful:

	if(response = 'y' || response = 'Y)
		cout << "\nYou answered YES.? << endl;


- Using else -
But what if you want the program to respond to No answers as well? The you can use the else statement.

	if(response = 'y' || response = 'Y)
		cout << "\nYou answered Yes." << endl;
	else << "\nYou didn't answer Yes!" << endl;


- Using else if -
When you want the program to respond to three or more possible conditions, use one or more else if statements after the if statement and before the else statement.

	#include 
	using namespace std;

	int main()
	{
	char response;
	cout << "\nAre you human? (Enter Y for Yes or N for No):  ";
	cin >> response;
	if(response == 'y' || response == 'Y')
	cout << "\nYou answered No." << endl;
	else if(response == 'n' || response == 'N')
	cout << "\nYou answered No! You must not be human!" << endl;
	else
	cout << "\nPlease press Y for Yes or N for No." << endl;
	cin.get(); cin.get();
	return 0;
	}

ASSIGNMENT #3


Lesson: Relational Operators and If Statements
Due Date: February 19

1.Write a GPA calculator program that asks for the numerical grades (0-4) for six classes and then prints out the grade average for those classes.
2.Modify the above program so that it prints ?Uh-oh!? to the screen every time a grade lower than 2 ( C ) is entered. Print -That's okay.- for a 2, -Good.- for 3, and -Great!- for a 4.
3.Write a second program that asks the user for their pre-tax salary. Print the amount of taxes the person owes according to the following rates:
10% tax if salary is less than $20,000
15% tax if between $20,000 and $55,000
20% tax if more than $55,000