Greetings,
The if-else statement is normally used to express decisions, where the else part is usually optional. Since an if simply tests the numeric value of an expression, coding shortcuts are possible:
if (expression)
// instead of
if (expression != 0)
Example 1.1: Using coding shortcuts
There is an ambiguity when an else is omitted from a nestedif sequence, since the else part of an if-else is optional.
if (a < 0)
if (b < c)
r = b;
else
r = c;
Example 1.2: Ambiguity between control flow statements
The construction of theif-else syntax is simple:
if (expression)
statement 1
else
statement 2
Theelse-if construction is similar, though it evaluates all expressions in order; and if any expression is true, the statement associated with it is executed and terminates the whole chain:
if (expression)
statment
else if (expression)
statement
else if (expression)
statement
else
statement
Example 1.3: Using the Else-If syntax
So to do accomplish your task, you could do something like the following:
if (total >= 80 && total <= 100) // 80 between 100
statement;
else if (total >= 50 && total <= 79) // 50 between 79
statement;
else if (total >= 45 && total <= 49) // 45 between 49
statement;
else // probably an F grade here
statement;
Code 1.1: Using if-else in application
If you have multiple statements withing oneif, you must group the declarations together into a compund statement using braces ({ }):
if (expression) { // multi-computation
statement 1;
statement 2;
}else // single computation
statement 3;
Example 1.4: Grouping declarations
I hope this helps, and if you have any further questions please feel free to ask.
-Stack Overflow