Times in Scala

Find this useful? Support us: Star on GitHub 6
Category: Loop | Language: Scala

In Scala, the * symbol represents multiplication, and you can use it to perform times operation.

Here are some examples of using times in Scala:

1. Multiplying two numbers:

val a = 5
val b = 3
val c = a * b // result 15

2. Printing a message multiple times using a for-loop:

for(i <- 1 to 5) {
println("Hello World!")
}

Output:
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!

3. Creating an array with a specified number of elements:

val arr = Array.fill(5)(0)
// Output: [0,0,0,0,0]

4. Repeating a string multiple times:

val str = "Scala Rocks!" * 3
// Output: "Scala Rocks!Scala Rocks!Scala Rocks!"

In Scala, times operation can be performed with both numbers and strings. You can also use for-loops, arrays and other features to work with times.