j

Sunday, 13 December 2015

What is Control Structure define the types of control structure.




 

Control Structure


                A statement used to control the flow of execution in a program is called control structure. The instructions in a program can be organized in three kinds of control structures to control execution flow. The control structure is used to implement the program logic.
Types of Control Structure
            Different types of control structure are as follows:
Sequence
In sequential structure, the statements are executed in the same order in which they are specified in program. The control flows from one statement to other statement in logical sequence. All statement is executed exactly once. It means that no statement is skipped and no statement is executed more that once
Example
                Suppose a program inputs two numbers and displays average on screen. The program uses two statements to input numbers, one statement to calculate average and one statement to display average number. These statements are executed in a sequence to find the average number. All statements are executed once when the program is executed. It is an example of sequence control structure.

Selection
                A selection structure selects a statement or set of statements to execute on the basis of a condition. In this structure, statement or set of statement is executed when a particular condition is true and ignored when the condition is false. There are different types of selection structure in C++ these are if….else and switch.
Example
                Suppose a program inputs the marks of a student and displays a message on screen whether the student is pass or fail. It displays pass if the student gets 40 or more then 40 marks. It displays fail when the marks are below 40. The program checks the marks before displaying the message. It is an example of selection structure.

Repetition
            A repetition structure executes a statement or set of statements repeatedly. It is also known as iteration structure or loop. There are different types of repetition structure in C++. These are while, do while and for.       
Example
                Suppose we want to display a Name “Umair Jatoi” on screen for 100 times. It is time consumi9ng to perform this task using control sequence; however, it is very easy to perform this task using repetition structure. A single loop statement can display the message for 100 times.


Function call
                Function call is a type of statement that moves the control to another block of a code the control returns back after executing all statement in the block. The remaining statement are executed immediately after the function call when the control returned


Subscribe to this Blog via Email :