What is do-while loop?The do-while loop is a control flow statement that executes the code block at least once and then it executes the code block repeatedly, depending on the condition given at the end of the code block. The do-while loop is also known as "post test loop" as in this loop, condition is checked after the execution of loop once. There are some cases, where we want to execute the code block infinite times, thus creating an infinite loop. When such type of loop is created, the break statement is used that allows the termination from the loop. Nested loop: The nested loop is an inner loop within the outer loop. Inner loop is executed till the outer loop finishes its execution. General syntax:Algorithm of do-while loop
Difference between do-while loop & other loops (i.e for loop, while loop)The statements in do-while loop are executed atleast once then the condition is verified but in the case of other loops (for, while loop), the statements are executed only when specified condition is evaluated. Syntax of do-while loop is common in C, C++, java, php, perl, javascript, C#.
Syntax of do-while loop in RubyYou can see in the above scenario, block statements are executed atleast once then the specified condition is evaluated. If the condition is false, the block statements are executed again. Syntax of do-while loop in RustRust does not support do-while loop. It supports only three kind of iterative activities i.e. loop, while and for. Syntax of do-while loop in swiftSwift contains "repeat-while" block. In this loop, the block statements are executed atleast once then the condition is evaluated. If the condition is true, the statements are executed again till the specified condition is true Syntax of do-while loop in pythonPython does not support the do-while loop. It supports only two kind of iterative activities i.e. for loop and while loop. Important points to be noted:
Infinite LoopLet's see the simple program of usage of an infinite loop in respective languages: Program in CThis program creates an infinite loop. Until and unless, we press the key y, this loop continues. When we press the key 'y', this leads to the termination from the loop. Program in C++This program creates an infinite loop. Until and unless, we press the key 'y', this loop continues. When we press the key 'y', this leads the termination from the loop. Program in C#This program creates an infinite loop. Until and unless, we press the key ?Enter?, this loop continues. When we press the key enter, it leads to the termination from the loop. Program in JavaThis program creates an infinite loop and thus, prints 'javaTpoint' infinite times. Program in JavaScriptThis program creates an infinite loop and thus, prints 'javaTpoint' infinite times. Program in PerlThis program creates an infinite loop and thus, prints 'javaTpoint' infinite times. Program in PHPThis program creates an infinite loop and thus, prints 'avaTpoint' infinite times. Program in RubyThis program creates an infinite loop and thus, prints 'javaTpoint' infinite times. Program in SwiftThis program creates an infinite loop and thus, prints 'javaTpoint' infinite times. Output: javaTpoint javaTpoint javaTpoint javaTpoint javaTpoint javaTpoint javaTpoint javaTpoint javaTpoint javaTpoint javaTpoint javaTpoint javaTpoint javaTpoint javaTpoint javaTpoint javaTpoint javaTpoint .................... (infinite times) Usage of Nested loopLet's see the simple program of printing a number pattern in respective languages. Program in CIn this program, nested looping is used to create a number pattern. With each iteration of i, j is executed 'i' times. In 'C language', printf() function is used to print a pattern. Program in C++In this program, i is executed 5 times. Therefore, with each iteration of i, j is executed i times. In C++ language, cout object is used to print the pattern. Program in C#In this program, i is executed 5 times. Therefore, with each iteration of i, j is executed i times. In C# language, Console.Write() function is used to print the pattern. Program in JavaIn this program, i is executed 5 times. With each iteration of i, j is executed i times. In "java language", System.out.print() is used to print the pattern. Program in JavaScriptIn this program, i is executed 5 times. With each iteration of i, j is executed i times. In javascript, document.write() is used to print the pattern. Program in PerlIn this program, $i is executed 5 times. With each iteration of $i, $j is executed $i times. In perl, printf() function is used to print the pattern. Program in PHPIn this program, $i is executed 5 times. With each iteration of $i, $j is executed $i times. In PHP, echo keyword is used to print the pattern. Program in RubyIn this program, $i is executed 5 times. With each iteration of $i, $j is executed $i times. In ruby, print keyword is used to print the pattern on the screen. Program in SwiftIn this program, i is executed 5 times. With each iteration of i, j is executed i times. In swift, print function is used to print the pattern. Output: 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 |