# For Loop

For In Element即loop目標以element為主，語法為:For | \${target} | in | @{target_list}

Example1: Traverse a single element with a list variable

```@{elements} | Set Variable | John        | Mary
:For        | \${element}   | in          | @{elements}
| log          | \${element}```

Example2: Traverse a piar of elements with a list variable

```@{elements} | Set Variable | John        | 39         | Mary        | 20
:For        | \${name}      | \${age}      | in         | @{elements}
| log          | \${name},\${age}```

RobotFramework無法處理太複雜的資料結構，list是最常使用的儲存方式。可以透過Example2的方式去取各別有意義的資料。

For In Element即loop目標以range為主，語法為:For | \${index} | in range | \${range}

Example1: From 0 to n

```comment     | dump 0, 1, ..., 9
\${n}        | Set Variable | 10
:For        | \${i}         | in range     | \${n}
| log          | \${i}```

Example2: From n1 to n2

```comment     | dump 1, 2, ..., 10
\${n1}       | Set Variable | 1
\${n2}       | Set Variable | 11
:For        | \${i}         | in range     | \${n1}      | \${n2}
| log          | \${i}```

Example3: From n1 to n2 and increases by k

```comment     | dump 1, 3, 5, ..., 9
\${n1}       | Set Variable | 1
\${n2}       | Set Variable | 11
\${k}        | Set Variable | 2
:For        | \${i}         | in range     | \${n1}      | \${n2}     | \${k}
| log          | \${i}```

### Sum of n1 to n2 and increases by k

```\${n1}       | Set Variable | 1
\${n2}       | Set Variable | 11
\${k}        | Set Variable | 2
\${sum}      | Set Variable | 0
:For        | \${i}         | in range     | \${n1}                 | \${n2}     | \${k}
| \${sum}       | Evaluate     | int(\${sun})+int(\${i})
log         | \${sum}```

### 9X9 Multiplication Table

RobotFramework無法做nested for loop，因此我們必須將內部迴圈實做成Keyword: Dump Multiplication Table。

```:For | \${i}                      | In Range                | 2                       | 10
| Dump Multiplication Table | \${i}```
Dump Multiplication Table接收的傳入值，將其各別乘以1~9。
```# Keyword Name: Dump Multiplication Table
# Arguments:    \${index}
:For | \${i}     | In Range                | 1                       | 10
| \${result}| Evaluate                | int(\${index})*int(\${i})
| log      | \${index}*{i} = \${result}```