Rarely used loops

WHILE and DO-WHILE Loops

Disclaimer : The content of this blog are my views and understanding of the topic. I do not intend to demean anything or anyone. I am only trying to share my views on the topic so that you will get a different thought process and angle to look at this topic.

Most programming languages provide options for looping conditions i.e. running set-of code till a given condition is true. We have FOR, WHILE and DO-WHILE loops. But we mostly use the FOR loop for most of the use cases. WHILE and DO-WHILE loops are left out. Are we really using a work around of misusing FOR loop for everything ? Are there any use cases wherein we could fit in WHILE and DO-WHILE loops ?

The differentiating factors of these loops are mentioned below. Let's check them once :

We know these basics but the question is :- when do we really use a WHILE or DO-WHILE loop ? We mostly use a FOR LOOP. Probably we are dealing with data mapping and conditional processing. You fetched some records from backed and want to map it to DTO then a simple for loop is good. 

WHILE LOOP use cases

DO-WHILE LOOP use cases

The WHILE and DO-WHILE LOOPS are useful in these and more such use cases but we generally use FOR LOOP with some workarounds which makes code difficult to read and understand. Software code is to be maintained and upgraded for years hence improving code readability is important. Keep reading your same LOOPING code again and again to find if the correct LOOP is used for the logic you want to implement. Remember basics of the different LOOPs provided in a programming language.