When and how to use { } brackets

 

Hello Folks,


I'm quite new to coding, do however manage to write some stuff which works as expected.


I need clarification on one thing at this point.

When and how to use { }

I guess I use way too many brackets, which seems not to hurt anything. But I would like to know if there is a reson to use less and if more brackets hurt the execution time of the whole code.


An example as I write at this point:

If (a > b)

   {

   if(x = y)

      {

      Alert ("a is > b and x & y are the same");

      }

   }



Greetings.

WorstCases

 

here is an example

If (a > b)
   {
   if(x = y)
      {
      Alert ("a is > b and x & y are the same");
      }
   }

// if x = y, but a is not greater than b then the EA didn't enter that code at all & the alert will not popup


If (a > b)
   if(x = y)
      {
      Alert ("a is > b and x & y are the same");
      }
//but in this example even if a is not greater than b if x = y the alert will popup
 

Thank you.

So this example code from you would not be useful in reality, because part of it does not influence the output:

If (a > b)
   if(x = y)
      {
      Alert ("a is > b and x & y are the same");
      }

So my example code is ok (if you only take into consideration the brackets), isn't it.

If it'd be about the code itself, it might be more clean to write it like this:

If (a > b && (x = y)
   {
   Alert ("a is > b and x & y are the same");
   }
   
 

the brackets denote a block of code that is needed in constructs like if and for and while and a few others.


// if you write

if (a == b)
  something();


// then this block of code consists of only one statement, 
// then (and only then) you can leave away the braces.

if (a == b) {
  something();
}

// would be the exact same thing written with braces.
// since there is only one instruction in this block there
// could never be any doubt where it begins and where it
// ends even without the braces, so you are allowed to
// leave them away.

if (a == b)
   if (c == d)
      something();

// in this case the second if as a whole is seen as *one* 
// statement for the outer if, although it spans across 
// two lines in total it cannot be splitted anywhere, there
// is no ambiguity.

if (a == b)
   something();
   somethingelse();

// would NOT WORK anymore as expected, it would assume that
// this is a block of only one statement, it cannot read your
// mind and the if expects a block and if there are no braces
// then it would assume that it is a single-statement block and
// only apply to the first line. If you want both lines to be
// affected by the if then you need braces:

if (a == b) {
   something();
   somethingelse();
}

// now the if knows without a doubt where the block begins 
// and where it ends.

// some people (including me) recommend always using braces
// and never leave them away, even if it is only a single 
// statement because it avoids problems when you later want 
// to extend the code and add another line and *forget* to add
// the now needed braces. In an environment where you always 
// have a mixture of the long form (with braces) and the 
// lazy shorthand (without braces) for single line blocks
// this is a permanent potential source of error. Also i believe
// it makes code easier to read.

// Only *sometimes* I also use the shorthand but then I make sure
// it really looks different and cannot be easily confused
// with the long form, then I write it all on the same line:

if (a == b) something();

// and as a general advice for formatting I recommend using the
// 1TBS style for formatting the braces which means putting
// the opening brace always at the end of the opening line:

if (a == b) {
   something();
   if (c == c) {
      someotherthing();
   }
}

// as opposed to

if (a == b) 
{
   something();
   if (c == c) 
   {
      someotherthing();
   }
}

// or the equally bizarre

if (a == b) 
   {
   something();
   if (c == c) 
      {
      someotherthing();
      }
   }


// you will certainly admit that this:

if (a == b) {
   something();
   if (c == c) {
      someotherthing();
   }
}

// just looks nicer and more compact and is really easier 
// to read, write and maintain. It is also the preferred
// style amongst most programmers of all C-like languages,
// its the recommended Java and C# style and also used by
// most C++ programmers.
 
 
7bit:

the brackets denote a block of code that is needed in constructs like if and for and while and a few others.


it's a great job u have done
 
qjol:

here is an example

your both examples are equivalent, the second version is NOT broken.
if (a == b)
   if (x == y) {
      Alert("a == b  AND x == y   This works as expected, the second if as a whole is only one statement!")
   }
 

Sorry for the late reply.

I was just checking if I had some new replies and I really must admit, that I really appreciate your post, 7bit.

This is a great peace of information and answers all my questions.

As you said: I guess I'll use the brackets at all times. This will avoid confusion and will make it more easy to add to the code later.

Thanks you!

WorstCases

 
what is shortcut keyboard for curly brakect in MQL4
 
Ssonawa1: what is shortcut keyboard for curly brakect in MQL4
  1. Don't Hijack other threads for your off-topic post. Next time, make your own, new, thread.

  2. The keyboard shortcut for a curly bracket are … … …  braces. "{", "}".
 
if(a > b && x = y)
   Alert ("a is > b and x & y are the same");
In this way both should be true otherwise no Alert.

Without brackets only the following line is conditioned 
 
Milko Vivaldi #:

Your copy of the code as well as the original are "flawed". The "=" operator is an assignment and not a comparison "==". It should have been ...

if(a > b && x == y)
Reason: