If Statements With Multiple conditions

Ok, I'm working on code for a project, and I need to know how to use the if statements for if/else if/else with multiple conditions. This is the info I have to code:
>= 89.5 A

>= 89.5 A

79.5 – 89.49 B

69.5 – 79.49 C

59.5 – 69.49 D

<= 59.49 F

this is the code that I have is there something wrong?
Syntax:
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
if(avg_grade >= 89.5)
    {
        cout<<"Grade     =       A"<<endl;
    }
            if(avg_grade <79.5 >=89.5)
            {
                cout<<"Grade     =       B"<<endl;
            }
                else
                {
                    if(avg_grade <69.5 >=79.49)
                    {
                        cout<<"Grade     =       C"<<endl;
                    }
                }
                    else
                    {
                        if(avg_grade <59.5 >= 69.49)
                        {
                            cout<<"Grade     =       D"<<endl;
                        }
                    }
                         else
                            {
                                if(avg_grade <= 59.49)
                                {
                                    cout<<"Grade     =       F"<<endl;
                                }
                            }


The code works, but once I get to the third condition they don't execute the court command. They just repeat the command from the second condition. Thanks in advance for any help.
Last edited on
yes, math does not work in code, you must be TOTALLY explicit.

if(avg_grade <79.5 >=89.5)
should be
if(avg_grade <79.5 || avg_grade >=89.5)

however this is just a fix to your SYNTAX.

logically, you want to work in absolutes (doing less tests, each test costs time in the CPU and bloats the code).

so thinking about this..
if its < 59.5 its an f.
if its < 69.5, its a d, and you already handled f.
that^^ is what the ELSE word is for.
so..
1
2
3
4
5
6
if(avg_grade < 59.5)
  cout... etc
else if(avg < 69.5)
  cout ...
else if(... and so on

see how stringing them carefully avoids having to test both ends, because you already knew some of that info and can exploit your understanding of the values to do less work? The final test does not even need a condition, its just else cout A
Last edited on
Duplicated: http://www.cplusplus.com/forum/general/282093/

PLEASE learn to use code tags, they make reading and commenting on source code MUCH easier.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

HINT: you can edit your post and add code tags.

Some formatting & indentation would not hurt either
Last edited on
Topic archived. No new replies allowed.