In programming, a statement represents a single instruction that a computer follows to perform a task. A C++ program consists of multiple statements that guide the execution of operations in a structured manner.
Writing a Statement in C++
A simple statement instructs the compiler to display text on the screen:
cout << "Rev up your engines!";
Each statement must end with a semicolon (;
). If omitted, the compiler will generate an error:
Example (Incorrect):
cout << "Rev up your engines!"
Error: expected ';' before 'return'
Executing Multiple Statements
Most C++ programs contain several statements that execute sequentially—meaning each statement runs in order from top to bottom.
Example:
#include <iostream> using namespace std; int main() { cout << "Rev up your engines!" << endl; cout << "Time to hit the road!" << endl; return 0; }
How It Works:
In the example above, three instructions are executed one by one:
- The first statement prints
"Rev up your engines!"
on the screen. - The second statement outputs
"Time to hit the road!"
. - The third statement (
return 0;
) signals that the program has successfully completed execution.