Query: If ... Else Statement

I am taking a course in Javascript. Can the "if ... else" statement be continued like in Javascript? (i.e. "if ... else if ... else if ... else if ...")

yes

if (a == b)
{
  Serial.println("equal");
} 
else if (a < b) 
{
  Serial.println("lower");
} 
else
{
  Serial.println("higher");
}

C++ also knows the Switch-Case statement. (in fact Java inherited a lot from C/C++)

in fact Java inherited a lot from C/C++

Borrowed or stole would be better terms. There is NO inheritance going on.

Paul: Too subtle for someone coming from Java... :confused:

econjack:
Paul: Too subtle for someone coming from Java... :confused:

I'm thinking that it's a language thing. After all, Rob's native language is not English.

(in fact Java inherited a lot from C/C++)

Whether Java begged, borrowed or stole the idea is irrelevant as the OP was asking about Javascript.

Java is an Object Oriented Programming language created by Sun Microsystems. JavaScript is a scripting language that was created by Netscape and was originally known as LiveScript. JavaScript is a (very) distant cousin of Java in that it is also an OOP language.

Wow, that derailed quickly. Thanks.

Another question on the "if ... else" does the Arduino code require the final "else"? or is it optional like in javascript?

See http://arduino.cc/en/Reference/HomePage
or more specifically
http://arduino.cc/en/Reference/If
and
http://arduino.cc/en/Reference/Else

Any syntactic questions can be referred to any standard C++ tutorial. It is not "Arduino code" it is C++.

Correction noted, thanks.

UKHeliBob:
See http://arduino.cc/en/Reference/HomePage
or more specifically
http://arduino.cc/en/Reference/If
and
http://arduino.cc/en/Reference/Else

I will look into it, thanks.

UKHeliBob:
See http://arduino.cc/en/Reference/HomePage
or more specifically
http://arduino.cc/en/Reference/If
and
http://arduino.cc/en/Reference/Else

Well, I’m old enough to have patience and read documentation, but on Arduino IDE, documentation is very poor.
For example this is we can learn from reference:

if (pinFiveInput < 500)
{
// do Thing A
}
else if (pinFiveInput >= 1000)
{
// do Thing B
}
else
{
// do Thing C
}

However, this construct seems valid as well under the Arduino IDE (wire library, Teensy):

void TwoWire::setClock(uint32_t frequency)
{
#if F_BUS == 60000000
if (frequency < 400000) {
I2C0_F = 0x2C; // 104 kHz
} else if (frequency < 1000000) {
I2C0_F = 0x1C; // 416 kHz
} else {
I2C0_F = 0x12; // 938 kHz
}
I2C0_FLT = 4;

#elif F_BUS == 56000000

if (frequency < 400000) {
I2C0_F = 0x2B; // 109 kHz
} else if (frequency < 1000000) {
I2C0_F = 0x1C; // 389 kHz
} else {
I2C0_F = 0x0E; // 1 MHz
}
I2C0_FLT = 4;
#else
#error “F_BUS must be 60, 56, 48, 40, 36, 24, 16, 8, 4 or 2 MHz”
#endif
}

So, the question is where can we find a C++ complete sintax definition ( else if = #elif ?) used by the Arduino IDE compiler?
If we struggle learning Arduino, at least to have all documentation you have…:slight_smile:

thx,
Vasile

where can we find a C++ complete sintax definition

Maybe start with Arduino - Else but #elif is not strictly C++ anyway

vsurducan:
So, the question is where can we find a C++ complete sintax definition ( else if = #elif ?) used by the Arduino IDE compiler?

The definitive reference, by the author of the language is: The C++ Programming Language, by Bjarne Stroustrup. Anyone who is serious about C++ programming should have a paper copy on their bookshelf (my copy is very well worn.)

You can also use any number of online C++ reference sources (Google shows lots of them.) For example:

The big difference between #if/#elif/#else/#endif and if/else if/else is that the former is part of the pre-processor and is computed at compile time, and the latter is part of the language and is computed at run time.

In the first example you gave, all of the code is included as part of the sketch, and the various tests are done at run time, during every pass of the loop.

The second example is testing various clock speeds. The clock speed is a constant that depends on the board you are using. It is known at compile time, and will never change while the program is running. The #if blocks are evaluated by the preprocessor before the code us actually compiled, and the compiled sketch will only include those portions of code that are in blocks evaluated as true. Any code on the blocks that evaluate as false are actually stripped from the code and won't be included in the compiled sketch. This is a great way to select between various configurations, but not bloat the sketch with code that is not appropriate for the current configuration.

vsurducan:
So, the question is where can we find a C++ complete sintax definition ( else if = #elif ?) used by the Arduino IDE compiler?
If we struggle learning Arduino, at least to have all documentation you have...:slight_smile:

Read this before posting a programming question

There really is no "Arduino compiler". It is the standard avr-g++ compiler, which is a variation of the g++ compiler used all around the world to compile C++ programs. Questions like "if" vs "#if" can be answered by a Google search. For example, Googling:

c++ if vs #if

... reveals over half a million hits.