Why i need to add+index to alpha 1 in the conditon (lecture 32)

why we need to add this ?after all index value its 0 so if i ask for array locatoin in index0(state=nextstat[index]) its need to give me location 0 of the array without to add +index in the condition(i understand why its
not give me number 1 location atc but not why not 0),

the first exeple for loop in the image not give me 0 location although his value its 0
compared to second exemple that give me 0 location

Hi shlomo,

Welcome to our community! :slight_smile:

The for-loop is a little program. You define the control variable, its initial value, the condition and how you want to increase it in each iteration step.

Whenever the for-loop program gets started, it starts with the initial values.

Variables are containers for data. You can reuse them wherever you want because C# does not care about the meaning of names and human logic. It’s common to use a for-loop to iterate over something and to do something with the control variable inside the loop block.

In our case, we add the value of the control variable to Alpha1. In the first iteration step, the condition becomes (Alpha1 + 0), in the second one (Alpha1 + 1), in the third one (Alpha1 + 2), and so on.

If you cannot understand the aforementioned condition, click on KeyCode in Visual Studio, then press the F12 key. You should now see a list with all KeyCode elements, and behind the KeyCodes, you should see a value.

Did this answer your question?

See also:

tnx for reply but again , i know how its work and atc but i dont understand why if im not adding + index to if condition its not give me the first location in the array like second exmple in the atttached picture

In line 38, the index (0) is hard-coded. That’s why your code accesses the same “slot” in the array whenever this line gets executed.

In line 28, the index depends on the value of i in line 22. If you press the Alpha1 key, the if-condition is true in each iteration step. (Remember you can also look at the lecture code changes via the link in the Resources of each lecture.)

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms