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.

3rd Apr 2024, 1:05 AM
halu
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."
3rd Apr 2024, 4:03 AM
Brian
Brian - avatar
+ 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
3rd Apr 2024, 3:30 AM
Tibor Santa
Tibor Santa - avatar
+ 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ă‚’è„±ć‡șă—ăŸă™ă€‚
3rd Apr 2024, 7:22 AM
Wong Hei Ming
Wong Hei Ming - avatar
+ 3
Thank you Wong Hei Ming Rain too!!
3rd Apr 2024, 4:45 PM
halu
+ 2
Thank you everyone for the clear explanation!! It makes sense a lot now :) Brian Tibor Santa
3rd Apr 2024, 4:44 PM
halu
+ 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".
3rd Apr 2024, 4:22 PM
Rain
Rain - avatar