• C

Logical Error in Multiple " If " Staments

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.

LVL 13
Kelvin_KingAsked:
Who is Participating?
 
jkrConnect With a Mentor Commented:
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
 
Kelvin_KingAuthor Commented:
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
 
jayesh_aminCommented:

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
Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

 
Kelvin_KingAuthor Commented:
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
 
jayesh_aminCommented:

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
 
Kelvin_KingAuthor Commented:
good good.. thank you, I will try it.
0
 
Kelvin_KingAuthor Commented:
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
 
ZoppoCommented:
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
 
jkrCommented:
That's what I wanted to say with 'unpredictable results' ;-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.