Saturday, 18 March 2017

Control Structure in C++

Leave a Comment

The Definitive Guide To Control Structures in C++

A C++ program is not always limited to a linear sequence of instructions. A program may repeat or leaves some of its statements. 
The Control structure is a statement used to control the flow of execution in a program. These control structures are very helpful in implementing the program logic.
There are 4 types of control structures are as follows:

Sequence:

In sequential structure, the statements are executed in the same order in which they are specified in the program. The control flows from one statement to another in a logical sequence. We can write as many statements as we want but all statements are executed exactly once. It means that no statement is skipped and no statement is repeated.
For example, we want to write a program to add two numbers. The set of statements will be:
1.    Get 1st number from user.
2.    Get 2nd number from user.
3.    Sum both numbers using addition ( + ) operator.
4.    Print the sum on screen.
All these statements are written and executed only once. And the program executes them sequentially to  find sum.

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 statements is executed when a particular condition is true and ignored when the condition is false. There are different types of selection structures in C++ like:
·         IF Statement
·         IF...ELSE Statement
·         SWITCH Statement

The IF statement is a single-selection statement because it selects or ignores a single action. The IF...ELSE statement is called a double-selection statement because it selects between two different actions (or groups of actions). The SWITCH selection statement is called a multiple-selection statement because it selects among many different actions.

For example, we want to write a program which checks whether the number entered by user is eve or odd. The program will print even if it is an even number and program will print Odd if it is an odd number. The set of statements will be:
1.    Get the number from user.
2.    Check whether it is even or odd.
3.    Print Even if it was an even number.
4.    Print Odd  if it was an odd number.
In above example, statement no. 2 is used to make a decision. On the base of this decision, statement no. 3 or 4 is executed by computer. If your want complete source code then Click Here.

Repetition:

A repetition structure executes a statement or set of statements repeatedly. It is also known as iteration structure  or loop.
This structure is also based on conditions. Statements are repeated as long as a condition remains true.
There are 3 different types of repetition structures in C++ like:
·         WHILE Loop
·         DO...WHILE Loop
·         FOR Loop

The WHILE and FOR statements perform the action in their bodies zero or more times. If the loop-continuation condition is initially false, the loop body will not be executed. The DO...WHILE statement performs the action in its body at least once.

For example, we want to print “Hello World” 500 times on the screen. If we accomplish this job using sequence structure then we would write 500 lines of code which is very time consuming. We can do this by using just a single statement of repetition structure. An example of this structure can be found Here.  

Function Call:

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


About Author:

Kamal Choudhary is a tech geek who loves to write about technology and programming. His personal blog contains some cutting-edge C++ Programming Tutorials. He is studying Computer Science in University of Gujrat, Pakistan.



If You Enjoyed This, Take 5 Seconds To Share It

0 Questions: