CodeClerks

Switch, Do-While and For Statement in C++



Enter a reason for deleting this comment

Switch, Do-While and For Statement in C++

This is a multiple-branching statement where, based on a condition, the control is transferred to one of the many possible points. This is implemented as follows:

   switch (expression)
   {
case1:
{
action1;
}
case2:
{
action2;
}
case3:
{
action3;
default:
{
action4;
}
   }
   action5;


The do-while statement
The do-while is an exit-controlled loop. Based on a condition, the control is transferred back to a particular point in the program. The syntax is as follows:

do
{
   action1;
}
while (condition is true);
action2;


The while statement
This is also a loop structure, but is an entry-controlled one. The syntax is as follows:
while (condition is true)
{
action1;
}
action2;

The for statement
The for is an entry-enrolled loop and is used when an action is to be repeated for a predetermined number of times. The syntax is as follows:
for(initial value; test; increment)
{
action1;
}
action2;

The syntax of the control statements in C++ is very much similar to that of C and therefore they are implemented as and when they are required.

Comments

Please login or sign up to leave a comment

Join