C++ Control Statements | if-else, switch, for, while and do-while

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 :

  • Sequence Statement
  • Selection Statement
  • Loop Statement
  • control

    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 branching statement where based on a condition, the control is transferred to one of the many possible points. It has the following syntax :
    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 number of time. It has the following syntax :
    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 :

    while(condition is true)
    {
     action 1;
    }
    action 2;
    

    do-while statement : do-while isn 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 :
    do
    {
     action;
    }
    while(condition is true);
    action 2;