A Brief Note on Braces


Placement of braces

These notes write compound statements in such a way that the right brace is directly beneath the matching left brace. For example, in

  if(x > y)
  {
    m = x;
  } 
  else
  {
    m = y;
  }
you can easily see how the braces match up. But there is another style that is in widespread use where a left brace for an if-statement or while-loop is put at the end of the line just before the body of the compound statement. In that style, you would write
  if(x > y) {
    m = x;
  } 
  else {
    m = y;
  }
We will stick with the earlier style here, but you might encounter the alternative style elsewhere.

Note. You will almost never see someone place a right brace at the end of the last line of the compound statement. That tends to make programs difficult to read. For example, avoid writing the following.

  if(x > y) {
    m = x;} 
  else {
    m = y;}