 # Nested If problem

Hi im trying to create a program that will pick the second largest number and display it on serial monitor i hard code. However i could not make it work. so if anyone has an idea on what i did wrong please help. the code below. P.S without using &&

int a=3;
int b=2;
int c=1;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}

void loop() {
// put your main code here, to run repeatedly:

if (a<b)
{ if (a>c)

Serial.println(“the second largest value is”);
Serial.println(a,DEC);
}
else if (b<a){

if (c>a)
Serial.println(“the second largest value is”);
Serial.println(a,DEC);
}
else if (b<a)
{
if (c>b)
Serial.println(“the second largest value is”);Serial.println(b,DEC);
}
else if (a<b){

if (c>b)
Serial.println(“the second largest value is”);Serial.println(b,DEC);
}
else if (c<a){

if (c>b)
Serial.println(“the second largest value is”);Serial.println(c,DEC);
}
else if (a<c){
if (b>c)
Serial.println(“the second largest value is”);Serial.println(c,DEC);
}
}

Why no &&?

Delta_G: Why no &&?

Probably because the homework assignment said so. ;)

Hi im trying to create a program that will pick the second largest number and display it on serial monitor

That would be 32766. MAX_INT - 1.

I’m not sure the second else statement will work right. I have a feeling it should just be an “if” at that point since you are asking the same question as the previous else. The problem is that if B<A then i’m not sure the second else will even run since it’s not the opposite of the first else. Will the pointer only run that second else if the first one is false? Or will it test that second else even when the first one proves true?

Regardless, if you remove the second else, the boolean logic holds because the first “else if” and the second “else if” are asking the same question so both secondary “ifs” under each of these can be combined into one “else if B<A” - then ask the two secondary tests.

I’m also not sure why there should be no &&s in the project. Unless it’s to let you get used to the pitfalls of elses

Just start by listing the combinations
A < B < C
A < C < B
B < A < C
B < C < A
C < A < B
C < B < A
There are 6 total. I started randomly with if(a > b), notice how 3/6 cases show A > B
B < A < C
B < C < A
C < B < A
and the others do not
A < B < C
A < C < B
C < A < B
Notice how asking 1 question narrowed our search to 2 cases of 3 possibilities. Lets expand our first case.
How do we differentiate between the 3 cases? I chose to ask if(a < c) because there is only 1 case where that is true.
B < A < C
Then I am left with
B < C < A
C < B < A
So I simply asked if(b > c) because it differentiates the 2 cases. If I had asked something like if(b < a), this does not get us anywhere because in both cases this is true. By asking questions about how each relate to each other we can narrow our search down to 1 choice. Back to our correct if statement “if(b > c)”. If this is true, then the result MUST be
C < B < A
and if this comparison is false, then the only remaining case is
B < C < A
This is how I differentiated the first half of all the possibilities,

``````if(a > b){
if(a < c){
//a is 2nd highest (bac)
}
else if(b > c){
//b is 2nd highest (cba)
}
else {
//c is 2nd highest (bca)
}
}
else{
//I will let you do the second half of the cases here, good luck
}
``````

And can you have two lines (both defined with semicolons) after an if statement without braces? I thought it was just one. If not then the values of the results will not be printed.

That is correct, only the first line will be executed BECAUSE of the if statement, the 2nd, 3rd, etc lines will be executed regardless of if the if statement is true or not

I supposed the reference needs to be updated then to include this detail, which if you sit and think about it makes sense, since at that point it's just another consecutive line in the code, but when sub nested in "else if's" it does tend to look like it will only execute through some logic test.

For this example and for the OPs question whether the second line and third lines execute will depend on the "if else" test that surrounds the nested if's

How about putting the values in an array and sorting high to low it then printing the second entry ?

How about using code tags? How about putting one statement on one line?

How about putting the values in an array and sorting high to low it then printing the second entry ?

I thought of that one too but when you get down into the libs &&s are being used so I thought that might be cheating given the parameters.

Why do you need a lib to do a bubble sort ?

You could use the keyword "and" instead of "&&". That would make their head spin. :)

!(a||b)

``````if (a > c)
Serial.println("the second largest value is");
Serial.println(a, DEC);
``````

If you formatted your code correctly (use Ctrl+T) you would see that you are missing some braces.