Need help with constructing Loops

Hi I have a problem when constructing For loops. My simple code are as below

For[ i=1 , i < 12 , i = i+2,  f[ i_ ] := i * 9 ]   

I want the result will be only exist when I enter

f[1] =9i, f[3]=27i,  f[5]=45i till f[11]=99i 

only

However, the results are for all values of i.

Anybody can explain why is that happening?

f[1] =9i, f[3]=27i, f[5]=45i till f[11]=99i

You can’t have a list that contain entries for only some index values. So, what should be in f[2] and f[5]? It has to contain something, even if it was Null or {}

Using For is not recommended. You can use Table, much better. Also make sure you know the difference between = and :=

You can’t have a list that contain entries for only some index values.

You can, but the definition needed has nothing to do with For loops at all.

In[2]:= f[i_ /; OddQ[i] && 1 <= i <= 11] := 9 i

In[3]:= f[2]
Out[3]= f[2]

In[4]:= f[3]
Out[4]= 27

In[5]:= f[11]
Out[5]= 99

In[6]:= f[13]
Out[6]= f[13]

What you wrote

For[ i=1 , i < 12 , i = i+2,  f[ i_ ] := i * 9 ]

is plainly wrong: if taken literally it reads for i=1 as f[1_]:= 1 * 9 and there is no possibility to have the number 1 as variable or placeholder.

But f[i] is not a list. These are indexed objects (also called Array in Mathematica). I was talking about normal lists, which I understood what the OP was trying to make, which is indexed by [[ ]] and not by []

MakingDefinitionsForIndexedObjects.html

You can define a list, say , then access its elements using , or
modify them using . This approach has a drawback, however, in that it
requires you to fill in all the elements when you first create the
list.

So in a list, one has to have value in each index. But not for indexed objects (or Array). If OP wanted array, then ofcourse they do not need to fill in each entry. Sometimes it is hard to know what OP is asking when code presented does not work at all.

But f[i] is not a list.

Of course not: The problem owner neither uses the word list nor does (s)he use list syntax like f[[i]]. Possibly an associative array is meant:

In[1]:= Clear[f]
For[i = 1, i < 12, i += 2, f[i] = 9 i]

In[4]:= Table[f[o], {o, 1, 12}]
Out[4]= {9, f[2], 27, f[4], 45, f[6], 63, f[8], 81, f[10], 99, f[12]}

Now problem owners code appears to contain only two incorrect characters: : and _. But that does not reproduce problem owners example. If (s)he really wants the i printed in the results, (s)he could do

In[5]:= Clear[f, i]
For[o = 1, o < 12, o += 2, f[o] = 9 o i]

In[7]:= Table[f[k], {k, 1, 12}]
Out[7]= {9 i, f[2], 27 i, f[4], 45 i, f[6], 63 i, f[8], 81 i, f[10], 99 i, f[12]}

This implicit i can be considered as bad style, so write
explicitly

In[20]:= Clear[f,x]
For[i = 1, i < 12, i += 2, f[i, x_] = 9 i x]

In[26]:= Clear[i]
Table[f[k, i], {k, 1, 12}]
Out[27]= {9 i, f[2, i], 27 i, f[4, i], 45 i, f[6, i], 63 i, f[8, i], 81 i, f[10, i], 99 i, f[12, i]}

if one does not clear the i it has value 13 and the result looks strange. Preferable

Clear[f]
f[i_ /; OddQ[i] && 1 <= i <= 11, x_] := 9 i x