Optimizing an if Program
In this lesson, we will learn how to use Scala's features to optimize a program using an if expression. You will also be introduced to the else expression.
We'll cover the following
The previous lesson gave you a general overview of the if
expression. In this lesson, our focus will be on how Scala allows you to write shorter code by consistently having result values.
Before we get started, let’s go over a couple of prerequisites for this lesson.
Input
The coding programs in this lesson require you to give input in order for them to execute successfully.
Before you press RUN, you must select the >_STDIN button located next to the RUN button which will provide an input field where you can type your input.
After you have typed the input, press RUN to execute the program.
Taking Input in Scala
Scala provides multiple methods that can be used to take external input from a user depending on the type of input required by the program. For this course, we will be using the readLine()
method which reads external input irrespective of its type.
The complete expression we will use is scala.io.StdIn.readLine()
where io
stands for input/output and lets the compiler know that either input or output is expected at this point. StdIn
stands for standard input letting the compiler know that the program specifically requires some sort of input from the user. readLine()
is a built-in method which reads input and converts it into a string.
That sums up the prerequisites. Let’s move on to the main focus of the lesson.
Using if
's Return Value
We will write a program which takes a file name from a user. If the file name provided by the user is empty (an empty string), the compiler will assign a default name to the file. If the file name provided by the user is not empty, the compiler will assign the user’s input to the file.
val input = scala.io.StdIn.readLine()var fileName = "default name"if (!input.isEmpty) {fileName = input}println(fileName)
Enter the input below
The code above first declares a variable by the name input
which stores the user’s input. Next, it declares a variable by the name fileName
which is set to a default value, default name
. From line 4 the if
expression starts, being provided with a condition that input
must not be empty. If input
is not empty, the compiler moves to line 4 which assigns the value of input
to fileName
. If input
is empty, line 4 will be skipped. The program ends with the compiler printing the value of fileName
.
The above approach requires us to declare a variable in which the if
expression will store the result.
Let’s look at the same program, but with a different approach.
val input = scala.io.StdIn.readLine()val fileName =if (!input.isEmpty) inputelse "Default Name"// Driver Codeprintln(fileName)
Enter the input below
The code above starts off the same way by taking input from the user, but immediately takes a different turn from the next line. The value being assigned to fileName
is now the if
expression itself; it is not assigned any prior value. Again, if input
is not empty, fileName
will be assigned the value of input
. Otherwise, it will be assigned the value, "Default Name"
which is being done on line 5. else
is a statement used with an if
statement which tells the compiler what to do if the if
condition is not fulfilled. In the previous examples we have done, the compiler simply leaves the if
expression if the condition is not fulfilled.
The code is optimized as now we can declare fileName
as an immutable variable using val
, unlike the first example where we had to declare it as a mutable variable using var
. Immutability is the functional way and provides a safety net which var
doesn’t.
We were only able to write this program because an if
expression in Scala has a return value which can be directly assigned to a variable.
The above code can also have the following form.
val input = scala.io.StdIn.readLine()val fileName = if (!input.isEmpty) input else "Default Name"// Driver Codeprintln(fileName)
Enter the input below
In the next lesson, we have a challenge for you to solve for yourself.