Confusion with the if/else statement

Hi. I am having trouble with a programming assignment as it pertains to the concept of the if/else statement. For example, we are supposed to enter a number 12, then enter a number 15, and the output is supposed to display the greater number. Here is the assignment below.

Write a program that determines the bigger of the two numbers provided by the user. At first, the program asks the user to enter a number. Then it asks the user to enter another but different number. Following that, it determines the bigger of the two numbers and displays it to the user.





Implementation



Do this assignment by using an if/else statement.





Testing



Use Input/Output dialog shown in test run 1 and test run 2.

(User input is shown in bold).



Test Run 1



Enter the First Number

12

Enter a Different Second Number

15



First number: 12

Second number: 15

Larger number: 15



Test Run 2



Enter the First Number

15

Enter a Different Second Number

12



First number: 15

Second number: 12

Larger number: 15



Discussion Of the If/Else Statement



Full Syntax



The syntax of the if/else statement is given below:



if (expression)

{

//Enter here one or more statements.



}

else

{

//Enter here one or more statements.



}



The word if and else are reserved words and must be used in the if/else statement.



Following the reserved word if, specify an expression within parentheses.



Following the parenthesized expression is an open brace and a close brace as shown above. You can put any number of statements within those braces.



The word else is a reserved word and must be used in the if/else statement as above.



In the else part of the if/else statement, following the word else is an open brace and a close brace as shown above. You can put any number of statements within those braces.





In the if/else statement, when the value of the expression is true, then the statements within braces in the if part are executed. Otherwise, the statements within the braces in the else part are executed.



Always, either the statements in the if part or the statements in the else part are executed. Never the statements in both the parts are executed. Also never the statements in both the parts are ever skipped.



On completing the if/else statement, the program continues to the statement following the if/else statement.



Short Syntax.



In the if/else statement, in the if or the else part, when there is only a single statement within a pair of braces, then that pair of braces can be skipped. However, if any of the pair of braces contain multiple statements, then that pair of braces cannot be skipped.


So my question is what code do I use to determine which number is bigger? I realize you use the cin>> to enter the variable, but what do you use to calculate the size of the number? Thank you.
Here is a quick example showing how to determine the bigger number:

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

int main()
{
	// Declare variables
	int a;
	int b;
	
	// Ask for first number and read it
	cout << "Please enter first number: ";
	cin >> a;
	cout << endl;

	// Ask for second number and read it
	cout << "Please enter second number: ";
	cin >> b;
	cout << endl;

	// Condition statement to check whether a is greater
	if (a > b)
	{
		cout << "The greater number is: " << a << endl;
	}
	// Check whether b is greater
	else if (b > a)
	{
		cout << "The greater number is: " << b << endl;
	}
	// Or if they are equal
	else
	{
		cout << "Both numbers are equal." << endl;
	}

	cin.ignore(2);

	return 0;
}

I recommend looking at the documentation files on this website. For example, logical operators for basic and advanced math/calculations:

http://www.cplusplus.com/doc/tutorial/operators/
Yeah, thanks. Usually I'm not this bad, but I'm already falling behind in my class because I can't understand what the instructor says due to language barrier so I had to switch teachers. Plus, my beginners C++ book is ok, but doesn't do a very good job of how you should put 2 and 2 together.
It's all good, we all have to start somewhere. Do you feel you understand how if/else statements are used? Once you have finished your program, submit back as a post and I'll go through it with you, if you would like.

Also, when pasting code, you can use the "Source code" button to display a neater format. It is to the right of the text box underneath "Format:".

Good luck mate.
Thanks Jerseppi. I think I have a greater handle on it. Right now I'm trying to figure out how to tie a Grade letter such as A, B, C, D, and F to the score average. Right now my professor has the box/variable as acount=0, bcount=0, count=0, dcount=0, and fcount=0.

But how would the variable know that fcount=0-59, dcount=60-69, and so forth?

He has given some sample code such as if(score>-90.0) acount=acount+1

But again, how would the code know that an A grade equals 90-100?
Well, you can assign the variable to enumerate by testing the user score. This would be using a few if/else statements. So for each statement, you will be checking the user score against the range for each grade.

I'll give you a simple example, but you should also look at the operators tutorial above to help improve your understanding.

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
	// Declare variables
	int scoreInput, gradeE = 0, gradeF = 0;
	
	// Ask for grade score and read it
	cout << "Please enter grade score: ";
	cin >> scoreInput;
	cout << endl;

	// Condition statement to check through grade scores
	// If scoreInput is "greater than/equal to 0" AND "less than/equal to 59"
	if (scoreInput >= 0 && scoreInput <= 59)
	{
		// Enumerate gradeF by 1
		gradeF = gradeF++;
	}
	// Repeat process for gradeE etc...
	else if (scoreInput >= 60 && scoreInput <= 69)
	{
		// Enumerate gradeE by 1
		gradeE = gradeE++;
	}
	

	// Print counters to user
	cout << "Grade F: " << gradeF;
	cout << "Grade E: " << gradeE;

	cin.ignore(2);

	return 0;
You will also need to look at the different types of "Loops" available to repeat the program, retrieving more than one grade.
Topic archived. No new replies allowed.