Start learning free

LESSON 120 OF 348 · WHILE LOOPS & FOR LOOPS

For Loops: Printing Numbers in Scala in Scala

Lesson

We can achieve many different outcomes with for loops.

It's all about tuning the initialization, condition and iteration statements.

For example, we can print all numbers from 2 through 6:

for (i <- 2 to 6) {
	println(i)
}

The output of this code is:

2
3
4
5
6

Let's break down this code:

  • The range starts at 2.

  • to 6 means the range includes 6.

  • The range increases by 1 by default.


Assignment

Let's print all numbers from 3 through 10 using a for loop.


Hint
Look at the examples above if you get stuck.


STEP 1 OF 2
    Ctrl + Enter to check

    Printing Numbers 3 in Scala

    Keep going with ScalaAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free