Control Structures & Braces

Control Structures

When developing applications, the use of control structures such as conditional statements, loops, and objects are expected to be used appropriately (as defined by the needs of the application). The use of unconditional branching (i.e. goto statements) is not supported.

When nesting control structures, you must adhere to the following limitations:

  • Conditional statements (if/else statements and switch statements) should not be nested more than 5 levels deep. As an addendum, ternary statements should never be nested as this degrades the readability of the code.
  • Looping statements (for loops, while loops, and do-while loops) should never be nested more than 2 levels deep. In exceptional cases, 3 levels may be used provided the programmer can account for the upper-bound run-time efficiency of the algorithm to ensure that it will not hinder the processing abilities of the host machine.1)

Braces

When using conditional statements, loops, and defining class functions and variables, you should always use braces. While some languages will allow you to use a conditional statement without using braces, it is helpful to use braces to prevent inadvertent errors during future development. There are a number of common styles for using braces. The University of Arizona Library supports the two following styles:

Braces Style 1

  <?php
     if($thisYear % 4 == 0)
     {
        // Print out our leap year message
        echo "The year " . $thisYear . " happens to be a leap year. What ".
             "an exciting event!\n";
     }
     else
     {
        // Print out our normal year message
        echo $thisYear . " is not a leap year.\n";
     }
  ?>

Braces Style 2

  <?php
     if($thisYear % 4 == 0) {
        // Print out our leap year message
        echo "The year " . $thisYear . " happens to be a leap year. What ".
             "an exciting event!\n";
     } else {
        // Print out our normal year message
        echo $thisYear . " is not a leap year.\n";
     }
  ?>
1) For more on upper-bound algorithm efficiency, see the Wikipedia entry for Big-O Notation
 
programming\control_structures_braces · Last modified: 2008/01/22 12:54 by admin
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki