In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces the reasons why Python does not design do-while loop structure, which has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.
Do-while is a basic loop structure in some programming languages, such as CCompact +, C #, PHP, Java, JavaScript, and so on.
Its core semantics are: first execute the loop body code, and then execute the conditional statement once; if the conditional statement is judged to be true, then continue to execute the loop body code, and execute the conditional statement again; until the conditional statement is judged to be false, then jump out of the loop structure.
The flow chart is as follows (Java example):
/ / print digits less than 20: public class Test {public static void main (String [] args) {int x = 10; do {System.out.print ("value of x:" + x); System.out.print ("\ n");} while (x < 20);}}
Python does not support the do-while structure, and "do" is not a valid keyword.
So why doesn't Python provide this grammatical structure, and what are the design considerations behind this status quo?
Before answering this question, let's think carefully about what problems do-while grammar can solve and what benefits can be gained from using this structure.
The most obvious benefit is that the do-while syntax guarantees that the loop body code will be executed first.
It may not be used in many scenarios, but unlike the "conditional pre" idea of ordinary while loop or for loop syntax, it embodies a kind of "conditional post" programming logic and a common way to control loops.
Their relationship seems to be a bit like the difference between iTunes + and + + I operations in languages such as CumberCraft +, which may be more efficient in some special situations.
In addition to this feature, the biggest application scenario of this structure is actually the special use of do {.} while (0) in CCharpy Cure +. This can be found in the source code of many open source projects, such as Linux, Redis, CPython interpreters, and so on.
The number 0 here indicates a Boolean value of False, which means that the loop is executed only once and then jumps out.
Isn't it weird to write like this? The so-called "loop" generally means that the program will be executed many times, but do {...} while (0) only needs it to be executed once, which seems a bit superfluous at first.
This writing method is mainly used in the definition of macro function, which can solve the compilation problem of macro code block and make the code reasonably divided into blocks according to our intention.
In addition, do {.} while (0) combined with break can also achieve a very elegant jump control effect.
In the following example, steps 1, 4, and 5 require that it must be performed, while step 2 depends on the execution result of step 1, and step 3 depends on the execution result of step 2.
Do {/ / execute step 1 if (condition 1 failed) {break;} / / execute step 2 if (condition 2 failed) {break;} / / perform step 3 if (condition 3 failed) {break;}} while (0); / perform step 4 jump / perform step 5
In this scenario, we really only need to do it once in order. The structure of do-while is clear, avoiding the situation of nesting multiple layers of conditions or setting a lot of extra tags.
Finally, at the assembly level, do-while is closer to the logic of assembly language than while and can save on instructions, which is an optimized way of writing in the past era of low memory.
After analyzing the benefits of do-while, let's get back to the topic: why doesn't Python need to design do-while loop syntax?
First of all, Python is too far from the underlying application programming to worry about the optimization of assembly instructions, and it does not involve the use of macros.
As for the difference between "conditional pre" and "conditional post", it doesn't really matter much, and because Python uses concise and elegant indentation plus colon syntax to divide code blocks, the literal translation of do-while syntax will look weird (note that there is nothing else after the conditions of literal translation of while):
Do: passwhile False
If you want to introduce new grammatical features, you must follow established styles and habits. The literal translation of do-while structures in other languages into Python is certainly not appropriate.
In fact, in 2003, there was a PEP proposal to add do-while syntax support to Python:
PEP-315 Enhanced While Loop
The PEP proposes to add an optional do clause to support the extension of the while loop like this:
Do: while:
This is not a simple translation from other languages to Python. Its while statement retains the indentation of Python and does not cause abrupt results in literal translation.
Plus the optional else clause that is already supported by the while loop itself, the complete syntax structure of while looks like this:
While_stmt: ["do": "suite]" while "expression": "suite [" else "": "suite]
(PS. In the next article in this series, we will explain why Python supports while-else syntax)
That is, while leaving the original while loop syntax unchanged, PEP-315 proposes to support the use of an optional do clause before while.
The do clause is executed only once, and when break appears in it, it jumps out of the entire do-while loop; when continue appears in the do clause, it jumps out of the do clause and goes into the conditional judgment of while.
With the do clause, it is easy to achieve the jump control effect of do {...} while (0).
However, this PEP has met with opposition from some core developers.
The objection is that there is no need to introduce new keywords and syntax, and the same function can be well achieved by using only the existing syntax:
While True: if not: break
Guido van Rossum, the father of Python, also disagreed. His original words were:
Please reject the PEP. More variations along these lines won't make the
Language more elegant or easier to learn. They'd just save a few hasty
Folks some typing while making others who have to read/maintain their code wonder what it means.
In a simple translation, this do-while syntax does not make Python more elegant and easy to use, but creates a burden of understanding to read / maintain the code.
From a personal point of view, I also disapprove of introducing the optional do-while syntax of PEP-315, although it is a little more flexible and elegant than the fixed form of do-while structure.
Finally, to sum up, do-while, as a common loop structure, has been used in other languages, and it has even developed the typical use of do {.} while (0). However, several problems that do-while can solve either do not exist in Python (macro definitions, assembly instructions), or there is already a more appropriate and low-cost implementation (jump control).
Thank you for reading this article carefully. I hope the article "what are the reasons why Python does not design do-while loop structure" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.