if, else problem

I am trying to get the LCD to print 3 decimals if the answer is below 1, but if answer is 1 or greater, then I would like only 1 decimal to be printed. i.e.

if < 1; print .xxx; else if >=1; print y.x

(this ver 0018 float thingy for decimals is really nice, and works!

From the reference for the if, else I find the following example:

if (pinFiveInput < 500)
{
// action A
}
else
{
// action B
}

I think I have the same thing here:

if (amp < 1.0);
{
  lcd.setCursor(8,0);
  lcd.print("A=");
  lcd.print(amp, 3);
}

else 
{
  amp >= 1.0;
  lcd.setCursor(8,0);
  lcd.print("A=");
  lcd.print(amp, 1);
}

but I get an error msg saying:

In function 'void loop()':
error: 'else' without a previous 'if

What am I missing? Why does the else not see the if statement above? I even tried writing the else as:

else (amp >= 1.0);
{
  lcd.setCursor(8,0);
  lcd.print("A=");
  lcd.print(amp, 1);
}

I have used if, else in the past:

if (amp < .01)
  {amp = 0;}
  else (amp >= .01);
  amp = (amp/.0405);

Is it because I'm using the lcd.print statements as the if, else?

Thank ya'll for any help and suggestions.

Ken H>

Could the semicolon you have in the if line be the problem?

if (amp < 1.0)[glow];[/glow]
{
  lcd.setCursor(8,0);
  lcd.print("A=");
  lcd.print(amp, 3);
}

else
{
  amp >= 1.0;
  lcd.setCursor(8,0);
  lcd.print("A=");
  lcd.print(amp, 1);
}

Lefty

Thanks Lefty - I NEED to open my eyes!

works just fine now.

Ken H>

I NEED to open my eyes!

Nothing wrong with your eyes. I call such errors Brain Farts (tm) and we all do them at times and sometimes they can smell real bad. :wink:

Lefty

That might be related to why C coders adopt a "style" to write these "blocks" of code this way...

if (amp < 1.0){
  lcd.setCursor(8,0);
  lcd.print("A=");
  lcd.print(amp, 3);
  }

...opening the brace at the end of the line... even though it seems "cleaner and clearer" to do it the way you did...

Willard, you are correct that seems to be the way "real coders" format. I am very new at this and for some reason it just seems to call my attention to the brackets better if I set opening and closing brackets on separate lines - they don't get lost as much for me.

It just now clicked maybe.... the "proper" way all closing brackets on a separate line, with the opening bracket at end of line? Maybe that does make more sense for long code.... is that the reason? Right now my code is usually pretty short - only a page or two long.

Thanks for the insight.

Ken H>

I prefer opening and closing curly braces on lines by themselves. It makes it easier, for me, to recognize a block of code, and to see that there are the correct number of { and }.

When I use an if statement, I type:

if(this == that)
{
}

Then, I go back and put code in the block. If you get in the habit of typing , {, , }, you'll never again make the mistake of adding a ; to the end of an if, for, or while statement.

Just my 2 cents...

I only open my eggs from the big end! :slight_smile:

Chevy/Ford
Synthetic oil/non-synthetic oil

There are pros and cons to each side.

I'd like to politely chime in and suggest that my way is better.

Also @op... what's this line supposed to do?

amp >= 1.0;

Well, commented or uncommented, happily, it does nothing!