178k views
5 votes
Enter one or more words to complete the sentence. In the below code, should be written in the for loop, so that the output returned is the list of numbers 1 through 3, with each number printed on a new line. val numbers = listOf(1, 2, 3) for (_______) { println(num) }

User DritanX
by
8.1k points

2 Answers

0 votes

Final answer:

In the code, fill the for loop with 'num in numbers' to print each number from the list on a new line, using Kotlin syntax.

Step-by-step explanation:

In the provided code snippet, the missing part in the for loop should iterate over numbers. To achieve the desired output, where each number is printed on a new line, you can write:

for (num in numbers) {
println(num)
}

The for loop iterates over the list of numbers and the println function is called for each element, which prints the number with a line break after each one.

User TimiTao
by
7.5k points
1 vote

The correct words to complete the sentence are:

Kotlin

for (num in numbers) { println(num) }

In the given code, the "num" variable should be written in the for loop, so that the output returned is the list of numbers 1 through 3, with each number printed on a new line.

```kotlin

val numbers = listOf(1, 2, 3)

for (num in numbers) {

println(num)

}

```

In the corrected code, the variable "num" is declared in the for loop. It will iterate over each element in the "numbers" list and assign the current element to the "num" variable. The println function is then used to print each value of "num" on a new line.

When you run this code, the output will be:

```

1

2

3

```

Each number in the list will be printed on a separate line, as requested.

User Pseabury
by
8.6k points

No related questions found