Solved

Logical Error in Multiple " If " Staments

Posted on 2000-03-03
9
205 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
ID: 2582286
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
ID: 2582900
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
ID: 2583966

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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 13

Author Comment

by:Kelvin_King
ID: 2583998
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
 

Expert Comment

by:jayesh_amin
ID: 2584025

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
ID: 2584392
good good.. thank you, I will try it.
0
 
LVL 13

Author Comment

by:Kelvin_King
ID: 2584400
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 31

Expert Comment

by:Zoppo
ID: 2587171
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
ID: 2589069
That's what I wanted to say with 'unpredictable results' ;-)
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

770 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