Thread: If statements

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    84

    If statements

    I'm having some trouble figuring out if statements, they've been very finicky. Can anyone detect the problem with this? Or, alternately, can anyone say whether the if if-else else method I am using is a good one, or if I should switch to something else (no pun intented).

    Thanks

    Code:
    		/* X-Axis Groups */
    		float VerSet_r_A; /*X Dimension of Vertices 0,2,4,6 */
    		float VerSet_r_D; /*X Dimension of Vertices 1,3,5,7 */
    		
    		/* Y-Axis Groups */
    		float VerSet_r_B; /*Y Dimension of Vertices 0,1,6,7 */
    		float VerSet_r_E; /*Y Dimension of Vertices 2,3,4,5 */
    		
    		/* Z-Axis Groups */
    		float VerSet_r_C; /*Z Dimension of Vertices 0,1,2,3 */
    		float VerSet_r_F; /*Z Dimension of Vertices 4,5,6,7 */
    		
    		int C_closer; // Value 1 = C is closer. Value 0 = F is closer.
    		int A_right; // Value 1 = A is right. Value 0 = D is right.
    		int B_up; // Value 1 = B is up. Value 0 = E is up.
    		
    		int Config_Case; // 0 = P, 1 = S, 2 = R, 3 = Q
    
    int main (void) {
    		if(VerSet_r_C > 0)
    			{
    				printf("Real_Cube is out of camera view. Real_Z value must be less than zero. \n");
    				return(0);
    			};
    			
    		if(VerSet_r_F > 0)
    			{
    				printf("Real_Cube is out of camera view. Real_Z value must be less than zero. \n");
    				return(0);
    			};
    			
    		if(VerSet_r_C > VerSet_r_F)
    			C_closer = 1; // true	
    		else
    			C_closer = 0; // false
    
    		if(VerSet_r_A > VerSet_r_D)
    			A_right = 1; // true	
    		else 
    			A_right = 0; // false	
    			
    		if(VerSet_r_B > VerSet_r_E)
    			B_up = 1;	// true
    		else 
    			B_up = 0;	// false
    			
    		if(C_closer == 1) {
    			if(A_right == 0) {
    				if(B_up == 0) {
    					Config_Case = 0;
    				}
    				else 
    				printf("Real_Cube Case orientation check error.");
    				return(0);
    			}
    			else if(A_right == 1) {
    				if(B_up == 1) {
    					Config_Case = 1;
    				}
    				else 
    				printf("Real_Cube Case orientation check error.");
    				return(0);
    			}
    			else
    				printf("Real_Cube Case orientation check error.");
    				return(0);
    		}
    		else if(C_closer == 0) {
    			if(A_right == 0) {
    				if(B_up == 1) {
    					Config_Case = 2;
    				}
    				else 
    				printf("Real_Cube Case orientation check error.");
    				return(0);
    			}
    			else if(A_right == 1) {
    				if(B_up == 0) {
    					Config_Case = 3;
    				}
    				else 
    				printf("Real_Cube Case orientation check error.");
    				return(0);
    			}
    			else
    				printf("Real_Cube Case orientation check error.");
    				return(0);			
    		}
    /* For variable "Config_Case": Integer Values: Case-P = 0, Case-S = 1, Case-R = 2, Case-Q = 3 */
    /* End Real_Cube Orientation Determination */			
    
    /* Main generation procedure? */
    
    //For now, we are going to deal only with Case-P, and fill the others in later.
    		printf("Case is: %i \n",Config_Case);
    
    if(Config_Case = 0) {
    	printf("okay, we made it this far. \n");
    }
    else
    	printf("We can only deal with Case-P Real_Cube orientations at the moment. \n");
    	return(0);
    
    /* END OF PROGRAM. */
    return(0);
    };

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Notes: This
    Code:
    if(VerSet_r_C > VerSet_r_F)
    			C_closer = 1; // true	
    		else
    			C_closer = 0; // false
    is the same as
    Code:
    C_closer = (VerSet_r_C > VerSet_r_F);
    The boolean expression on the right will always return 0 or 1.

    Similarly, I wouldn't test
    Code:
    if (C_closer == 1)
    but just
    Code:
    if (C_closer)
    (it "says what you mean"), and certainly you don't need to test it again in the else-clause. (If you're there, you know C_closer is 0.)

    You can maybe get away with some condensing on the inner loops -- for example, if C_closer is 1, then what we really care about is that A_right and B_up are the same; I might write that part something like this:
    Code:
    .
    .
    .
    if (C_closer) {
        if (A_right != B_up) {  /* axes not positioned correctly */
            printf("Real_Cube Case orientation check error.");
            return(0);
        }
        /* if we're here, we're correct, so let's cheat a little */
        Config_Case = A_right;
    }
    .
    .
    .
    but as noted that's cheating.

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    84
    Thanks.

    What do I do in cases where C_closer is false?
    Code:
    if (C_closer == 1)
    Code:
    if (C_closer)
    something to replace:
    Code:
    if (C_closer == 0)
    in a direct way.

    thanks

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Normally if C_closer has boolean semantics (that is, you're thinking of it as a true-false expression), then the obvious way to do it is
    Code:
    if (!C_closer)
    Of course, here you shouldn't test C_closer is 0 at all (at least not in the snippet you posted), since when you get to the else clause C_closer has to be 0 (why test for something you know is true?).

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I should note that you should indent better here or pay attention:

    Code:
    		else if(C_closer == 0) {
    			if(A_right == 0) {
    				if(B_up == 1) {
    					Config_Case = 2;
    				}
    				else 
    					printf("Real_Cube Case orientation check error.");
    				return(0);
    			}
    			else if(A_right == 1) {
    				if(B_up == 0) {
    					Config_Case = 3;
    				}
    				else 
    					printf("Real_Cube Case orientation check error.");
    				return(0);
    			}
    			else
    				printf("Real_Cube Case orientation check error.");
    			return(0);
    		}
    I marked the lines I changed in red. This is because, as you can see, the else only executes the statement below, contrary to what you might believe if you indent both lines.
    As we can see, whether or not B_up is true (among others), you do a return 0, which most likely you do not want.
    If you look closely at your source, and indent properly, you'll see that whether or not C_closer is 1 or 0, the function will return 0.

    You can also argue this is you want, but with this many nested IFs, I find it easier the put the start { at a row of its own:
    Code:
    		if(C_closer == 1)
    		{
    			if(A_right == 0)
    			{
    				if(B_up == 0)
    				{
    					Config_Case = 0;
    				}
    				else 
    					printf("Real_Cube Case orientation check error.");
    				return(0);
    			}
    			else if(A_right == 1)
    			{
    				if(B_up == 1)
    				{
    					Config_Case = 1;
    				}
    				else 
    					printf("Real_Cube Case orientation check error.");
    				return(0);
    			}
    			else
    				printf("Real_Cube Case orientation check error.");
    			return(0);
    		}
    		else if(C_closer == 0)
    		{
    			if(A_right == 0)
    			{
    				if(B_up == 1)
    				{
    					Config_Case = 2;
    				}
    				else 
    					printf("Real_Cube Case orientation check error.");
    				return(0);
    			}
    			else if(A_right == 1)
    			{
    				if(B_up == 0)
    				{
    					Config_Case = 3;
    				}
    				else 
    					printf("Real_Cube Case orientation check error.");
    				return(0);
    			}
    			else
    				printf("Real_Cube Case orientation check error.");
    			return(0);			
    		}
    This is much easier to read, to see where each if starts and ends. You don't have to do it like this, it's up to you, but it is something I would recommend for better readability.
    Last edited by Elysia; 03-03-2008 at 02:33 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Feb 2008
    Posts
    84
    Hey, thanks. I'm loosing my mind though... I can't figure out what is going on here.
    My code was running last night, as was another program referenced at: http://cboard.cprogramming.com/showthread.php?t=99807
    But now, each of them builds. But when I run, it goes straight to logout.

    What is that about? Can there be some outside force? Tried restarting the computer. working on xcode on os 10.5.2

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  2. newbie question - if statements without conditions
    By c_h in forum C++ Programming
    Replies: 2
    Last Post: 07-18-2008, 10:42 AM
  3. C++ If Statements Help
    By moporho in forum C++ Programming
    Replies: 19
    Last Post: 01-18-2008, 08:40 AM
  4. Efficiency of case statements
    By Yasir_Malik in forum C Programming
    Replies: 26
    Last Post: 05-23-2006, 11:36 AM
  5. Switch statements for strings
    By cxs00u in forum C++ Programming
    Replies: 5
    Last Post: 04-17-2002, 03:38 PM