Nested if/else statements (using !, &&, ||)

Hey everyone. I am having trouble fully understanding how nested if/else statements work, especially when they include the !, &&, and || operators. I was assigned some homework and I'm not sure if I'm doing it right. The homework instructions are:

"Using the !, && and || operators....

Retain the program from Chapter 4 - 03 (Grade program) and update the logic for several conditions:

1) Using the ! [NOT] operator:
a) Setup the logic for the original sequence of grades using the ! operator. That is, with ascending or descending grades setup the IF and IF/ELSE statements to display the proper grade entered. Remember when you use an IF or IF/ELSE you are testing based upon the affirmative (Yes or True). So an IF testGrade>=90 will test for your 'testGrade' to be above or equal to 90 and then the ELSE will be less than 90. An IF (!(testGrade>=90)) will provide a result of TRUE when the testGrade is less than 90. You are essentially considering the "ELSE" of the original IF/ELSE in the Grade program. [This may take a little thinking!]. Sometimes in development it's not what is TRUE but what is FALSE that you want to focus on and then work from there."

The code we are supposed to work with is below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Aguirre, Gabriel CSCI 155 CH. 4-03.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
	// Constants for grade thresholds
	const double F_SCORE = 50,
		D_SCORE = 60,
		C_SCORE = 70,
		B_SCORE = 80,
		A_SCORE = 90;

	double testScore;		// To hold a numeric test score.

	// Get the numeric test score.
	cout << "Enter your test score to find out your letter grade: ";
	cin >> testScore;

	// Determine the letter grade.
	if (testScore >= D_SCORE)
	{
		cout << "Your letter grade is: D\n";
	}
	else
	{
		cout << "Your letter grade is: F\n";
	}
		{
		if (testScore >= C_SCORE)
		{
			cout << "Your letter grade is: C\n";
		}
		else
		{
			if (testScore >= B_SCORE)
			{
				cout << "Your letter grade is: B\n";
			}
			else
			{
				if (testScore >= A_SCORE)
				{
					cout << "Your letter grade is: A\n";
				}
				else
				{
			
				}
			}

		}
	}
	cin.get();
    return 0;
}


I wrote this code and I'm not really sure if it's correct but I'm supposed to use it to complete the instructions.

Thanks for any help!
Hope this little exemple I made you can help ! ^^

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
using namespace std;

int main()
{
	int x = 10;
	int y = 10;
	string name = "Wyclef Jean";

	if (x == 10 && y == 10) // <----- && (And) operator.
	{
		cout << "X & Y are equals to 10 !" << endl;

		if (!(name == "Jonhny Cash")) // <----- !() (Not) operator.
		{
			cout << "The name is not Jonhny Cash" << endl;
		}
		else if (x == 15 || y == 10) // <----- || (Or) operator
		{
			cout << "X is equal to 15 OR y is equal to 10, whatever..." << endl;
		}
	}

	return 0;
}


Ask if you need anything !
And from what i've read, it seems they want you to do something like this :

1
2
3
4
5
6
7
8
9
10
11
12
13
14

if (!(boolean))
{
    return false;
}
else
{
    if (...)
    {
        ....
    }
...
return true;
}


You are essentially considering the "ELSE" of the original IF/ELSE in the Grade program. [This may take a little thinking!]. Sometimes in development it's not what is TRUE but what is FALSE that you want to focus on and then work from there."
Topic archived. No new replies allowed.