Monday, October 11, 2010

Project 10 - Control Structures


A. Four basic control structures and their sub-constructs

The four basic control structures are:
  1. Conditional (if and else)
    • If statements check a condition
    • If else statements will be checked after the initial If statement
    • Else statements come last and will always be activity IF no other parts returned true.
  2. Iteration (loops)
    • While loop, continue doing an action until condition is false
    • Do-While same as while loop, only it will always perform the process once before checking the condition
    • For loop, same as a while loop, only provides specific locations to contain an initialization statement and increase statment
  3. Jump Statements
    • Break statements will leave a loop even if the condition for its end has not been fulfilled
    • Continue Statements will skip the rest of the loop in the current iteration as if the end block had been reached
    • Goto make an absolute jump to another point in the program
    • Exit terminates the currend program
  4. Selective (switch statement)
    • Check several possible constant values for an expression, similar to if and else if instructions

B. Sequence 

Sequence is a very basic control structure. It is simply a list of things to do.
  • Do this thing
  • Than this thing
  • Than this other thing.
And so on. There is no decision-making, looping, or branching. It is just a series of statements, one after the other. For example:

This would be an example of a sequence structure.

double average = 0;
int first = 8; 
int second = 5;
int third = 3;
int fourth = 7;
average = (first + second + third + fourth)/4;
System.println(average);

All it does is run through the code line by line until it reaches it's end and therefore has performed the function the programmer wanted it to. This is the simplest and least powerful control structure available and is usually used in combination with selection and loop structures.





C. Selection (Decision, If Then/Else) - Case Statement Available in Some Language

Selection is exactly what it sounds like, it selects something based on some condition.

This is an example of the selection structure

Selection statements come in if/then/else format. For instance:

bool cake = false;

if(cake){
   eatCake();}
 else{
   sleep();}

The selection control structure takes the statement and decides wether it is true or false (Boolean logic comes into play here and if it's numbers you need <, >, or = for comparison) than depending on whether this statement is true or false it takes a certain action. In this case, since cake is always false the program will never run the method eatCake() and will always go to the else statement and run the method sleep().

D. Loop (looping, iteration) - For Statement Available in Some Language

Loop statements will continue to perform some function until some parameter is met.

Example:

int piecesOfCake = 10;

while(piecesOfCake > 0){
    eatPiece();
    piecesOfCake--;}

sleep();

In this example the paramater is checking if there are any pieces of cake left, if there are it eats a piece then goes to check if there is another one again and so on until there is no longer cake, then it exits the loop and goes to sleep. Some languages also use do-while (execute statement, than check paramater) and for (so long as this condition is true, usually a counter of some sort) loops.

E. Unconditional Branch (Goto)

Goto statements are a one-way trip to another section of the code. Most languages use label to identify the place where the Goto statement jumps to, though some languages use line numbers. There has been a lot of criticism about Goto statements as bad programming habit, favoring instead the structured programming paradigm, though it has been argued that sensible programmers could use it to improve speed, size and code clearness. Whichever side one tends to agree with, it seems both can agree that newer programmers should stay away from the Goto statement until they are more familiar with the potential advantages and disadvantages involved.

No comments:

Post a Comment