Statements in C++

Video Tutorial
FREE
Statements and Expressions thumbnail
This video belongs to
C++ Course: Learn the Essentials
14 modules
Certificate

Overview

A computer program is a list of instructions to be executed by a computer. In a programming language, these programming instructions are called statements. C++ statements are the elements of programs that controls how and in what order programs are executed. The statements can either be a single line of code with a semicolon ; at the end or a block of code inside curly braces {}.

Types of statements in C++

C++ contains the following types of statements:

  • Labeled statements
  • Expression statements
  • Compound statements
  • Selection statements
  • Iteration statements
  • Jump statements
  • Try-Catch blocks
  • Declaration statements

Let's discuss each of these in detail in the subsequent sections.

Labeled statements

Labeled statements are used to direct the program's control to the given statement from a specific position.

There are multiple labels that can be applied to a statement. There are following three important labels in C++:

  • Target label for goto statement.
  • Case label in a switch statement - Handles all the possible values the variable in the switch statement can take.
  • Default label in a switch statement - The default action to take when the switch variable does not hold any value matching the cases that we defined.

Syntax:

Expression statements

Constants, operators, and variable are combined to make an expression. An expression is something that can be evaluated to a value. To create a value, an expression may have one or more operands and zero or more operators.

An expression statement has a semicolon ; at the end. Almost all the statements in C++ are expression statements. A few examples are assigning, declaring, function calls, etc.

Let's consider the following example:

Syntax:

Compound statements

A group of statements contained in curly brackets {} is a compound statement. When a single statement is required yet a set of many statements must be executed in order, we use a compound statement. Few examples are if , for loop etc.

Let's consider the following example:

Syntax:

Selection statements

A selection statement selects one of several possible control flows. It helps to provide a way to run code sections on a conditional basis.

There are following types of selection statements as follows:

if statement

If a statement executes the code inside its block if and only if the condition inside the if statement is true.

Let's consider the following example:

Syntax:

Nested if

Code:

if statement with an else clause

If the condition for if is true, then the code section inside if will be executed; otherwise, the code section inside else will be executed.

Let's consider the following example:

Syntax:

switch statement

In C++, the switch statement compares an expression to one or more cases. The statement associated with the 'case' that holds true for the provided expression is executed by the 'switch' statement.

Syntax:

Iteration statements

In C++, iteration refers to looping through the contents in a container. When you need to get to the contents of a container, this is the method to use (e.g., an array, vector, etc.).

while loop

In C++, the while statement is used to create a loop that executes code till the given condition is true. If the condition is false, the loop ends. Learn more about While Loop in C++.

Syntax:

do-while loop

In C++, the do-while statement is used to create a loop that executes a certain statement first (the do block). After the do-block is finished, it jumps to the while block. The do-block is executed repeatedly if the while block's condition is true; else, the loop stops.

Syntax:

for loop

The C++ statement for creating a loop includes the initialization, condition, and final-expression expressions. The loop is ended when the condition is false.

Syntax:

range for loop

This form of for loop iterates over all elements in range, with the declaration defining a variable that may take the value of any element in the range. Ranges are collections of items that support the begin and end functions, such as arrays, containers, and other types.

Syntax:

Jump statements

Jump statements enable one to modify a program's flow by jumping to a specific section of code.

In C++, jump statements are as follows:

break statement;

The break statement in C++ is used to stop the execution of a program.  The break statement is commonly used with loops to prevent them from executing once a condition is met.

Syntax:

continue statement

To skip a loop iteration in C++, the continue statement is widely used. When the program encounters the continue statement while completing the loop, it skips the rest of the statements in the block and jumps to the next iteration.

Syntax:

return statement

After a function has completed its execution, it returns control to the caller function or passes control to the operating system from the main function. The calling function resumes execution where the call was initiated. It has two syntaxes:

  • return statement with an optional expression It returns an expression, for example, return (a<b); this expression is optional.

    Syntax:

  • return statement using list initialization It returns an initialized list inside braces. For example return {1,2,4};

Syntax:

goto statement

goto allows us to jump to a certain location in the program. The target point is identified by a label, which is then supplied as an argument to the goto command. The colon(:) is used to declare the label.

Syntax:

Exception Handling Statements

The following are exception handling statements in C++:

Try..Catch

The try..catch statement in C++ is used to execute statements with a chance of failing. It's an error-handling strategy written in C++. The try statement describes the code block to be executed, whereas the catch statement deals with any errors arising during the code block's execution.

Syntax:

Throw

The throw statement in C++ is used to handle custom errors. The program finishes when the throw statement is met and jumps to the try-catch block (If the try-catch block is not present, the program terminates).

Syntax:

Declaration statements

In a program, the declaration statement establishes a name and associated data type.

Syntax:

Conclusion

  • C++ statements are the set of keywords used to control the program flow.
  • C++ contains various types of statements such as labeled statements,expression statements, compound statements, selection statements, etc.
  • if, else, and switch are some selection statements.
  • while, do while, and for loop are some of the iteration statements in C++.
  • The statements can either be a single line of code with a semicolon ; at the end or a block of code inside curly braces {}.