The control structures appear in both structured programming languages as well as object oriented programming languages. The three constructs used are:
i). Sequence: - This is when one statement is executed following another.
ii). Selection: -It involves making a choice between at least two alternative courses of action. In C++
there are two ways of making selections.
with else being optional
The following is the general format:
Case value: ; [-----break;]
The switch is appropriate when a number of possible states of variable have to be evaluated at
iii). Iteration: - It involves repeating a section of code, for a number of times. In C++ it is achieved in
- for loop
- while loop
- do.....while loop.
In each case, there is a condition which allows the loop to terminate.
The for loop has three principle elements:
9 The start condition
9 The terminating condition
9 The action which takes place at the end of each iteration Syntax
for(start condition; continue condition; re-evaluation)
The following code segment outputs the squares of numbers one to ten
cout<<"The square of: "<
It is used to execute a block of statements an indefinite number of times, for zero or more number of times (i.e. pre-test loop).
It is used to execute a block of statements an indefinite number of times, for one or more number of times and will execute at least once (i.e post-test loop).
} while (condition);