# Loop function question [duplicate]

I'm new to Mathematica. Below is the code I have trouble with.

Do[Print[
Subscript[C, k][x_, y_]: =
Subscript[q, 2 k - 1] + x*Subscript[q, 2 k] - (Subscript[q, l] + y*Subscript[q, l + 1])
],
{k, 3, 5}, {l, 3}]


In this loop, I want k=3 map to l=1, k=4 map to l=2, and k=5 map to l=3. However in this loop function, it loops through l=1,2,3 when k=3 and so on. So is there an automatic way or code to stop the certain looping?

-

## marked as duplicate by Mr.Wizard♦Jan 18 at 15:28

Quick fix would be to leave only one iterator, k and instead of l put k-2. –  Kuba Jan 14 '14 at 1:00
And Printing a function definition will give you Null. Not really useful. –  asterix314 Jan 14 '14 at 3:22
Cheers Guys. The reason I have k and l is that they will make my following programming much easier. Just curious is there other ways or code can handle that? Thanks again. –  user11726 Jan 14 '14 at 10:05

If you are aiming to use loop to define functions you could use MapThread:

MapThread[(Subscript[C, #1][x, y] :=
Subscript[q, 2 #1 - 1] +
x*Subscript[q, 2 #1] - (Subscript[q, #2] +
y*Subscript[q, #2 + 1])) &, {Range[3, 5], Range[3]}];'


Now you can test functions are defined:

Table[Subscript[C, j][a, b], {j, Range[3, 5]}]


This yields:

{-Subscript[q, 1] - b Subscript[q, 2] + Subscript[q, 5] +
a Subscript[q, 6], -Subscript[q, 2] - b Subscript[q, 3] + Subscript[
q, 7] + a Subscript[q, 8], -Subscript[q, 3] - b Subscript[q, 4] +
Subscript[q, 9] + a Subscript[q, 10]}

-
Cheers ubpdqn! That's exactly what I wanted. Much appreciated! –  user11726 Jan 14 '14 at 12:47