Multiple if statements and conditions

This is a discussion on Multiple if statements and conditions within the C Programming forums, part of the General Programming Boards category; I'm writing a program that involves manipulating 3 integer variables (a, b and c). At the moment, I have a ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    26

    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?

    Cheers in advance!

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    74
    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. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    26
    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. #4
    Registered User
    Join Date
    May 2010
    Posts
    74
    can you post more of your code?

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    26
    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!
    Last edited by tomeatworld; 11-06-2010 at 09:04 AM.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    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; }
    Does that solve your problem?

  7. #7
    Registered User
    Join Date
    Nov 2010
    Posts
    26
    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. #8
    Registered User
    Join Date
    Nov 2010
    Posts
    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. #9
    Registered User
    Join Date
    May 2010
    Posts
    2,917
    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. #10
    Registered User
    Join Date
    Nov 2010
    Posts
    26
    Quote Originally Posted by jimblumberg View Post
    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. #11
    Registered User
    Join Date
    May 2010
    Posts
    2,917
    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. #12
    Registered User
    Join Date
    Nov 2010
    Posts
    26
    Ok thanks a lot! I'll work that into what I need. Thanks again!

  13. #13
    Registered User
    Join Date
    May 2010
    Posts
    74
    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. #14
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    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.

    Stuffing in else statements may not help you here....

  15. #15
    Registered User
    Join Date
    Nov 2010
    Posts
    26
    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!

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help on the homework (if statements)
    By Neotriz in forum C++ Programming
    Replies: 3
    Last Post: 10-14-2009, 01:55 AM
  2. newbie question - if statements without conditions
    By c_h in forum C++ Programming
    Replies: 2
    Last Post: 07-18-2008, 11:42 AM
  3. tips for testing conditions
    By Silvercord in forum Game Programming
    Replies: 12
    Last Post: 04-10-2003, 02:42 PM
  4. multi conditional if statements?
    By chaser in forum Game Programming
    Replies: 3
    Last Post: 07-26-2002, 11:52 PM
  5. Winning conditions (Connect 4)
    By Dual-Catfish in forum Game Programming
    Replies: 3
    Last Post: 12-15-2001, 01:53 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21