Breaking News: Grepper is joining You.com. Read the official announcement!
Check it out

Optimizing an if Program

Sumit Rawal answered on May 23, 2023 Popularity 1/10 Helpfulness 1/10

Contents


More Related Answers

  • Performance optimization
  • optimize python code
  • optimization function
  • What PCA optimizes?
  • Program optimization
  • Optimizing cold start times
  • Optimize loops
  • make this code its highest optimisation

  • Optimizing an if Program

    0

    Input

    Taking Input in Scala

    Using if's Return Value

    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. 

    Popularity 1/10 Helpfulness 1/10 Language scala
    Source: Grepper
    Tags: scala
    Link to this answer
    Share Copy Link
    Contributed on May 23 2023
    Sumit Rawal
    0 Answers  Avg Quality 2/10


    X

    Continue with Google

    By continuing, I agree that I have read and agree to Greppers's Terms of Service and Privacy Policy.
    X
    Grepper Account Login Required

    Oops, You will need to install Grepper and log-in to perform this action.