# Thread: Multiple if statements and conditions

1. ## Multiple if statements and conditions

I'm writing a program that involves manipulating 3 integer variables (a, b and c). At the moment, I have a few if statements set up for various situations but I'm having trouble with one. When variables are entered into the program, if a and b are equal to zero, a certain if statement should be executed, but also if only a is 0 a different statement should be used. I'm currently having a problem I assume is to do with the two different a = 0 statements. The snippet of my code current looks like:

Code:
```else if(a == 0)
{
statement;
}
else if(a == 0 && b == 0)
{
statement 2;
}```
but on running and having both a and b as zero, the program runs as if only a == 0 and gives an incorrect output. I've tried putting the second 'else if' first but that was unsuccessful and also by putting 'b == 0' before 'a ==0' with the same problem. What am I missing? Is there more validation I require, or a different condition? 2. That's because the first if is going to evaluate to true, and jumps the else if.
You could try having the if (a == 0 && b == 0) first, and then the if (a== 0), changing the order.

or you could,
Code:
```if (a == 0)
{
if (b == 0)
{}
else
{}
}``` 3. I tried using the if within an if, but it still won't work as required. I have:

Code:
```else if(a == 0)
{
if(b == 0 && c != 0)
{}
else if(c ==0 && b != 0)
{}
else if(b ==0 && c == 0)
{}
else
{}```
but that won't work. Anything i'm still missing? 4. can you post more of your code? 5. Sure. The bit including what i'm talking about:

Code:
```        if(DET < 0)
{
printf("The roots of this equation are imaginary!\n");
}
else if(DET == 0)
{
Root1 = (-b)/(a*2);
printf("There is 1 root.\nRoot1 = %lg.\n", Root1);
}
else if(a == 0)
{
if(b == 0 && c != 0)
{
printf("There are no roots as the equation has no x components (x may take any real value).\n");
}
else if(c == 0 && b != 0)
{
printf("There is 1 root.\n Root1 = 0.\n");
}
else if(b == 0 && c ==0)
{
printf("There are no roots as you have no equation! (x may take any real value)\n");
}
else
{
Root1 = (-c)/b;
printf("There is 1 root.\n Root1 = %lg.\n", Root1);
}
}
else
{
if(b < 0)
{
q = -0.5*(b - sqrt (DET));
Root1 = q/a;
Root2 = c/q;
printf("There are 2 roots.\nRoot1 = %lg. Root2 = %lg.\n", Root1, Root2);
}
else
{
q = -0.5*(b + sqrt (DET));
Root1 = q/a;
Root2 = c/q;
printf("There are 2 roots.\nRoot1 = %lg. Root2 = %lg.\n", Root1, Root2);
}
}```
Can provide more of the code if required! 6. Ok... you need to pass an error message if any one of the three is 0...
I don't think it needs to be that complex...

Code:
```if ((a == 0) || (b == 0) || (c == 0))
{ printf("Don't enter zeros!\n");
return 0; }``` 7. I'm afraid not. In the cases where some are 0, the function can still work.
For example, if only c is zero a result is still possible. 8. i would try starting with an if ((a == 0) || (b == 0) || (c == 0)), the drop down to a if ((a == 0) || (b == 0)) so on just narrowing down the options. might work. 9. I think where you are getting confused is with all of the if else clauses. You should rework your logic without using the if else. It will make your logic a lot clearer.

Also you should always keep your terms in the same order.

Code:
```   if((a == 0) && (b != 0))
{
}
else if((a != 0) && (b ==0))
{
}```
Jim 10. Originally Posted by jimblumberg I think where you are getting confused is with all of the if else clauses. You should rework your logic without using the if else. It will make your logic a lot clearer.
Thanks, but where abouts do you mean? In the integrated if function (inside "if(a == 0)")? 11. Code:
```	if(b == 0 && c != 0)
{
printf("There are no roots as the equation has no x components (x may take any real value).\n");
}
else if(c == 0 && b != 0)
{
printf("There is 1 root.\n Root1 = 0.\n");
}
else if(b == 0 && c ==0)
{
printf("There are no roots as you have no equation! (x may take any real value)\n");
}
else
{
Root1 = (-c)/b;
printf("There is 1 root.\n Root1 = %lg.\n", Root1);
}```
In this section for starters.
To something like this:
Code:
```if(b == 0)
{
if(c == 0)
{
printf("There are no roots as you have no equation! (x may take any real value)\n");
}
else
{
Root1 = (-c)/b;
printf("There is 1 root.\n Root1 = %lg.\n", Root1);
}
}
else
{
if(c == 0)
{
printf("There are no roots as you have no equation! (x may take any real value)\n");
}
else
{
printf("There are no roots as the equation has no x components (x may take any real value).\n");
}
}```
Please note: This logic might not be exactly what you want but shows my meaning.

Jim 12. Ok thanks a lot! I'll work that into what I need. Thanks again! 13. I can't see a problem there, but you've got alot of if else statements there. When one of them evaluates to true, the rest is bypassed, so maybe that's where the problem occurs.

For example if DET is less than 0, the first printf is printed, but everything else isn't going to be evaluated, so if you still wanted to test the a and b, it wouldn't.

If that isn't the problem, then maybe more code?  14. Actually, given the complexity of the situation, you might be better to work with multiple sequential ifs, instead of trying to nest them...

Test for a == 0 and deal with that
Then test for b == 0 and deal with that
Finally test for c ==0 and deal with that. 15. Changed my code to:
Code:
```else if(a == 0)
{
if(b == 0)
{
/* If a, b and c are 0, no equation */
if(c == 0)
{
printf("There are no roots as you have no equation! (x may take any real value)\n");
}
/* If a and b are 0, no root */
if(c != 0)
{
printf("There are no roots as the equation has no x components (x may take any real value).\n");
}
}
if(b != 0)
{
/* If a and c are 0, x must be 0 */
if(c == 0)
{
printf("There is 1 root.\n Root1 = 0.\n");
}
/* For only a is 0, simple rearranging gives x */
if(c != 0)
{
Root1 = (-c)/b;
printf("There is 1 root.\n Root1 = %lg.\n", Root1);
}
}

}```
which still doesn't work properly. Not sure why, but I'm going to look at it again another time. Thanks for all the help so far and any more pointers are much appreciated! Popular pages Recent additions 