← Back to C++ Course | Chapter 1: Introduction & Basics | Lesson 6 of 15

C++ Statements

A statement is the smallest complete instruction C++ executes, and a program is fundamentally a sequence of declaration, expression, and compound statements executed in order.
Syntax
cpp
type variable_name = value;   // declaration statement
variable_name = expression;  // expression statement
function_name(arguments);    // function call statement

What is a Statement?

A statement is the smallest complete instruction in C++ that the compiler executes, such as declaring a variable, calling a function, or evaluating an expression, and a C++ program is fundamentally a sequence of statements executed in order.

Example: What is a Statement?

cpp
#include <iostream>

int main() {
	int x = 5;             // declaration statement
	x = x + 1;              // expression statement
	std::cout << x;          // function call statement
	return 0;
}

Expression Statements

An expression statement consists of an expression, like an assignment or a function call, followed by a semicolon, and it's the most common kind of statement, executed purely for its side effect.

Example: Expression Statements

cpp
#include <iostream>

int main() {
	int total = 0;
	total = total + 10; // expression statement, executed for its side effect
	std::cout << total << std::endl;
	return 0;
}

Compound Statements (Blocks)

A compound statement, or block, groups multiple statements together inside curly braces, letting several statements be treated as a single unit wherever the language expects just one statement, such as inside an if or loop.

Example: Compound Statements (Blocks)

cpp
#include <iostream>

int main() {
	if (true) {
		int a = 1;
		int b = 2;
		std::cout << a + b << std::endl; // block treated as one statement
	}
	return 0;
}

Declaration Statements

A declaration statement introduces a new variable, specifying its type and name, and optionally an initial value, making that identifier available for use in the statements that follow.

Example: Declaration Statements

cpp
#include <iostream>

int main() {
	int score = 100; // declaration statement: type, name, initial value
	std::cout << score << std::endl;
	return 0;
}

Statement Termination with Semicolons

Every simple statement in C++, apart from compound statements enclosed in braces, must be terminated with a semicolon, which tells the compiler exactly where one instruction ends and the next one begins.

Example: Statement Termination with Semicolons

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	int a = 1; int b = 2;
	// Print to the console with cout
	std::cout << a + b << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Leaving out the semicolon at the end of a statement such as int x = 5, which causes a compile error on the next line.
  2. Putting a stray semicolon after if (x > 0);, which makes the empty statement the body and the following block always run.
  3. Assuming variables declared inside a { } block are still available after the closing brace, when they go out of scope there.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.