Control Statements in C++ with Examples
Control Statements in C++:
In C++, Control Statements are usually jumped from one part of the C++ code to another depending on whether a particular condition is satisfied or not. There are three types of C++ Control Statements are given as follows:
1. Sequence Statement
2. Selection Statement
3. Loop Statement
Types of Control Statements in C++:
if-else statement:
if-else statement is used to execute a statement block. It has the following syntax:
if(expression is true
{
action 1;
}
else
{
action 2;
}
switch statement:
It is a multiple multiple-branching statement where based on a condition, the control is transferred to one of the many possible points. It has the following syntax:
xxxxxxxxxx
switch(expression)
{
case1:
{
action 1;
}
case 2:
{
action 2;
}
case 3:
{
action 3;
}
default:
{
default statement;
}
}
for statement:
for is an entry-controlled loop. It is used when an action is to be repeated for a predetermined time. It has the following syntax:
xxxxxxxxxx
for(initial value; test condition; increment or decrement)
{
action 1;
}
while statement:
It is also a loop control structure but it’s an entry-controlled loop. It has the following syntax:
xxxxxxxxxx
while(condition is true)
{
action 1;
}
action 2;
do-while statement:
do-while is an exit-controlled loop based on a condition, the control is transferred back to a particular point in the program. It has the following syntax:
xxxxxxxxxx
do
{
action;
}
while(condition is true);
action 2;