Friday, December 9, 2011

Using C++ Control Statements to Transfer Control of Execution

Introduction to C++ Control Statements: Statements that specify the order in which actions are executed are called control statements. C++ control statements in a program execute one after the other in the order they are written (also known as sequential execution) unless there is a direct instruction to act otherwise. All the programs we have used so far included sequential execution which is one of types of C++ control statements. There are however other ways in which a program can execute a statement other than the sequence of next in the line . Enabling this to happen is known as transfer of control. Transfer of control includes other two forms of control statements. These types of C++ control statements  include selection statements and repetition statements.

Selection Statements

Selection Statements which are forms of C++ control statements are sub divided in three types of other types namely, if, if….else and switch control statements.

If Selection Statement

One form of a C++ control statement is the if selection statement which performs an action if a set condition is correct but if the condition is false the program skips the action and execution continues with the rest of the statements. An if statement is written with a key if followed by a condition enclosed in braces and then a statement to be executed when the condition is correct. Where the if form of control statement is to be used, it is advisable to enclose statements in curly brackets, where more than one are to be executed, . 

For more information on C++ control statements, please refer to the detailed chapter on C++ Selection Statements, Repetition C++ control statements and Other transfer of control statements .