Control Structures in C++

The entire logic of the program is controlled by four types of structures, these structures are known as control structures. Control structures are used to control the flow of execution. When program starts the control structures directs that which statements should execute in sequence, repeat or which statements should skip.

Types of Control Structures

There are three types of control structures.

1. Sequence

In sequence control structure the statements/instructions are executed in the same order in which these are written. No statement is skipped and no one is repeat. This is the by default implemented control structure.

Example of Sequence Control Structure

#include <iostream>
using namespace std;

int main(int argc, char** argv) {
	
	cout<<"Hello";
	cout<<"World";
	return 0;
}

In the above example statements will execute one after another. First Hello will print then World will print.

2. Repetition

In repetition control structure statement or set of statements are executed for a specified no of time. If more than one statement are to be repeated then these statements are enclosed in curly brackets. The statements enclosed in curly brackets is called statement block.

Example of RepetitionControl Structure

#include <iostream>
using namespace std;

int main(int argc, char** argv) {
	
	int a=1;
	
	while (a<=5) {
		cout<<"Hello World\n";
		a = a + 1;
	}
	
	return 0;
}

In above example Hello will be print 5 times.

3. Selection

In selection control structure statement or set of statements are executed or skipped on the basis of particular condition. Condition is the relational expression that returns either true or false.

Example of Selection Control Structure

#include <iostream>
using namespace std;

int main(int argc, char** argv) {
	
	int a;
	a=1;
	
	if (a > 0 ) {
		cout<<"Hello World";
	}
	
	return 0;
}

In above example expression a > 0 is true so that Hello World will print.

Comments
Login to TRACK of Comments.