control statement and their types

Control statement

To execute the program according to any condition, skip a statement in the program or execute or execute multiple time, terminating the program under a particular condition such as the operation, we use the control statement in c ++.

In C ++ or in any other programming language, control statement has an important role. Control statement is the basic concept of a language. From which we do the code writing for a task.

The control statement in c ++ is divided into three parts which are as follows-

 

Selection/condition Statement

The selection statement contains one or more set of statements that are declared in the peranthesis bracket ({}) and execute only when each block expression value is true. This type of statement is also called decision making statement because it executes the statement when the expression value is true.

 

Type of Selection Statement in C++

Here is another one,

nested if-else

 

if statement

If the expression value is true so body of if is executed and false body of if skipped and execution goes to next statement, we can understand it with the help of the program given below.

SYNTEX:

if(expression/condition)
 {
    Body of if;
 }

Here is the flow-diagram,

if-statement

Example:-
In this program, we have been given the condition that if the user-input is less than 5, then body of if executed

#include<iostream.h>
#include<conio.h>

void main()
{
 clrscr();
 int x; // variable declaration

  cout<<"Enter number: ";
 cin>>x; 

  if(x<5) // expression
  {
    cout<<"Number is: "<<x; // Body of if
  }

getch();
}
OUTPUT:-
Number is: 4

explanation:-

in the program, input given by the user will be stored in variable x, and then expression value check in the if statement. Which is becoming true (4<5) So body of if will execute.

 

if-else statement

If the expression value is true so body of if is executed , and if it is false then the body of else (which can also be other if-statement) is executed.

SYNTEX:

if(expression/condition)
  {
    Body of if;
  }
else
  {
    Body of else;
  }

Here is the flow-diagram of if-statement,

 

if-else

Example:-
In this program, we have given condition that if the digit given by user is less than 5 then body of if executed and false, then body of else is execute.

#include<iostream.h>
#include<conio.h>

void main()
{
 clrscr();
 int x; // variable declaration

  cout<<"Enter number: ";
 cin>>x; 
 
   if(x<5) // expression
   {
     cout<<"number smaller then 5"; // body of if
   }
  else {
         cout<<"number greater then 5"; //body of else
      }

getch();
}
OUTPUT:
Enter number: 4 
number smaller then 5

 

 

nested if-else

When an if statement is the body of another if statement in the program then it is called nested if-else.

In this type of statement, first if body (outer if statement) is the second if statement. When the outer if expression value is true then execution goes to the inner if statement, but if false, then execution skips the body of the inner if statement. And the body of outer else is executed.

You can understand it below with the help of the program given below.

#include<iostream.h>
#include<conio.h>

 void main()
{
 clrscr();
 int x; 

  cout<<"Enter number: ";
 cin>>x;

  if(x<5) // outer if statement
  { // outer if body
     if(x<3) //inner if statement
      {
         cout<<"number greater than 3"; // inner if body
      }else {
              cout<<"number smaller than 3";//inner else body
          }
  }
 else{
      cout<<"outer else body execute"; // body of outer else
   }

  getch();
}
OUTPUT:
Enter number: 2
number greater than 3

Enter number: 5
Outer else body execute

 

Related Exercise

previous-typedef Data-Type in C++

next-switch Statement