
Posted by Nelly Cuan on December 08, 1999 at 02:05:00:
(I have received approval from Dr. Bell to write on this topic.)
***Control Structures***
Usually statements in a program are executed one after the other in the order they are written, but there are many statements that allows the programmer to specify that the next statement that will be executed will be other than the next one in sequence. These statements are called control structures.
In order to determine the execution path, conditional statements uses true and false expressions (Boolean). An example is 5 == 4. This expression checks and see if the two operands (5, 4) or integers are equivalent. Also, in C++, if the expression evaluates to a nonzero then it is true. Some of the other conditional operators are <, >, <=, >=, etc.
The different control statements that I will discuss are if-else, while, do/while, for, and break/continue keyword.
The if/else statement can be used in two ways, one with the else statement and one without. Here is an example:
If (boolean_expression)
Statement;
Or it can be written as:
If (boolean_expression)
statement;
else
statement;
For the first If statement example, the statement under the expression is any statement that would be executed if the boolean_expression evaluates to true. If it does not, then it would skip the statement and continue executing the next line after (sequence).
For the second if statement example, the statement under the expression can also be any statement that would be executed if the boolean_expression evaluates to true. If it does not, then it would execute the statement after else.
The statement inside the control statement can be another if statement if another boolean_expression is necessary.
If/else statements are selection statements. Now we will talk about iteration or control looping. There are three types of iteration statements: while, do/while, and for. For iteration statements, a statement continues to execute until the controlled expression evaluates to false. It is important that the boolean_expression will evaluate to false along the line. Otherwise, it will be an infinite loop and can cause the program to crash. An example of a while loop:
while (boolean_expression)
statement;
In while loop, the boolean_expression is not restricted to a simple text like the if/else statement. It can be as complex as you would need it to be, just as long as the expression evaluates to true or false. Also the expression is evaluated in the beginning of the loop, therefore, it is possible that the statement after the expression is never executed if it is false.
The difference between the do/while loop and the while loop is that the statement in the do/while loop will always execute at least once. Thus, if the boolean_expression is false the statement will still go through one time. This is the case because the statement comes before the boolean_expression. Here is an example of a do/while loop:
do
statement;
while (boolean_expression);
For loop is another control structure. They are usually used for counting. There are three parts to the expression after the keyword for. They are initialization, conditional, and the step. An example of a for loop is:
for (initialization; conditional; step)
statement;
Initialization executes once in the beginning. The main part of the expression is the conditional part. It is tested before each iteration and determines whether to execute the statement or skip it and continue executing the next line of code. At the end of the loop the step executes and increments the number of times it runs the statement.
Another way we can manipulate the iteration statements discussed above is using the break and continue keywords. By adding the keywords, you can control the flow of the loop. Using break in the body of the loop will quit the loop without executing the rest of the statements specified in the loop. Using continue stops the execution of the current iteration and goes back to the start of the loop to begin a new iteration on the same loop.
Controlling execution is important in programming. Therefore, I find this topic worth discussing. As you can see, if we do not include conditional logic in our programs, the application will not be effective to perform well in a business world filled with choices and decisions.