Why is a for-loop able to use a variable that isn’t defined yet? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why is a for-loop able to use a variable that isn’t defined yet?

10th Feb 2019, 4:28 PM
Konstancja Tanjga
Konstancja Tanjga - avatar
5 Answers
0
I think because the for loop uses a variable and if that specified variable is not defined yet, it will be defined only inside the loop.
10th Feb 2019, 4:38 PM
EoflaOE
EoflaOE - avatar
+ 2
Let's say you write: For letter in 'abcde': Then letter will not be undefined, but 'a' in first iteration, 'b' in second and so on.
10th Feb 2019, 4:47 PM
HonFu
HonFu - avatar
+ 1
Thank you. So it means that variable is defined by foor-loop at the beginning, when it starts. Each time initializing it to the current element of the loop iteration.
10th Feb 2019, 4:41 PM
Konstancja Tanjga
Konstancja Tanjga - avatar
0
You may see instances of: for (int i = 0; i>=10; i++){} in Java, or in any other programming languages. Which programming language you are using?
10th Feb 2019, 4:33 PM
EoflaOE
EoflaOE - avatar
10th Feb 2019, 4:34 PM
Konstancja Tanjga
Konstancja Tanjga - avatar