×
>
<

Aptitude

Control Structures

Introduction to control statements

In C++ language we have to perform a lot of operations and to do them we have a bunch of control statements that help us to perform specific tasks .

In C++ we can decide which statements we want to execute in our program and which statements and what to skip. This is called decision making. Most decisions are done on a condition basis.

When a particular condition arrives, you can execute the statements. Because these statements work with the conditions, they are also called conditional statements. And because they control execution in the statement program, they are also called control statements.

Control statement in C++

C++ program is the group of two or more statements, which is sequentially executed. But sometimes programmer may need to repeat any statements many times or change the sequence of program.

Which can be obtained by the use of control statements or control structure to controls the flow of the programm as required.

IF Statement
  • An if statement is used to make a comparison between two or more set of values, and if that condition or comparison is satisfied then the part of the code written in the if statement will get executed .
  • if statement block’s is defined by curly braces{}. When the condition is true then the statement given in this block is executed.
  • If the condition is false then skip the entire block.
if statement
Syntax of if statement
  
if(condition)
{
      //statements to be executed if condition is true.
}
  

Example :

  
#include "iostream"
using namespace std;
void main()
{
     int a=8;
     int b=5;        //checks if a is greater than b
     cout << "a is greater than b" << endl;
}
  
Output :
  
a is greater than b
  
if else statement

In some cases when if condition is not satisfied an if else statement will be the next statement that the compiler will now go to and execute if conditions matched.

if else statement is considered to be part of an if statement. But else block is added in it. The statement given in Else block is executed when the condition of if is false.

Syntax of if else statement
  
// Check whether an integer is odd or even

#include "iostream"
using namespace std;
int main() {
    int number;
    cout << "Enter an integer: ";
    cin >> number;

    // True if the remainder is 0
    if  (number%2 == 0) {
        cout << number << " is an even integer.";
    }
    else {
        cout << number << " is an odd integer.";
    }

    return 0;
}
  
output
  
Enter an integer: 7
7 is an odd integer.
  
else-if statement

if we want to put another condition in the middle of if and else, then we can achive this by defining else if block.

  
if(condition)
{     
      //statement to be executed if condition is true
}
else if(condition)
{  
    //statement to be executed if else if condition is true
}
else(condition)
{
    // statement to be executed if, neither if nor else if is true.
}
  

Example :

  
#include "iostream"
using namespace std;
void main()
{
   int a=5;
   int b=5;
   if(a>b)      //checks if a is greater than b
   {
       cout << " a is greater than b";
   }
   else if(a < b)   //checks if a is less than b
   {
       cout << " a is less than b";
   }
   else
   {
       cout << " a is equal to b";
   }
}
  
output
  
a is equal to b
  
There are a lot of other comparisons we can do in an if statement like
  
if(a==b)   //if a is equal to b
if(a!=b)  //if a is not equal to b
if(a==b && a>5)   //if a is equal to b and a is greater than 5
if(a==b || a>5)  //if a is equal to b or a is greater than 5
  
Switch cases ?

In a switch case we provide (or take input from the user) a key and give it to the switch and the switch compares that value with its cases and if it is equal it will execute that case and if no cases match then it runs a default case.

  
switch(value)
{    
   case (condition):
       //statement to be executed if condition is true
       break;
   case (condition):
      //statement to be executed if condition is true
      break;
   default:
     //statement to be executed if none of the case is true
}
  
Example :
  
#include "iostream"
using namespace std;
int main()
{
   int a=3;
   switch(a) //value of 3 is given to switch
   {
       case 1 : //checks if a is equal to 1
         cout << "One";
         break; //jumps out of all switch cases
       case 2 : //checks if a is equal to 2
          cout << "Two";
          break; //jumps out of all switch cases
       default :
          cout << "No values matched";
   }
}
  
Output
  
No values matched
    
Loop Statements

Sometimes we need to execute the block of code repeatedly while some condition evaluates to true. However, loop statements are used to execute the set of instructions in a repeated order. The execution of the set of instructions depends upon a particular condition.

Here, for loop is similar to C . It enables us to initialize the loop variable, check the condition, and increment/decrement in a single line of code. We use the for loop only when we exactly know the number of times, we want to execute the block of code.

Syntax :

  
for(initialization, condition, increment/decrement) {    
    //block of statements    
}
  

Example :

  
#include <iostream> 
using namespace std;  
int main() {  
         for(int i=1;i<=10;i++){      
            cout<< i <<"\n";      
          }       
    }
  
output
  
1
2
3
4
5
6
7
8
9
10
  
While Loop

The while loop is used to iterate over the number of statements multiple times.But it is used in case where we don't know the number of iterations in advance, it is recommended to use a while loop. Unlike for loop, the initialization and increment/decrement doesn't take place inside the loop statement in while loop

It is also known as the entry-controlled loop since the condition is checked at the start of the loop. If the condition is true, then the loop body will be executed; otherwise, the statements after the loop will be executed..

Syntax :

  
while(condition){    
    //looping statements    
}
  

Example :

  
#include <iostream>  
using namespace std;  
int main() {         
 int i=1;      
         while(i<=10)   
       {      
            cout << i << "\n";    
            i++;  
          }       
    } 
  
output
1
2
3
4
5
6
7
8
9
10
Do While Loop

The do-while loop is quite same like the while loop, insted it checks the condition at the end of the loop after executing the loop statements. When the number of iteration is not known and we have to execute the loop at least once, we can use do-while loop.

It is also known as the exit-controlled loop since the condition is not checked in advance. The syntax of the do-while loop is given below.

Syntax :

  
do{    
    //statements    
} while (condition);   
  

Example :

  
#include <iostream>  
using namespace std;  
int main() {  
     int i = 1;    
          do{    
              cout<< i <<"\n";    
              i++;    
          } while (i <= 10) ;    
}
  
output

1
2
3
4
5
6
7
8
9
10

Jump Statements

These statements are used to transfer the control of the program to the specific statements. In other words, jump statements transfer the execution control to the other part of the program. There are two types of jump statements in C++, i.e., break and continue.

C++ break statement

As the name suggests, the break statement is used to break the current flow of the program and transfer the control to the next statement outside a loop or switch statement. However, it breaks only the inner loop in the case of the nested loop.

The break statement cannot be used independently in the C++ program, i.e., it can only be written inside the loop control statements.

The break statement example with for loop

  
#include <iostream>  
using namespace std;  
int main() {  
      for (int i = 1; i <= 10; i++)    
          {    
              if (i == 5)    
              {    
                  break;    
              }    
        cout<< i <<"\n";    
          }    
}  
  
Output
  
1
2
3
4
  
C++ continue statement

Unlike break statement, the continue statement doesn't break the loop, whereas, it skips the specific part of the loop and jumps to the next iteration of the loop immediately.

The continue statement cannot be used independently in the C++ program like break , i.e., it can only be written inside the loop control statements.

The continue statement example with for loop

  
#include <iostream>  
using namespace std;  
int main()  
{  
     for(int i=1; i<7; i++){      
            if(i==5){      
                continue;      
            }      
            cout<< i << "\n";      
        }        
}    
  
Output
  
1
2
3
4
6