What's wrong with my if loop??

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mingke
    New Member
    • Dec 2008
    • 16

    What's wrong with my if loop??

    Hi...

    So I have problem with my if condition..I don't know what's wrong but it keeps resulting the wrong answer....

    So here's the part of my code I have problem with:

    Code:
     for (i=0; i<size2; i++){          
          for (k = 0; k < point3[i]; k++){       
               for (a=0;a<b[i][k];a++){
                  if (90<sudut [i][k][a]<=180){
                    teta[i][k][a]=180-sudut[i][k][a]; 
                  }  
                  else{          
                    if (180<=sudut [i][k][a]<270){
                       teta[i][k][a]=sudut[i][k][a]-180;
                    }
                    else {
                         if (270<=sudut [i][k][a]<360){
                            teta[i][k][a]=360-sudut[i][k][a]; 
                         }
                         else {
                              if (0<=sudut[i][k][a]<90){
                                 teta[i][k][a]=sudut[i][k][a];
                              }
                         }
                    }
                  }        
                  
                  printf ("[%d][%d][%d]=%f\t%f\n",i,k,a,sudut[i][k][a],teta[i][k][a]);
                                               
               }
          }
        }
    and some of the result is:( I edited it for make it easier to see)

    sudut [0][0][0] = 53.157051
    teta [0][0][0] = 126.842949

    sudut [0][0][1] = 206.000000
    teta[0][0][1] = -26.000000

    sudut[0][0][4] =341.555695
    teta[0][0][4] = -161.555695

    sudut [0][1][0] =127.272743
    teta [0][1][0] = 52.727257

    All of the result is wrong, I just show some because it is so many. I'm sorry...


    P.S: if I change the order of the conditions, only the first condition will be result the right answers.

    Could you tell me, where I did wrong?
    Thank you for your help....
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    You can not write a conditional of the form a <= b < c. It doesn't have the meaning you think it is evaluated as (a <= b) < c. Since the output of (a <= b) is 0 or 1 if c > 1 (always in your case) the condition is always true.

    Look at this test program

    Code:
    #include<stdio.h>
    
    int main()
    {
        int i;
    
        for (i=0; i<10; i++)
        {
            printf ("%d: %d %d\n",i, (4<i<=8), (4<i && i<=8));
        }
    
        return 0;
    }
    Also your code would lay out better if your made use of else if rather than else { if ... }

    i.e. this

    Code:
    if (...){
        ...
    }  
    else if (...){
        ...
    }
    else if (...){
        ...
    }
    else if (...){
         ...
    }
    instead of this

    Code:
    if (...){
        ...
    }  
    else{          
        if (...){
            ...
        }
        else {
             if (...){
                ...
             }
             else {
                  if (...){
                     ...
                  }
             }
        }
    }
    P.S. if is not a loop

    Comment

    • mingke
      New Member
      • Dec 2008
      • 16

      #3
      Thank you for correcting my mistake and my codes...

      It's working fine now...

      Comment

      Working...