Nested if comparing

so The problem is to input 5 numbers and then it will display the one with smallest value

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  if(A<B)
        if(A<C)
               if(A<D)
                      if(A<E)
                             cout<<"\n A is smallest";
  else
       if(B<C)
              if(B<D)
                     if(B<E)
                             cout<<"\n B is smallest";
       else
              if(C<D)
                     if(C<E)
                            cout<<"\n C is smallest";
              else
                     if(D<E)
                            cout<<"\n D is smallest";
                     else
                            cout<<"\n E is smallest";


So the thing was i thought it was working. I inputted 1 for A, 2 for B, 3 for C
4 for D and 5 for E. And the program displayed that A is smallest as I expected.
But when you make B the smallest one, it wont display anything. Same with C,D, and E. A is the only one working. Please help. I'm new at this
cause here else sets with nearest if
So, how do i troubleshoot this? is there any error in my codes?
Use braces to seperate if statements
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
if(A<B)
{
        if(A<C)
               if(A<D)
                      if(A<E)
                             cout<<"\n A is smallest";
}
  else
  {
       if(B<C)
              if(B<D)
                     if(B<E)
                             cout<<"\n B is smallest";
   }
       else
       {
              if(C<D)
                     if(C<E)
                            cout<<"\n C is smallest";
        }
              else
              {
                     if(D<E)
                            cout<<"\n D is smallest";
                     else
                            cout<<"\n E is smallest";
               }


Like this? I did that and It said the position of else is wrong
Last edited on
you need to use else...if satement
cant use multiple else statements
when you omit braces, the if/else statements will only apply to the first line. This is a pretty messy layout, better if you combine statements (using &&) like so.

1
2
3
4
5
6
7
8
9
10
if (A<B && A<C && A<D && A<E) // only if all 4 statements are true
    cout<<"\n A is smallest";
else if (B<C && B<D && B<E)
    cout<<"\n B is smallest";
else if (C<D && C<E)
    cout<<"\n C is smallest";
else if (D<E)
    cout<<"\n D is smallest";
else
    cout<<"\n E is smallest";


There is still a flaw in that code though, say A-E were 10,10,10,10 and 20 respectively, it will say D is the smallest because none of the first 3 if statements were true. Maybe a minor detail though.
Last edited on
So should i redo the whole thing? thanks for replying btw
Thanks man, I appreaciate it
is there any error in my codes?

There are no syntax errors in your code (i.e. it's valid C++).

But it isn't doing what you require, so it's got logic errors. In this case, you're missing a lot of cases (if you want to stick to the nested if tests, that is.)

So, how do i troubleshoot this?

You could add output for all the elses, so you can see what's going on with your logic.

But...

As well as being a lot tidier, manudude03's version is using operator&& which causes the less-than conditions to be evaluated in groups, e.g.

if (A<B && A<C && A<D && A<E)

is testing that A is less than B, C, D, and E all at the same time. But your style of code does the tests one by one and must therefore handle every single, possible permutation independently. But you're missing several cases, which is why the logic doesn't work out.

Andy

PS To get the nested if tests to work as well as manudude03's code, you need:

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
    if(A<B)
        if(A<C)
            if(A<D)
                if(A<E)
                    cout<<"\n A is smallest";
                else                           // EXTRA
                    cout<<"\n E is smallest";  // EXTRA
            else                               // EXTRA
                if(D<E)                        // EXTRA
                    cout<<"\n D is smallest";  // EXTRA
                else                           // EXTRA
                    cout<<"\n E is smallest";  // EXTRA
        else                                   // EXTRA
            if(C<D)                            // EXTRA
                if(C<E)                        // EXTRA
                    cout<<"\n C is smallest";  // EXTRA
                else                           // EXTRA
                    cout<<"\n E is smallest";  // EXTRA
            else                               // EXTRA
                if(D<E)                        // EXTRA
                    cout<<"\n D is smallest";  // EXTRA
                else                           // EXTRA
                    cout<<"\n E is smallest";  // EXTRA
    else
        if(B<C)
            if(B<D)
                if(B<E)
                    cout<<"\n B is smallest";
                else                           // EXTRA
                    cout<<"\n E is smallest";  // EXTRA
            else                               // EXTRA
                if(D<E)                        // EXTRA
                    cout<<"\n D is smallest";  // EXTRA
                else                           // EXTRA
                    cout<<"\n E is smallest";  // EXTRA
        else
            if(C<D)
                if(C<E)
                    cout<<"\n C is smallest";
                else                           // EXTRA
                    cout<<"\n E is smallest";  // EXTRA
            else
                if(D<E)
                    cout<<"\n D is smallest";
                else
                    cout<<"\n E is smallest";
Last edited on
andywestken, Thanks man, I'll keep everything in mind. Nested is not that complicted after all. Thanks again guys I learned a lot :))
Topic archived. No new replies allowed.