A control statement controls the flow of a program based on given conditions. In a program, the statements get executed one after the other, sequentially, with neither any repetitions nor any exceptions. In case if we require execution of certain statements, then here the role of control statements come into play.


DECISION CONTROL STATEMENT


C++ handles decision-making by following statements:


The if statement

The if statement is used to specify a block of code to be executed if a specified conditon is true.


Syntax:

if(condition)

{

  statement1; //gets executed if condtion is true

}

  statement2;  //gets executed irrespective of any condition


 Example:

//C++ program to demonstrate if statement

#include<iostream.h>

#include<conio.h>

using namespace std;

int main()

{

  int num= 17;

  if (num % 2 == 1)

  {

    cout<<"It is an odd number.";

 

  return 0;

}

OUTPUT:



The if-else statement

If the condition provided in the if statement is false, then the block of code in the else statement gets executed.


Syntax:

if(condition)

{

  statement1; //gets executed if condition is true

{

else

{

  statement2;  //gets executed if conditon is false

}


 Example:

//C++ program to demonstrate if-else statement

#include<iostream.h>

#include<conio.h>

using namespace std;

int main()

{

  int num = 18;

  if (num % 2 == 1)

  {

    cout<<"It is an odd number.";

  } 

  else 

  {

    cout<<"It is an even number.";

  }

  return 0;

}

OUTPUT:




Nested if Statement

It means an if statement inside another if statement.


Syntax:

if(condition1)

{

  statement1;

  if(condition2)

  {

    statement2; //gets executed if both condition1 & condition2 is true

  }

}


 Example:

//C++ program to demonstrate nested if statement

#include<iostream.h>

#include<conio.h>

using namespace std;

int main()

{

  int a=5, b=10;

  if(a<15)

  {

    if(a<b)

    {

      cout<<"a is smaller than b.";

    }

    else

    {

       cout<<"a is less than 15.";

    }

  }

  else

  {

    cout<<"a is greater than 15.";

  }

  return 0;

}

OUTPUT:




The if-else-if ladder statement

When a program contains multiple conditions, then if-else-if statement is used. It is a top-down approach.


Syntax:

if(condition1)

{

  statement1;  //gets executed if condion1 is true

}

else if(conditon2)

{

  statement2;    //gets executed if condition1 is false & condion2 is true

}

else if(condition3)

{

  statement3;    

}

else

{

  statement4;

}


 Example:

//C++ program to demonstrate if-else-if statement

#include<iostream.h>

#include<conio.h>

using namespace std;

int main()

{

  int a = 5;

  if(a<10)

  {

    cout<<"The number is less than 10.";

  }

  else if(a>10)

  {

    cout<<"The number is greater than 10.";

  }

  else

  {

    cout<<"The number is 10";

  }

  return 0;

}

OUTPUT:




Note: In case where two or more conditions are true, then the statement associated with the top most  condition only gets executed. It is because the conditions are checked from top and if it is true then the rest of the conditions are not checked.


LOOP CONTROL STATEMENTS


What are Loops?

Loops are used to execute a block of codes repeatedly until a particular condition is true. In this, the statements in a function are executed sequentially. The statements within curly braces calledas loop body. After every execution, the condition is checked and if it is true, the loop body is executed again. 


There are three types of loops:



1. for loop

It allows execution of statements till the condition returns false. It is performed when the exact number of times of execution of the loop body is known to us. 


Syntax:

for(initialization; condition; increment or decrement)

{

  //statement

};


 Example:

//C++ program to demonstrate for loop

#include<iostream.h>

#include<conio.h>

using namespace std;

int main()

{

  int i;

  for (i=10; i>0; i--)

  {

    cout<<i<<" ";

  }

  cout<<"\nUsing for loop.";

  return 0;

}

OUTPUT:




In the above example, initialization is done once, then the condition is checked and if it is true, the loop body gets executed and then decrement operator is executed. Then again, the condition is checked and loop body gets executed for true condition and it continues till the condition return false. 


2. While loop

It repeats the statement while the condition is true.


Syntax:

while(condition)

{

  //statement

}


 Example:

//C++ program to demonstrate while loop

#include<iostream.h>

#include<conio.h>

using namespace std;

int main()

{

  int i=10;

  while(i>0)

  {

    cout<<i<<" ";

    i--;

  }

  cout<<"\nUsing while loop.";

  return 0;

}

OUTPUT:




3. do_while-loop

In this, the condition is checked at the end, after executing the statement. If the condition is true then the loop body continues to get executed otherwise it will come out of the loop.


Syntax:

do

{

  //statement

}

while(condition);


 Example:

//C++ program to demonstrate do-while loop

#include<iostream.h>

#include<conio.h>

using namespace std;

int main()

{

  int i=10;

  do

  {

    cout<<i<<" ";

    i--;

  }

  while(i>0);

  cout<<"\nUsing do-while loop.";

  return 0;

}

OUTPUT:





Note: In do-while loop, the loop body gets executed at least once whether the condition is true or not.


CASE CONTROL STATEMENTS

These are used in order to execute only specific statements. The various case control statements are:


The switch statement

The switch statement is used to test the equality of a variable against a set of values, each of which is called as case. When the variable is matched with a case then the statement associated with case gets executed. It is a substitute for long if statement.


Syntax:

switch(expression)

{

  case 1:

     statement1;

     break;

  case 2:

     statement2;

     break;

   case 3:

      statement3;

      break;

   default:

      statement4;

}


Explanation of the above keywords:

i. expression: It is a variable for which comparison is made.

ii. case: It has an integer value after the case keyword. It can also use characters. Its value is compared with the expression.

iii. break: When the case value is equal to the switch expression, then break keyword is used to come out of the switch and prevents further execution of the code.

iv. default: If none of the case value is matched with the switch expression, then the statement inside the default keyword gets executed. 


 Example:

//C++ program to demonstrate switch statement

#include<iostream.h>

#include<conio.h>

using namespace std;

int main()

{

  int n;

  cout<<"Enter a number from 1 to 5: ";

  cin>>n;

  switch(n)

  {

    case 1:

        cout<<"i = 1";

        break;

    case 2:

        cout<<"ii = 2";

        break;

    case 3:

        cout<<"iii = 3";

        break;

    case 4:

        cout<<"iv = 4";

        break;

    case 5:

        cout<<"v = 5";

        break;

    default:

        cout<<"Invalid number.";

        break; 

  }

  return 0;

}

OUTPUT:




Note: The break and default keywords are optional in the switch statement.


The goto statement

It is an unconditional jump statement. It alters the normal sequence of execution.


Syntax:

goto label;

.......

.......

label:


In the above code, label is an identifier. On true condition, control of the program jumps to label: and code gets executed after it. It is user supplied.

Note: goto and the identifier must be within same function for its execution.


 Example:

//C++ program to demonstrate goto statement

#include<iostream.h>

#include<conio.h>

using namespace std;

int main()

{

  int a = 5;

  if(a=5)

  {

    goto print;

  }

  else if(a>5)

  {

    cout<<"The number is greater than 5.";

  }

  else

  {

    cout<<"The number is less than 5.";

  }

  print: 

  cout<<"The number is 5.";

  return 0;

}

OUTPUT:





Note: The use of goto statement is not preferred because logically, it makes the program more complex.


The break statement

It is used to come out of a loop and terminates the loop based on some conditions. After terminating, the control of the program jumps to the first statement after the loop body.


 Example:

//C++ program to demonstrate break statement

#include<iostream.h>

#include<conio.h>

using namespace std;

int main()

{

  for (int i=10; i> 0; i--)

  {

    if(i== 5)

    {

      break;

    }

    cout<<i<<" ";

  }

  return 0;

}

OUTPUT:





The continue statement

It breaks (or skip) only one statement, based on the condition, in the loop and continues with the next execution of the loop body.


 Example:

//C++ program to demonstrate continue statement

#include<iostream.h>

#include<conio.h>

using namespace std;

int main()

{

  for (int i=10; i> 0; i--)

  {

    if(i== 5)

    {

      break;

    } 

    cout<<i<<" ";

  }

  return 0;

}

OUTPUT: