Basic Programming Constructs – Scala

Let us understand basics of programming using Scala.

  • Declaring Variables
  • Basic Programming Constructs
  • Code Blocks

Declaring Variables

As part of this topic we will see details about declaring variables. We will also look at data types. To explore Scala you should primarily use your laptop or desktop.

We will use Scala REPL to explore variables in scala.

  • val or var (immutable vs. mutable)
    • val is immutable
    • var is mutable
  • Implicit data types or Type inference
  • Explicit data types
  • There is no distinction such as primitive types and classes for primitive types
  • In Scala all primitive types are nothing but classes. Complexity is hidden from developers.
  • Primitive types are better in performance compared to classes for primitive types
  • When you specify classes for primitive types in Scala, if the variable have to behave like primitive type Scala compiler will take care of it
  • Here are the basic data types
Byte 8-bit signed two’s complement integer
(-2^7 to 2^7 – 1, inclusive)
Short 16-bit signed two’s complement integer
(-2^15 to 2^15 – 1, inclusive)
Int 32-bit signed two’s complement integer
(-2^31 to 2^31 – 1, inclusive)
Long 64-bit signed two’s complement integer
(-2^63 to 2^63 – 1, inclusive)
Char 16-bit unsigned Unicode character
(0 to 2^16 – 1, inclusive)
String a sequence of Chars
Float 32-bit IEEE 754 single-precision float
Double 64-bit IEEE 754 double-precision float
Boolean true or false

Task

  • create val a that is an integer value 42
  • create val b that is a long value 42
  • create val c that is a float value 42.0
  • create val d that is a double value as multiplication of a, b, c
  • create val e that is a short value 42.0
  • create val f that is a byte value 42.0
  • create val g that is a hex of number (42) in base 10

Basic Programming Constructs

As part of this topic we will see core language constructs.

Let us see basic programming constructs, such as if else, while loop, for loop etc. As any programming language, Scala also support

  • if condition
  • ternary operator
  • while loop
  • for loop
  • Arithmetic operations
  • Boolean operations
  • and more


However we do not use for loop as extensively while dealing with collections, we will see it later.

Task 1 – Factorial

Develop a program which will print factorial of a given number

Task 2 – Fibonacci

Develop a program which will generate number of elements in a Fibonacci series

Code Blocks

As part of this topic we will see fundamental of programming – Code blocks

  • Any thing that comes in the middle of { } is considered to be block of code
  • A block of code can be assigned to a variable. Code will be evaluated first and then the value will be returned to variable
  • It can also be assigned to functions – next topic
  • Blocks can be nested
  • Each line of code need not be ended with ; as in java. ; is optional
  • But if you want to write more than one expression in one line then each expression should be ended with ;
  • In blocks, the last statement of the code-block becomes the return value

Expression vs. Statement

  • Expressions are some code that yields a result. You can also say that an expression evaluates to a result or results in a value.
  • Statements are some code that does some action. This action is also called as a side effect. They do not return anything.
  • In scala, many constructs like code-blocks, if-else, method bodies are expressions
  • Expressions always return a value.
  • The value of c in the below code snippet from gist is the value of i – j
  • The type of the last statement becomes the type of the target variable
  • Data type of sqr in the below code snippet from gist is boolean