473,396 Members | 1,743 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

What's wrong with my if loop??

16
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:

Expand|Select|Wrap|Line Numbers
  1.  
  2.  for (i=0; i<size2; i++){          
  3.       for (k = 0; k < point3[i]; k++){       
  4.            for (a=0;a<b[i][k];a++){
  5.               if (90<sudut [i][k][a]<=180){
  6.                 teta[i][k][a]=180-sudut[i][k][a]; 
  7.               }  
  8.               else{          
  9.                 if (180<=sudut [i][k][a]<270){
  10.                    teta[i][k][a]=sudut[i][k][a]-180;
  11.                 }
  12.                 else {
  13.                      if (270<=sudut [i][k][a]<360){
  14.                         teta[i][k][a]=360-sudut[i][k][a]; 
  15.                      }
  16.                      else {
  17.                           if (0<=sudut[i][k][a]<90){
  18.                              teta[i][k][a]=sudut[i][k][a];
  19.                           }
  20.                      }
  21.                 }
  22.               }        
  23.  
  24.               printf ("[%d][%d][%d]=%f\t%f\n",i,k,a,sudut[i][k][a],teta[i][k][a]);
  25.  
  26.            }
  27.       }
  28.     } 
  29.  
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....
May 6 '09 #1
2 2205
Banfa
9,065 Expert Mod 8TB
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

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2.  
  3. int main()
  4. {
  5.     int i;
  6.  
  7.     for (i=0; i<10; i++)
  8.     {
  9.         printf ("%d: %d %d\n",i, (4<i<=8), (4<i && i<=8));
  10.     }
  11.  
  12.     return 0;
  13. }
  14.  
Also your code would lay out better if your made use of else if rather than else { if ... }

i.e. this

Expand|Select|Wrap|Line Numbers
  1. if (...){
  2.     ...
  3. }  
  4. else if (...){
  5.     ...
  6. }
  7. else if (...){
  8.     ...
  9. }
  10. else if (...){
  11.      ...
  12. }        
  13.  
instead of this

Expand|Select|Wrap|Line Numbers
  1. if (...){
  2.     ...
  3. }  
  4. else{          
  5.     if (...){
  6.         ...
  7.     }
  8.     else {
  9.          if (...){
  10.             ...
  11.          }
  12.          else {
  13.               if (...){
  14.                  ...
  15.               }
  16.          }
  17.     }
  18. }        
  19.  
P.S. if is not a loop
May 6 '09 #2
mingke
16
Thank you for correcting my mistake and my codes...

It's working fine now...
May 6 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

226
by: Stephen C. Waterbury | last post by:
This seems like it ought to work, according to the description of reduce(), but it doesn't. Is this a bug, or am I missing something? Python 2.3.2 (#1, Oct 20 2003, 01:04:35) on linux2 Type...
51
by: WindAndWaves | last post by:
Can anyone tell me what is wrong with the goto command. I noticed it is one of those NEVER USE. I can understand that it may lead to confusing code, but I often use it like this: is this...
6
by: Niklaus | last post by:
Hi, Can someone point out what is wrong with this code ? How can i make it better optimize it. When run it gives me seg fault in linux. But windows it works fine(runs for a long time). Do we...
5
by: ken | last post by:
Hi, I have two questions the first is: in the example below how can I call an event from within a statement, such as replace Stop1 with cmdStop1 which is a button on my form? My second question...
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
30
by: Bill Reid | last post by:
#define MAX_VALUES 64 typedef struct { unsigned value_1; double value_2; double value_3; double value_4; } VALUES; typedef struct {
16
by: chutsu | last post by:
Ok Here is a problem, I got a imaginary database program that I need to code, to add a patient I have function inser_patient. but when I try to input the details it doesn't quite work the way I...
14
by: Mohamed Mansour | last post by:
Hey there, this will be somewhat a long post, but any response is appreciated! I have done many PInvoke in the past from C++ to C#, but I did PInvoke within C# not C++/CLI. Can someone explain...
10
by: DavidSeck.com | last post by:
Hi, I am working with the Facebook API right now, an I have kind of a problem, but I don't know what I am doing wrong. So I have a few arrays, f.ex.: User albums: array(2) {
16
by: raylopez99 | last post by:
I am running out of printing paper trying to debug this...it has to be trivial, but I cannot figure it out--can you? Why am I not printing text, but just the initial string "howdy"? On the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.