Pages: [1]   Go Down
Author Topic: Two conditions in an if statement  (Read 1701 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi, This is probably a silly mistake I have made but I am trying to get the arduino to read the serial input and then change the colour of a tri-colour LED accordingly. For this to happen I need it to check that the number entered fits the conditions >= 0 and <= 255 which I put into the following statement:

Code:
if(rValue >= 0 && <= 255){goto B;}

However it doesn't like this and wants a primary expression before the '<=' I have no idea what this is so could somebody please advise me on how to do it?
Thanks
Owen
Logged

California
Offline Offline
Faraday Member
**
Karma: 82
Posts: 3123
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
if( (rValue >= 0) && (rValue<= 255) ){goto B;}

I would also recommend against using the goto statement, but it's your party.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That works thanks, Why would you not advise using a goto command?
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 239
Posts: 24373
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Because people will throw rocks at you and shun you.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Saskatchewan
Offline Offline
Sr. Member
****
Karma: 15
Posts: 348
When the going gets weird, the weird turn pro. - Hunter S. Thompson
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have a packet of gravel at the ready. Good thing I remembered my beard.  smiley-lol
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This joke has gone way over my head..... If anyone cares enough to give an explanation?
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 509
Posts: 31487
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Why would you not advise using a goto command?
Once upon a time there was a language that had the 'go to' command as the only way of changing the execution of code. While the code was simple all was well. But as the code became more and more complex it became harder and harder to follow. The King named this spaghetti code and as he detested pasta banished it from the land. However Mr Ritchie had an Italian wife who loved pasta so he included it in his new language called C. However if the King ever catches anyone using it then he gets most cross as he still doesn't like spaghetti.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So basically code should be executed in its linear order as it is written and shouldn't be allowed to jump around all over the place?
Logged

California
Offline Offline
Faraday Member
**
Karma: 82
Posts: 3123
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So basically code should be executed in its linear order as it is written and shouldn't be allowed to jump around all over the place?

That's the best practice. You may have the cool guys with their aviator glasses coming around to tell you it doesn't matter and Goto is fine, but most will agree that for smaller projects it doesn't matter, but scaling it up can cause it to be very difficult to read/debug.
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 509
Posts: 31487
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Basically if you have to use a goto then you are doing the code wrong and you haven't thought enough about how it should work.
Logged

Peoples Republic of Cantabrigia
Offline Offline
God Member
*****
Karma: 6
Posts: 691
Arduino happiness
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Ideally, all your code fragments / functions are called from loop() and the results are returned, processed, etc. in a manner similar to GOSUB. Why is this better?

For one, it's a lot more efficient because you can set up recurring code fragments as separate functions and call them from loop() or from within another function. Secondly, you only have to write a particular function once, then you can re-use it over and over. Additionally, it is helpful from the point of view of the limited SRAM your processor has - as functions are terminated, the SRAM is freed up again. Last but certainly not least, you only have to debug a function once, you can isolate it easily and make sure it works 100% of the time.

The only downside to using functions as opposed to doing it all inside a massive loop() program is that functions cannot return arrays or multiple numbers, only single variables. Thus, you may have to define a couple of global variables (which eat up SRAM) to handle additional values, etc. that each function may need to return. But on balance, I expect the use of functions to be way more efficient in terms of SRAM utilization than doing it in a big loop.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 239
Posts: 24373
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
The only downside to using functions as opposed to doing it all inside a massive loop() program is that functions cannot return arrays or multiple numbers, only single variables.
A function can return an array if it is inside a "struct" smiley
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Peoples Republic of Cantabrigia
Offline Offline
God Member
*****
Karma: 6
Posts: 691
Arduino happiness
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

A function can return an array if it is inside a "struct" smiley
Something to research and thanks for the correction.
Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 58
Posts: 4020
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So basically code should be executed in its linear order as it is written and shouldn't be allowed to jump around all over the place?

It's how you jump that matters. Goto can just be a pain to track and hell to maintain. It's necessary when you don't have structures or the right structure available in the language you use but once you have them it is best avoided.

We have

if (expression)
{
  do if true
{
else
{
  do if false
}

so there's generally (never say never) no clean reason to branch to a label on true.
Logged

Examples can be found in your IDE.

Pages: [1]   Go Up
Jump to: