Semicolon is a punctuation mark (;) indicating a pause, typically between two main clauses, that is more pronounced than that indicated by a comma. In programming, Semicolon symbol plays a vital role. It is used to show the termination of instruction in various programming languages as well, like C, C++, Java, JavaScript and Python.
In this article, let us see the act of Semicolon in different programming languages:
Role of Semicolon in C:
- Semicolons are end statements in C.
- The Semicolon tells that the current statement has been terminated and other statements following are new statements.
- Usage of Semicolon in C will remove ambiguity and confusion while looking at the code.
- They are not used in between the control flow statements but are used in separating the conditions in looping.
for(initialization/declaration; condition; increment/decrements) { // body }
Role of Semicolon in C++:
- Semicolon is a command in C++.
- The Semicolon lets the compiler know that it’s reached the end of a command.
- Semicolon is often used to delimit one bit of C++ source code, indicating it’s intentionally separated from the respective code.
- Usage of Semicolon in C++ is after class and structure definitions, variable declarations, function declarations, after each statement generally.
Role of Semicolon in Java:
- Java uses Semicolon similar to C.
- Semicolon is a part of syntax in Java.
- It shows the compiler where an instruction ends and where the next instruction begins.
- Semicolon allows the java program to be written in one line or multiple lines, by letting the compiler know where to end the instructions.
Role of Semicolon in JavaScript:
- Semicolons in JavaScript are optional.
- In JavaScript, there is a process called Automatic Semicolon Insertion (ASI) which inserts a Semicolon whenever needed but not placed.
- Semicolons are also used to terminate the statements.
- Placing the new line next to the previous line will result in valid JavaScript which will not trigger ASI to correct it.
- So in conditional statements like if ..else and looping statements like while, for, do-while, doesn’t require Semicolon.
Role of Semicolon in Python:
- Python doesn’t use Semicolons but it is not restricted.
- In Python, Semicolon is not used to denote the end of the line.
- Python is called the simple coding language because there is no need to use Semicolon and if we even forget to place, it doesn’t throw an error.
- Sometimes Python makes use of Semicolon as line terminator where it is used as a separator to separate multiple lines.
Role of Semicolon in Perl:
- Perl language employs Semicolon after every line, except at the end of the block.
- Perl lets us omit the Semicolon because it can be used as a separator rather than a terminator.
- Every statement in Perl is terminated with Semicolon unless it is the last line of the program.
Role of Semicolon in SQL:
- Semicolon in SQL allows the user to execute the program in the same call by separating the statements in the database.
- SQL has a unique feature of adding Semicolon by default to terminate the statements.
- Semicolon is a statement terminator which is purely used to identify the end of a statement.
- Generally, by looking at the syntax, we can identify but using a Semicolon makes it more clear.
Role of Semicolon in Go language:
- Semicolon in Go language is used to separate the initializer, condition, and continuation elements.
- Semicolon is added as a terminator when the line’s last token is:
- An integer, floating-point, imaginary or string literal
- one of the keywords (eg. break, continue, return etc..,)
- an identifier.
- one of the operators and delimiters like ++, –, ), ], or }
Role of Semicolon in C#:
- C# makes use of Semicolon to get rid of ambiguity and confusion as its usage makes the code clear, structured and organised.
- Like other languages especially C and C++, C# also follows the same rules in the Semicolon application.
- The absence of Semicolon throws an error by the compiler which has to be rectified.
- It also lets the compiler know the end of the statement.
Role of Semicolon in Scala:
- Semicolon plays a vital role in all the programming language by marking the end of the statement.
- But Semicolon in Scala, not only marks the end of the statement but also the end of the expression.
- Scala’s syntax encourages clear and concise code, so it is necessary to use Semicolon properly whenever needed.
- Scala adapts different syntaxes like dot syntax, syntax using braces, syntax using parenthesis and syntax using empty line separator which replaces the use of Semicolon.
Role of Semicolon in PL/I:
- PL/I is a language which is a series of declarations and statements. So Semicolon is necessary to separate the statements to avoid ambiguity.
- Statements in PL/I should be placed in separate lines with Semicolon to improve the readability.
- In multi-line statements, Semicolon is used to separate the lines and in a single-line statement, Semicolon is used to terminate.
Role of Semicolon in Pascal:
- Semicolon in Pascal acts as statement separator i.e it separates two or more statements.
- Exactly one Semicolon should be used in separating two statements. But using more than one Semicolon (extra Semicolon) will raise an error.
- Unlike other languages, Pascal makes use of Semicolon in a different way. There is no need for a direct Semicolon before else in Pascal. Also, the last statement before the end of the code/program doesn’t need a Semicolon.
- Pascal needs Semicolon when statements are written in a sequence.
- Pascal doesn’t need Semicolon for a case or a block and also before the keyword.