STATEMENT FLOW CONTROL

In a program, statements may be executed sequentially, selectively or iteratively. Every programming language provides constructs to support sequence, selection or iteration.

Let us discuss what is meant by sequence, selection or iteration constructs.

Sequence

The sequence construct means the statements are being executed sequentially. This represents the default flow of  statement. 
Every C++ program begins with the first statement of main(). Each statement in turn is executed (sequence construct). When the final statement of main() is executed, the program is done. This construct specifies the normal flow of control in a program and is the simplest one. Following figure explains sequence construct.
The sequence construct



Selection

The selection construct means the execution of statement(s) depending upon a condition test. If a condition-test. If a condition evaluates to true, a course-of-action (a set of statements ) is followed otherwise another course-of-action (a different set of statements) if followed. This construct (selection construct) is also called decision construct because it helps in making decision about which set-of-statements is to be executed. Following figure explains selection construct.
The selection construct

Iteration

The iteration construct means repetition of a set-of-statements depending upon a condition-test. Till the time a condition is true (or false depending upon the loop), a set-of-statements are repeated again . as soon as the condition becomes false (or true), the repetition stops. The iteration construct is also called looping construct. 

Following figure illustrates an iteration construct.
The iteration construct


The set-of-statements that are repeated again and again is called the body of the loop. The condition on which the execution or exit of the loop depends is called the exit condition or test-condition. 

Every programming language must provide all these constructs as the sequential program execution (the default mode) is inadequate to the problems we must solve. C++ also provides statements that support these constructs. Coming sections discuss these C++ statements.

As you have seen selection and iteration constructs depend upon a conditional expression that determines what course-of-action is to be taken. A conditional expression evaluates to either true or false. In C++, any nonzero value is a true value including negative numbers. A 0 (zero) is treated as false. Though you have already read this concept of true and false values in C++, yet we revised it just because it is relevant to the topic of this article.






Post a Comment

0 Comments