Statements in C++

A statement is a fragment of a program. It may be a single line of code always ended by a semicolon (;) or a block of code that determines how and in what order program is executed. In this tutorial, we will be learning various types of statements in C++ with examples.

Types of statements in C++

  • declaration statements
  • selection statements
  • iteration statements
  • expression statements
  • compound statements
  • jump statements
  • exception handling statements, etc.

Declaration Statements in C++

Declaration statement introduces a name along with its data type in a program

int main()
   int a; // a is declared here of type int
   string b; // b is declared here of type string
   return 0;


Selection Statements in C++

Selection statements choose a section of code to execute on the basis of the outcome of the test it performs. If the test evaluates to true it execute a section of code. And if the test evaluates to false it choose another section of the program if available or does nothing. Examples of selection statements are if, if…else…, and switch, etc.

int main()
  int a, b, c;

  // if a and b are equal then c = a+b
  // otherwise c = a into b
     c = a+b;
     c = a*b;

  return c;


Iteration Statements

These statements repeatedly execute a block of code of a program until a condition(s) is(are) satisfied. Examples of Iteration statements are while loop, do-while loop and for loop, etc.

int main()
  int num = 5;
    cout << num << " " ; //this statement is executed untill 
                         //condition num > 0 is satisfied
     num -= 1;
  return 0;
Output: 5 4 3 2 1

Expression Statements

These statements are those which evaluate an expression, print output on the console window, take input from console window, assign some values to variables or call functions. If an expression is missing from the statement only semicolon (;) is there then this type of statement is called a null statement.

int main()
  int a, b;

     ;              //null statement
     int c = a/b; //expression statement

  return 0;    


Compound Statements

A compound statement is a group of statements enclosed in curly braces. We use a compound statement when a single statement is expected but for this a group of multiple statements needs to be executed in a sequence.

if (num >= 1) // start of if statement
{             // start of if block
  int a = 10; // declaration statement
  cout << a << endl; //expression statement
}                    //end of if statement and if block

variables declared in a block (say if block, while block, etc.) can be used within the block, not outside of the block.

Jump Statement

Jump statements are used to transfer control from one location of the program to another location within a function or return control from the function. Examples of jump statements are break, continue, return and goto, etc.

int sum(int a, int b)
    return a+b ; //return statement

#Jump Statement.

Jump statements are generally used to transfer the flow control unconditionally.

The following are the Jump statements provided by C++.

  1. break statements.
  2. continue statements.
  3. return statements.
  4. goto statements.

Exception Handling Statements

Exception handling statements are Try, Catch, and Finally. These three statements play a role in exception handling. Try statement identify the exception and throw it. Catch block catch exception and finally block does the default work when an exception occurs.

try { statement(s)}

catch (exceptiontype name) { statement(s)}
finally { statement(s)}

Leave a Reply

Your email address will not be published. Required fields are marked *