Semicolons in Scala

In this tutorial, we will learn about the semicolon, the use of semicolon in Scala with example. Submitted by Shivang Yadav Last updated : April 02, 2023

What is semicolon (;)?

A semicolon or semi-colon (;) is a punctuation mark in programming, it is used to separate multiple lines of code. It is common in major programming languages like C, C++, Java, Pascal. In modern programming languages like Python, Scala.

Use of semicolon (;) in Scala

The semicolon is treated as the end of line statement. It treats it as the end of the expression, if not the expression can continue to the next line. But, in Scala, the end of the line is treated as the end of the expression. This means the user does not need to compulsorily use the semicolon (;) statement.

Syntax

Code with a semicolon :     var a : int = 3445; 
Code without semicolon:     var a : int = 3445

Example: Scala program to demonstrate the use of semicolons

object MyClass {
      def main(args: Array[String]) {
         println("This statement is executed with semicolon");
         println("This statement is executed without semicolon")         
      }
   }

Output

This statement is executed with semicolon
This statement is executed without semicolon

Code logic

The code here has two print statements one print statement ends with a semicolon and second without semicolon. Both lines are valid and print the given strings.






Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.