Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Semicolons in C++
A semicolon in C++ is used to terminate or end the statement; it tells the compiler that this particular instruction is completed.
According to the ISO C++ specifications, the lexical representation of C++ programs (breaking down code into small parts) is called tokens. Some of these tokens are punctuators, which are special symbols used to structure your code. The semicolon is one of these punctuators.
Example
Here is the following basic example code showcasing the working of a semicolon in C++.
#include <iostream>
using namespace std;
int main() {
int x = 5; // End of declaration statement
x = 10; // End of assignment statement
cout << x <<'\n'; // End of output statement
//OR
int y = 5; y = 10; cout << y;
return 0;
}
Output
10 10
Both the given codes are correct, returning the same output, ';' indicating the end of a particular instruction. The '\n' (newline character) is used to represent a new line, which makes the cursor start from the new line.
Semicolon is used in various parts of C++ to mark the end of statements, like in Control Structures (if, else, for, while, etc), functions, classes, structs, and namespaces.
