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 Answers
+ 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をiに割り付けて印刷する。次回は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をiに割り付けて印刷する。次回は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をiに割り付ける。
もしiは0より大きいなら、iを印刷して、そしてiを3にひく。
結果は10,7,4,1を印刷した。
最後のターンにiは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".