Solved

Logical Error in Multiple " If " Staments

Posted on 2000-03-03
9
203 Views
Last Modified: 2010-04-15
I'm programming with Borland C 2.0 Dos version.
The part of my code that i'm having problems with is this :
if(adc_output==0)      //various light setting

      {
      gotoxy(5,12);
      printf("It's day time, Lights Off");
      }
      if(10<adc_output<80)
      {
      gotoxy(5,12);
      printf("Dimmer Lights on         ");
      }

      if(81 < adc_output <170)
      {
      gotoxy(5,12);
      printf("Moderate Lighting        ");
      }
      if(170 < adc_output)
      {
      gotoxy(5,12);
      printf("Full Brightness"          );
      }

      }
When I compile, of course there are no errors, but the error is logical.
What I have done before is get the input form an adc, and stored it into a variable called adc_output which has a value from 0 to 255. I'm comparing the valule with all the various conditions above, but instead of printing out the statement I want eg(10<adc_output<70)
it prints out all the 5 statements.
Almost as if the program cannot read the "if" statements, and cannot make a comparison. After adjusting it code a little, ( I dont really know what I adjusted ), the program seemed to be able to display the correct setting for
if(81 < adc_output <170)
      {
      gotoxy(5,12);
      printf("Moderate Lighting        ");
      }
      if(170 < adc_output)
      {
      gotoxy(5,12);
      printf("Full Brightness"          );
Only. I'm my input was reduced to below 80, it still could'nt detect the other conditions, and kept printing
" moderate lighting"

Once again, there are no errors like brackets not being closed etc.

0
Comment
Question by:Kelvin_King
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 150 total points
Comment Utility
You're mixing up C with 'everyday logic' - an expression like

if(81 < adc_output <170)

evaluates to unpredictable results, though it's valid C code.. change it to read

while ( 1)
{
if(adc_output==0)      //various light setting

{
gotoxy(5,12);
printf("It's day time, Lights Off");
break;
}
if(10<adc_output && adc_output<80)
{
gotoxy(5,12);
printf("Dimmer Lights on         ");
break;
}

if(81 < adc_output && adc_output<170)
{
gotoxy(5,12);
printf("Moderate Lighting        ");
break;
}
if(170 < adc_output)
{
gotoxy(5,12);
printf("Full Brightness"          );
break;
}

}
}

BTW: The 'while' pseudo-loop just ensures that the other statements aren't evaluated when a match is found, just to prevent multiple matches....


Fel free to ask if you need more information or encounter problems!
0
 
LVL 13

Author Comment

by:Kelvin_King
Comment Utility
I'll try it out in the lab to see if it works. btw, Do I need the while() loop ?
because thoes conditions are already inside another if statement
so I dont want to clutter up my code.
0
 

Expert Comment

by:jayesh_amin
Comment Utility

Here is the structure you REALLY want:

/**** Begin code ******/
gotoxy(5,12);
if(ADC < 10)
  printf("Scared of darkness? \n");
else if (ADC < 80)
  printf("Good morning \n");
else if (ADC < 170)
  printf("Hmmm that's better \n");
else
  printf("Whoa...easy big fella \n");
/***** end code ********/

Now there...happy? :) :)
I would strongly recommend getting a
beginner's book and polishing these
basic structures.

As mentioned by jkr, (80<adc<170),
is not what you think it is!! but it
does not evaluate to unpredictable results either.. :)
something like 80 < ADC < 170 will
always evaluate to 1 (true) if the
compiler is left associative, which I
think any ANSI compiant one should be
(0 (false) if it is right associative).
Let me know if you want further
explaination....

Good luck!
Jayesh

0
 
LVL 13

Author Comment

by:Kelvin_King
Comment Utility
Jayesh amin>>
let me just check something with you
let's say my ADC_output is 8
the code will take
if(ADC < 10) - TRUE
  printf("Scared of darkness? \n");
else if (ADC < 80) - TRUE
  printf("Good morning \n");
else if (ADC < 170) - TRUE
  printf("Hmmm that's better \n");


//the result ALL statements will be printed out.

Hence, for ANY value less than 170
it will printf("Hmmm that's better \n");
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Expert Comment

by:jayesh_amin
Comment Utility

Hi Kevin,

when the if condition is true, the
else statements are not evaluated.

Here is the flow (I will add the
brackets for clarity this time, though
not required in the code snippet):

if (x<10)
{  
   printf("x<10 \n");
}
else // block evaluated only if x>=10
{   if (x<80)
    {
        printf("x>=10 AND x<80 \n");
    }
    else // evaluated only if x>=80
    {
        if (x<170)
        {
          printf("x>=80 AND x<170 \n");
         }
        else
         {printf("x>=170 \n");}
    }
}


Now remove all the brackets and rearange
the white-space, you will get back to my
earlier code. About the brackets, if you
have only one statement in a block, you do not need brackets. e.g.

if (cond1)
   printf("cond1 is true \n");
else
   printf("cond1 is false \n");

OR

for(i=1,sum=0;i<=10;i++)
   sum += i; /* to add 1+2...+10 */

and so on...
Please just try the code and I assure
you, it will do what you want.... :)

Good luck!
Jayesh
     

0
 
LVL 13

Author Comment

by:Kelvin_King
Comment Utility
good good.. thank you, I will try it.
0
 
LVL 13

Author Comment

by:Kelvin_King
Comment Utility
OHHH
Ok let me understand what you were saying eariler:
example
if(10< X <100)
do stuff;

the compiler will only care about the 10 < X part, and ignore the X<100 part
Something like jumping the gun.
too anxious to carry out the tasks..
that's the meaning of left associative.
0
 
LVL 30

Expert Comment

by:Zoppo
Comment Utility
Hi Kelvin_King,

>>the compiler will only care about the 10 < X part, and ignore the X<100 part

FYI, that's not correct, too:

the compiler evaluates the whole expression regular like i.e. this:

10 < x < 100             is evaluated as
(10 < x) < 100           and if 10 < x this results in
1 < 100                    for each x > 10
or
0 < 100                    for each x <= 10, so both TRUE

if you wouold write i.e.:

10 < ( x < 100 )         is evaluated as
10 < 1                      for each x < 100
or
10 < 0                      for each x >= 100, so both FALSE

hope that helps,

ZOPPO
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
That's what I wanted to say with 'unpredictable results' ;-)
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now