ADVERTISEMENT
ADVERTISEMENT

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

Here, we are going to learn how to implement infinite loop using while and do-while loop in Scala programming language?
Submitted by Nidhi, on May 06, 2021

Problem Solution:

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.

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.

Scala Looping Programs »


ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.