Skip to content Skip to sidebar Skip to footer

Statements in C++

In c++, we all have written many programs till now. But do you know that each part of that program separately makes a specific type of statement, which collectively comes under the header of Statements. Now, let us dive into learning about this part of the ocean.

What are Statements in C++?

A statement is a fragment or a part of a whole program. It may consist of identifiers, names, literals, operators, and parentheses. Each statement has a role and can perform different functions depending on its type.

Types of Statements in C++

In C++, we have about 8 types of Statements that are:

  1. Declaration Statements
  2. Selection Statements
  3. Iteration Statements
  4. Expression Statements
  5. Null Statements
  6. Compound Statements
  7. Jump Statements
  8. Exception Handling Statements

Let’s Learn about them in detail.

Declaration Statements

From the beginning of the course, you might have noticed us using the word declare or declaration. Well, what we meant by that was to name any variable or introduce any variable or constant, etc., with a data type as a prefix is known as a declaration. And such statements where a declaration is present are known as declaration statements.

Example:

int main()
{
 int live; // live is declared here of type int
 string life; // life is declared here of type string
 return 0;
}

Selection Statements

As the name suggests, these are the set of statements from which we have to select our choice based on a condition. If the condition turns out to be true, then the statement related to it will run. If the condition turns out false, then its statements will not run. The other statement will execute if we have more than 2 sets of statements from which we can choose. 

Some selection statements are if, if…else, switch, etc. We will learn about them in detail later.

Example

#include <iostream>
using namespace std;

int main ()
{
  int vacation;
  cout << "Number of vacations in a year: " ;
  cin >> vacation;

  if (vacation <= 2)
    cout << "Mind is peaceful." << "\n";	// if vacation is less than or equal to  2 times a year
  else if (vacation > 2)
    cout << "On vacation, No time for it.";	// if vacarion is more than 2 times a year
}

Output

Mind is peaceful. // If the input is less than or equal to 2.
On vacation, No time for it. // If the input is more than 2.

Iteration Statements

Iteration means repetition. In c++, iteration statements help a code block repeat itself like a loop unless the conditions are satisfied, or the loop is terminated or ended. 

Some Iteration statements are for loop, while loop, do-while loop, etc., which we will learn later.

Example

#include <iostream>
using namespace std;

int main()
{
    int icecream = 5;
    while (icecream > 0)// till amout of icecream is less than 0
    {                    
       cout << icecream << "\n"; // display the amout of icecream left
       icecream--;               // decrease the amount of icecream
    }
}

Output

5
4
3
2
1

The output is from 5 to 1 because after 1 gets printed, the amount of icecream left is 0. And as the condition gets satisfied, the loop gets terminated. Hence, the statements afterward do not get implemented.

Expression Statements

Expression statements are those which evaluate an expression. Like x=2 is an expression but x=2; is an expression statement.

Example

#include <iostream>
using namespace std;

int main()
{
    int headphones = 2; // expression statement
    int mobile = 4;     // expression statement
    int Total_electronics;

    Total_electronics = headphones + mobile; // expression statement
    cout << Total_electronics;
    return 0;
}

Output

Null Statements

When there is no expression in an expression statement, it is known as a null statement. But still, it consists of a semi-colon.

Example

#include <iostream>
using namespace std;

int main()
{
    int anum, bnum;

    if (anum > 0)
        ; // null statement
    else
        int cnum = anum / bnum; // expression statement

    return 0;
}

Output

Nothing will be displayed as if condition is a null statement. It simply means there is no statement in the code block of if condition.

Compound Statements

Statements are known to be compound statements when we enclose a group of statements to run in a sequence in curly braces({}).

Example

#include <iostream>
using namespace std;

int main()
{
    int headphones = 2;
    int mobile = 4;
    int Total_electronics;
    if (headphones < 2)
        ;
    else
    { // compound expression
        Total_electronics = headphones + mobile;
        cout << Total_electronics;
    }
    return 0;
}

Output

6

Jump Statements

Jump statements are known as jump statements as they help transfer the control of one location to another within the same part or function of the program or return the control from the function

Some of the jump statements are continue, return, break, etc.

Example:

case 1: cout<< “this is case 1”

break;

after the break statement, the program terminates in this case.

Exception Handling Statements

These are the statements that help in handling any exception that arises. We do this with the help of three statements: try, catch, and finally. try statements helps in determining the exception and throwing them as well, catch helps in catching the exception and finally helps in running the statements outside this try and catch block. We will go into detail later.

Syntax

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

For detailed explanation about try-catch block refer to this blog

CONCLUSION

In this blog, we’ve learned about another crucial topic and moving towards another step of learning  C++ programming, i.e., Statements. What they are, how to use them, what their types are, and I think many more questions must have been cleared—hoping to learn many more related topics shortly.

As you all know, GeekonPeak has just begun and is looking for some loyal supporters so we, the new-gen geeky friends, can grow and create more comprehensive and easy-to-grasp courses for you (for free). Follow us on FacebookInstagram & Twitter as well. Also, don’t miss our newsletter subscription. We provide exclusive blogs, tips, tricks, and advice to program efficiently and market smartly. We also promote some of our partners with excellent experiences to share with you. Until next time 🙂 

Leave a comment

Sign Up to Our Newsletter

Be the first to know the latest updates