Home Written Works Formatting Control Structures

Search

Formatting Control Structures Print
Written by Chris Gountanis   

The control structures in your program are the most direct representation of the logic needed to implement your specifications. The format of these control structures, therefore, will have a significant impact on the readability of your code. Indentation is the most important element of control structure layout. Always keep statements of the same "logical level" at the same indentation level.


If Statements
In general, the IF statement is composed of clauses in which there is a Boolean expression or condition and a section of code executed when that condition evaluates to TRUE. The “if statement” basically answers the question is it true or false. If the tested value matches the condition it runs the code primary code or follows the “else statement”. I “nested if statement” contains grouped “if statements” one when in another. If you find yourself going more than 3 levels deep try a “switch statement”.

Basic If Example
If(condition) {
        ...
}

Nested If Example
If(condition)
        If(condition)
        ...
        Else
                If(condition)
                ...

Formatting Control Structures

 

Switch
A switch or select case statement's purpose is to allow the value of a variable or expression to control the flow of program execution.

Switch Example
switch (employeeCode) {
    case 1:
           commission = .10 * employeeSales;
           break;
    case 2:
           commission = .14 * employeeSales;
           break;
    case 3:
           commission = .18* employeeSales;
           break;
}

Formatting Control Structures

 

Loops
The “while statement” is used to repeat a block of statements while some condition is true. The condition must become false somewhere in the loop, otherwise it will never terminate. You can have the condition at the start or end of the “while statement allowing pre/post testing of that condition.

While Loop Example
while (condition) {
        ...
}

Formatting Control Structures

 


Many loops consist of three operations surrounding the body: (1.) initialization of a variable, (2.) testing a condition, and (3.) updating a value before the next iteration. The for loop groups these three common parts together into one easy to read statement.

For Loop Example
for (int i = 1; i <= 20; i++)
         ...
Next i

Formatting Control Structures

 


Functions and Parameters
In programming, a subroutine (function, method, procedure, or subprogram) is a portion of code within a larger program. This subset of code performs a specific task and can be relatively independent of the remaining code. (wikipedia.com)

Function with Parameters Example
public static double getComm(double employeeSales, int employeeCode)

 

 

Formatting Control Structures

 

 


Formatting


• Decimal Formatting ($1,000.00)
       DecimalFormat twoDigits = new DecimalFormat ("$#,000.00");
       twoDigits.format(variable)

• Rounding (25.56342 to 25.56)
       public static float Round(float Rval, int Rpl) {
              float p = (float)Math.pow(10,Rpl);
              Rval = Rval * p;
              float tmp = Math.round(Rval);
              return (float)tmp/p;
       }

Computer Programming and Technical Research
Last Updated on Saturday, 16 February 2008 14:01