Statements in C++
A C++ statement is a single or multiple expression that can produce results. It’s a command to the computer to perform a task.
For Example,
cout<< "I'm a C++ Statement!";
The only action performed by this statement is to display the results “I’m a C++ Statement!” on the screen.
The whole statement means to insert a sequence of characters (in this example, the ‘I’m a C++ Statement!’ series of characters) into the standard output stream, and cout
is the name of the standard output stream in C++. The standard header file <iostream.h>
declares cout
.
Statement Terminator (;) in C++
Each C++ statement is concluded by a symbol called a statement terminator, which is a semicolon ;
. The semicolon ;
at the end of each statement in C++ specifies the distinction between statements.
In C++, you can put multiple statements on a single line, but it’s essential to separate them with semicolons. While placing each statement on its line enhances program clarity, the absence of semicolons to differentiate them can lead to errors.
Comments and Their Syntax in C++
Comments are pieces of source code that the compiler ignores. They have no effect other than to make the code more readable and understandable.
Their sole function is to allow programmers to add comments or descriptions to the source code. There are two sorts of comments in C++.
1. Single Line Comments in C++
The double slash //
symbol represents it. It discards everything from where the pair of slash signs //
is found to the end of the same line.
Syntax
// Your_Comment_Here
2. Multi or Double Line Comments In C++
The symbol /*....... */
is used to indicate double-line comments. The compiler ignores everything between these two symbols and considers it to be a blank space. When programmers require several line comments, this form of comment is used.
Syntax
/* Your_Comments_Here */
Example: C++ Program to demonstrate the concept of statements, statement terminator (;)
, and comments in C++
#include <iostream> using namespace std; int main() { cout << "Welcome! I'm a C Plus Plus Statement "; // This line is a C++ statement ending with a semicolon (;) /* This is a double-line comment. Everything that I write here will not be displayed in the output. */ return 0; }