info@tutsfinder.in



Control Statements


Control Statements controls the flow of program execution.

These are keywords that are used for making a decision. In this, we will discuss about conditional statements and unconditional statements. These statements are used to control the flow of statements.

Conditional Statements

Conditional Statements are used when the execution of statements depends on one or more conditions.

Decision making statements :

• If

• If-else

• if-else-if

Example of if statement –


#include <iostream>
using namespace std;

int main() 
{
    int num;
    cout << "Enter an integer: ";
    cin >> num;

    // checks if the number is positive
    if ( num > 0) 
    {
        cout << "You entered a positive integer: " << num << endl;
    }
    return 0;

}


Output :

Enter an integer: 5
You entered a positive integer: 5


Syntax of if-else statement -

if(condition)
{
//statement if condition is true
}
Else
{
//statement if condition is false
}

Program to show how to use if else statement

#include <iostream>
using namespace std;

int main() 
{
    int num;
    cout << "Enter an integer: ";
    cin >> num;

    if ( num >= 0)
    {
        cout << "You entered a positive integer: " << num < endl;
    }
    
    else
    {
        cout << "You entered a negative integer: " << num << endl;
    }

    return 0;
}


Output :
Enter an integer: -23
You entered a negative integer: -23


Program of if else if statement –

#include<iostream>
using namespace std;
int main(){
   int num;
   cout << "Enter an integer number between 1 & 9999: ";
   cin >> num;
   if(num < 10 && num < =9){
       cout << "It's a one digit number";
   }
   else if(num < 100 && num > =1) {
      cout << "Its a two digit number";
   }
   else if(num < 1000 && num > =100) {
      cout << "Its a three digit number";
   }
   else if(num < 10000 && num > =1000) {
      cout << "Its a four digit number";
   }
   else {
      cout << "number is not between 1 & 9999";
   }
   return 0;
}


Enter an integer number between 1 & 9999: 15
Its a two digit number


Uncoditional Statements

Uncoditional Statements are not used to satisfy any conditions, they immediately move the control of program to another part

goto - Goto statement is used to take control of the program to any part of the program.

For Example -

statement1;
goto label;
statement2;
statement3;
label:
statement4;

In above example,when goto statement is encountered , it takes control to the place where label is defined in the program.

break - This statement is used to change the normal flow of the program. It is used to terminate the loop means break th ecurrnet flow of the program.

continue - Where continue statement encountered inside a loop ,control directly jumps to the begining of the loop of next iteration.