Similar Syntax?

Hello folks. First, thanks to all the guys who take their valuable time to answer questions here!

Do the 2 pieces of code below do the same exact thing? Is Code 2 just a different way of writing Code 1 or is there any advantage to the way Code 2 is written?

Code 1

if(myFunction() == true)
apple = 56;

Code 2

if(myFunction() == true)apple = 56;

I have seen this kind of code revision in a few sketches and thought I should ask.

Thank you for your replies!

The two results are the same. Only the looks different.
The latter is more likely to cause weird write bugs than the former, and I think many people always write brackets.

1 Like

So is this based on the rules of the C++ language?


The two do the same thing

Personally I prefer the first at the expense of it taking up more screen space in the editor. To me it is more obvious what is in the dependant code block and lends itself to easily extending that code

An alternative form to avoid typing is

if(myFunction() )
apple = 56;

All of the formats depend on the myFunction() returning a boolean variable or a value that can be interpreted as a boolean, but that is a different subject

You need to provide a statement that will be executed when the condition is true. a simple assignment is a statement so you can write it that way
if (myFunction() == true) apple = 56;

if myFunction() returns a boolean, it's already a truth value, so one would actually write

if (myFunction()) apple = 56;

but if you are not careful and later on want to do other stuff in that if, then brackets will be needed. That's why you'll often see a (useless) compound statement (the {...}) with only one statement in it

if (myFunction()) {
  apple = 56;

just in case you want to add stuff later on and are not careful...

Minor note: This is not entirely equivalent. If myFuntion() returns an integer the 'if(myFunction() == true)' is equivalent to 'if(myFunction() == 1)' but 'if(myFunction())' is equivalent to 'if(myFunction() != 0)'

int function0() {return 0;}
int function1() {return 1;}
int function2() {return 2;}

void setup()

  if (function0())
    Serial.println("\"function0()\" is true");
    Serial.println("\"function0()\" is false");

   if (function0() == true)
    Serial.println("\"function0() == true\" is true");
    Serial.println("\"function0() == true\" is false");

   if (function1())
    Serial.println("\"function1()\" is true");
    Serial.println("\"function1()\" is false");

   if (function1() == true)
    Serial.println("\"function1() == true\" is true");
    Serial.println("\"function1() == true\" is false");

   if (function2())
    Serial.println("\"function2()\" is true");
    Serial.println("\"function2()\" is false");

   if (function2() == true)
    Serial.println("\"function2() == true\" is true");
    Serial.println("\"function02() == true\" is false");

void loop() {}
"function0()" is false
"function0() == true" is false
"function1()" is true
"function1() == true" is true
"function2()" is true
"function02() == true" is false

C Language allows creating excuteable codes using artficial/modified English Language. In English language, we have the following style of expression using if:

If you attend regular classes, there is a chance for you to get good grade in the exam.

In the above, the sub-ordinate clause (there is a chance for you to get good grade in the exam.) is in the same line with the principle clause (If you attend regular classes,).

In C Language, the Literate Programmers put the sub-ordinate clause in the 2nd line with an indentation and preferably with a pair of {} braces. Accordingly, we should have:

1. Literate Style

if(myFunction() == true)
    apple = 56;

2. Illiterate Style; but, it is correct syntactically and semantically.

if(myFunction() == true)apple = 56;

yep, that's why I wrote "if myFunction() returns a boolean"

what does this have to do with this thread???

and it does not have to do with literate or Illiterate style. it's usually driven by coding rules where you work. Some places will make the compound statement mandatory even if you have only one expression statement in the flow control of the Selection statement. This is to guard against future bugs.

Some other say it's OK as long as you put the expression statement on the same line as the if statement so that it looks like one thing all together. So if (myFunction()) apple = 56; would be fine then but

  if (myFunction())
    apple = 56;

would not as there is a risk of adding with the same indentation (those python programers...) something you want to execute when the condition is true and forget to add the compound statement.

litterate programmers would use the space bar, though :slight_smile: and not write
if(myFunction()==true)apple = 56;

1 Like

The way a word is not sovereign; in the same way, a partial quote is not sovereign; rather, it has to be seen/read/interpreted collectively with the rest of the write-up.

I don't know; but, I remember that my tutor told me in the first class of C programming that one has to be a Literate Programmer if he wishes that his codes would be read by others.

So, you do admit the existence of Literate Programmers!:slight_smile:

Hate this practice.

if (condition)

Much prefer:

if (condition) {

Same with 'for', 'while', etc. Putting the '{' on its own line just takes up an extra line and does not add anything. Putting the '}' on its own line makes sense as it lines up with the 'if ()'.


well... Enlight us in the context of this post and your full post... I still don't see why it's relevant.

(and we use C++ not C)

Yes, my turn to be picky today :slight_smile:

and I feel the entirely opposite way

Putting the opening brace on its own line really emphasises the start and end of the dependant code block and both of the braces line up with the 'if()' which makes even more sense

Luckily we can both do it our own way, but the first thing that I do with code copied from the forum into the IDE for examination is to use Auto format which I have set up to format the code in my preferred way

Indentation come in a variety of styles...

But we all know that the style we use ourselves is best :grinning_face_with_smiling_eyes:

1 Like

Unless forced into a don't like style when writing code at job!

+1 @UKHeliBob :+1:

I would like to believe that you are also a student of C and not C++; rather you obtained an ornamental degree in C++.:slight_smile:

Me, too. That's why I wrote my own "formatter.conf" to force sketches to my preferred format. :slight_smile:

# This configuration file contains a selection of the available options provided 
# by the formatting tool "Artistic Style"
# If you wish to change them, don't edit this file.
# Instead, copy it in the same folder of file "preferences.txt" and modify 
# the copy. This way, you won't lose your custom formatter settings when 
# upgrading the IDE
# If you don't know where file preferences.txt is stored, open the IDE, 
# File -> Preferences and you'll find a link


# 2 spaces indentation

# Put braces on separate lines

# also indent macros

# indent classes, switches (and cases), comments starting at column 1

# put a space around operators

# put a space after if/for/while

# if you like one-liners, keep them


Me too