0
Question about loop
Does anyone know this? Why are the words âforâ and âwhileâ used for loop? What do these words stand for? Thanks for you help.
6 Antworten
+ 6
Both words, "for" and "while", are plain English words, not acronyms. To native English speakers (such as myself) they make sense in the programming context, though they are an extremely concise version of natural language. I believe the conciseness was influenced by language used by telegraph operators. It is known that early digital codes were influenced by Morse Code. Telegraph operators used as few words as possible to save time because it took so long to transmit messages. Early computers had little memory and processing power, so languages had to be extremely concise.
I think of the 'for' loop to mean "iterate the following lines for the conditions in which..."
The expansion of 'while' is simpler. "While the condition is true, iterate the following lines."
+ 5
For all we know, they seem to have no meaning whatsoever, while widely used in many programming languages, they are just mumbojumbo in English. They follow more than 60 years of tradition of not meaning anything.
*sarcasm*
https://en.m.wikipedia.org/wiki/For_loop
https://en.m.wikipedia.org/wiki/While_loop
"The first programming language to introduce both for and while loops was ALGOL 60. It was developed in the late 1950s and early 1960s and served as a foundation for many modern programming languages." - chatgpt
+ 3
Assume you live in Japan, I try to explain it in Japanese, and use Python syntax as example (which you are studying.)
Syntax of for loop:
for i in range(10):
print(i)
Repeat the operation 10 times. On the first run assign 0 to variable i, next time assign 2, until the last which is 9.
ăăźæäœă10ćăçč°ăèżăăŸăă
ććă0ăïœă«ćČăä»ăăŠć°ć·ăăăæŹĄćăŻ1ăă2ăâŠ9ăŸă§ă
Another syntax of for loop:
for i in "01234":
print(i)
For the first execution, assign 0 to variable i. Next iteration assign 1, 2...4.
ććă0ăïœă«ćČăä»ăăŠć°ć·ăăăæŹĄćăŻ1ăă2ăâŠ4ăŸă§ă
Syntax of while loop:
i = 10
while i > 0:
print(i)
i = i - 3
Output: 10, 7, 4, 1
First we set i equals to 10.
When i is greater than 0, print variable i, then subtract i by 3.
On the last run i becomes -2, which is smaller than 0, the loop stop.
ćăă«ă10ăïœă«ćČăä»ăăă
ăăïœăŻ0ăă性ăăăȘăăïœăć°ć·ăăŠăăăăŠïœă3ă«ăČăă
ç”æăŻ10ïŒ7ïŒ4ïŒ1ăć°ć·ăăă
æćŸăźăżăŒăłă«ïœăŻ1ăć°ć·ćŸ3ăćŒăăŠăă-2ăă«ăȘăăŸăă
ă-2ăăŻă0ăăăć°ăăăăăwhile loopăè±ćșăăŸăă
+ 3
Thank you Wong Hei Ming Rain too!!
+ 2
Thank you everyone for the clear explanation!! It makes sense a lot now :) Brian Tibor Santa
+ 1
Wong Hei Ming ,
One correction, this one assigns str objects, not int objects, to i.
Another syntax of for loop:
for i in "01234":
print(i)
For the first execution, assign "0" to variable i. Next iterations assign "1", "2", "3", "4".