for structure

for structure usually use when you know how many times you will loop

Simple for structures:

for <time counts> times

<tasks to do repeatedly>

<tasks to do repeatedly> could be single command or block

$_time is a var that hold the loop time, starting with 1.

Ex: Write 3 time “I’ll never play games in classes.”, with number:

for 3 times

@echo $_time + “. I’ll never play games in classes.”

> 1. I’ll never play games in classes.

> 2. I’ll never play games in classes.

> 3. I’ll never play games in classes.

You could also use at keyword here

Ex: Write 4 time “I’ll never play games in classes.”, at time 3, write “This is time 3” :

for 3 times

@echo $_time+”. I’ll never play games in classes.”

at $_time = 3

” This is time 3″

> 1. I’ll never play games in classes.

> 2. I’ll never play games in classes.

> 3. I’ll never play games in classes. This is time 3

You could replace times with any variable:

for 3 $i:

for 3 $j:

if $j = $i:

@echo ‘ * ’

else:

@echo ‘ ’

@echo “\n” //new line

> *

> *

> *

Full for structures:

for <initation>,<break_condition>,<increasement>

<tasks to do repeatedly>

Ex: Write 3 time “I’ll never play games in classes, with number”:

for $i:1,$i<3,$i++

@echo ($i+1)”. I’ll never play games in classes.”

> 1. I’ll never play games in classes.

> 2. I’ll never play games in classes.

> 3. I’ll never play games in classes.

For-in with arrays

You could iterate through elements in array and do actions for each of those element musing for and in.

for <element’s value holding var> in <array>

<action for each element>

In case you need both the key and the value of each element:

for < element’s key holding var> => <element’s value holding var> in <array>

<action for each element>

Ex: write out all name in an array which is a list of names.

$list:[“Annie”,”Bob”, “John”, “Sabrina”]

for $name in $list

@echo $name + “\n” // \n is the end of output line.

> Annie

> Bob

> John

> Sabrina

>

Now, write out all name in an array which is a list of names with it’s index.

$list : [“Annie”,”Bob”, “John”, “Sabrina”]

for $index => $name in $list

@echo $index + “. ” + $name + “\n

> 1. Annie

> 2. Bob

> 3. John

> 4. Sabrina

>

Foreach

foreach $list, $k, $v:

foreach $list, $v:

foreach $list:

$_k, $_v