Hello All, I am new in this community. I am learning python and I want to know the difference between while loop, do while loop, and for loop? Can anyone tell me the difference with some basic examples with their syntax?
Hello @aartiyadavpersonal
This forum is aimed at software developers using Refinitiv APIs. The majority of the members of this forum and developers using Refinitiv APIs, the moderators on this forum are experts in the use of Refinitiv APIs.
For the programming language, coding questions like this one, you can post it on Stack Overflow Public Q&A which can help you get answers to your toughest coding questions.
For Loop
The 'for' loop allows Initialization, Evaluation, and "Action" stages all placed within the loop declaration. It also supports locally scoped iterator & variables (memory efficiency, safety), something other types of loops such as the 'while' and 'do..while' loop doesn't. In the case of 'while' and 'do..while' loop, Initialization needs to be placed outside, only Evaluation is placed in loop declaration, and "Action" is placed inside the loop body.
While Loop
The 'while' loop evaluates loop condition BEFORE it executes the instructions inside the loop body, on the other hand...
Do While Loop
The 'do..while' loop evaluates loop condition AFTER it executes instructions inside the loop body (at least once). √ The 'do..while' loop is most suitable for situations where you need the loop to run once, at least, before the condition is evaluated. e.g. prompt for verifiable input, ask user login info, showing a menu, etc.
To know more and understand it deeper with the examples, Checkout this post.