# If statements

• 03-03-2008
hebali
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); };```
• 03-03-2008
tabstop
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.
• 03-03-2008
hebali
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
• 03-03-2008
tabstop
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?).
• 03-03-2008
Elysia
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.
• 03-03-2008
hebali
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/c-programming/99807-random-number-range-generation.html
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