Approach 1: Using for loop (for-each) model
for(ele <- primesArray) print(s"$ele ")
scala> val primesArray = Array(2, 3, 5, 7, 11)
val primesArray: Array[Int] = Array(2, 3, 5, 7, 11)
scala> for(ele <- primesArray) print(s"$ele ")
2 3 5 7 11
Approach 2: Using for loop by accessing index.
scala> for(index <- 0 until primesArray.length) {
| val ele = primesArray(index)
| print(s"$ele ")
| }
2 3 5 7 11
Approach 3: Using while loop.
scala> var index = 0
var index: Int = 0
scala> while(index < primesArray.length){
| val ele = primesArray(index)
| index += 1
| print(s"$ele ")
| }
2 3 5 7 11
Approach 4: Using do-while loop
scala> var index = 0
var index: Int = 0
scala> do{
| val ele = primesArray(index)
| index += 1
| print(s"$ele ")
| }while(index < primesArray.length)
2 3 5 7 11
No comments:
Post a Comment