Simple if statement

                        if(expression is true)
                        {
                                    action1;
}
                        action2;
                        action3;
Example for if

#include <iostream> 
using namespace std;
 
int main ()
{

 

   if (x == 100)

{
   cout << "x is ";
   cout << x;
}
            }

The if …… else statement

if (expression is true)
{
            action1;
}
else
{
            action2;
}
action3;

Example for if….else

#include <iostream> 
using namespace std;
 
int main ()

{
if (x > 0)
  cout << "x is positive";
else if (x < 0)
  cout << "x is negative";
else
  cout << "x is 0";
           }

 

The switch statement

            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;

#include <iostream> 
using namespace std;
 
int main ()
{
   int x;

                        cout<<”Enter the value for x”;
                        cin>>x;

     switch (x) {
 

  case 1:
  case 2:
  case 3:
    cout << "x is 1, 2 or 3";
    break;
  default:
    cout << "x is not 1, 2 nor 3";
  }

 

The do-while loop

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;

Example of do-while loop

#include <iostream> 
using namespace std;
 
int main ()
{
   unsigned long n;
  do {
    cout << "Enter number (0 to end): ";
    cin >> n;
    cout << "You entered: " << n << "\n"; 
    } while (n != 0);
  return 0; 
}

 

The While Statement

The while statement is also a loop structure, but is an entry-controlled one. The syntax is as follows:

 

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

Example of while loop

#include <iostream>
using namespace std;
 
int main ()
{
  int n;
  cout << "Enter the starting number > ";
  cin >> n;
 
  while (n>0) {
    cout << n << ", ";
    --n;
  }
 
  cout << "FIRE!\n";
  return 0;
}