Q:

Scala program to implement infinite loop using while and do-while loop

belongs to collection: Scala Looping Programs

0

Here, we will implement an infinite loop using the while and do-while loop and print the "hello" message infinite times on the console screen.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program/Source Code:

The source code to implement infinite loop using while and do-while loop is given below. The given program is compiled and executed on the ubuntu 18.04 operating system successfully.

In the all below program, we used an object-oriented approach to create the program. We created an object Sample. We defined main() function. The main() function is the entry point for the program.

Implement infinite loop using while loop

// Scala program to implement infinite loop 
// using "while" loop

object Sample {  
    def main(args: Array[String]) {  
        while(true)
        {
            printf("Hello");
        }
    }
}  

Output:

HelloHelloHelloHelloHelloHelloHelloHelloHelloHello
....
infinite time

Explanation:

Here, we implemented the infinite loop using the while loop to print the "Hello" message infinite times on the console screen. we used the true value in the loop condition, that's why it will execute infinite times.

Implement infinite loop using do-while loop

// Scala program to implement infinite loop 
// using "do-while" loop

object Sample {  
    def main(args: Array[String]) {  
        do
        {
            printf("Hello")
        }while(true)
    }
} 

Output:

HelloHelloHelloHelloHelloHelloHelloHelloHelloHello
....
infinite time

Explanation:

Here, we implemented the infinite loop using the do-while loop to print the "Hello" message infinite times on the console screen. we used the true value in the loop condition, that's why it will execute infinite times.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Scala program to demonstrate the nested while and ... >>
<< Scala program to print numbers from 1 to 10 using ...