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

Control Flow#

Sumit Rawal answered on February 14, 2022 Popularity 8/10 Helpfulness 1/10

Contents


More Related Answers

  • flowtype
  • What is a Workflow?

  • Control Flow#

    0

    Let’s look at the control flow of a while loop below.

    The above flow is showing that if the condition has been fulfilled, the compiler will execute the block of code and check the condition again. This loop will occur until the condition is not fulfilled in which case the compiler will exit the block of code.

    Syntax

    Before we see the while loop in action, let’s go over the syntax and see how to write a block of code with while using Scala.

    The syntax is pretty straight forward with a while followed by the condition to be checked in () which is further followed by curly brackets {} in which appears the block of code to be executed if the condition holds true.

    The condition must be an expression of type Boolean. If the condition is true, the code will execute. If the condition is false, the compiler will exit the code.

    while in Action

    In the example below, we want to print the numbers from 1 to 10 using a while loop.

    Popularity 8/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

    Closely Related Answers



    1
    Popularity 10/10 Helpfulness 2/10 Language whatever
    Source: Grepper
    Link to this answer
    Share Copy Link
    Contributed on Feb 14 2022
    Anxious Addax
    0 Answers  Avg Quality 2/10

    0

    Let’s look at the control flow of if.

    The above flow is showing that if the condition has been fulfilled, the compiler will execute the conditional code and if the condition has not been fulfilled, the compiler will exit that block of code without executing the conditional code.

    Before we see the if expression in action, let’s go over the syntax and see how to write a block of code with if using Scala.

    The syntax is pretty straight forward with an if followed by the condition to be checked in () which is further followed by the code to be executed if the condition is true.

    To make the code more readable, you can write the conditional code underneath the if expression and condition. In this case, surround the conditional code in curly brackets ({}).

    The condition must be an expression of type Boolean. If the condition is true, the code will execute. If the condition is false, the compiler will exit the code.

    if in Action

    In the example below, we want to change the first element of an array, but before we can do that we need to make sure the array is not empty.

    Popularity 8/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.