"Sputnik" help  
Sputnik Help
For ( <init> ; <condition> ; <increment> )
{
statements
...
} // There are many more styles to the For loop // Be sure to read the Examples to see them all // The parameters deal with the default For loop // the rest are explained in the Examples.

Description

Loop based on an expression.

The Sputnik For loop is very versatile and supports many features make sure to read all the examples.

Parameters

<init>

The init step is executed first, and only once. This step allows you to declare and initialize any loop control variables. You are not required to put a statement here, as long as a semicolon appears.

You can have multiple init statements separated by commas.

<condition> The condition is evaluated just like an If statement. If it is true, the body of the loop is executed. If it is false, the body of the loop does not execute and flow of control jumps to the next statement just after the for loop.
<increment>

Before the increment takes place the condition is evaluated again. If it is true, the loop executes and the process repeats itself (body of loop, then increment step, and then again condition). After the condition becomes false, the for loop terminates.

You can have multiple increment statements separated by commas.

Remarks

For statements may be nested.

The For loop in Sputnik is a versatile control structure that allows you to execute a block of code repeatedly based on a specified condition. The loop consists of three components: initialization, condition evaluation, and incrementation.

During the initialization step, you can declare and initialize loop control variables. The condition is then evaluated before each iteration of the loop, and if it is true, the loop body is executed. After each iteration, the increment step is executed to update loop control variables.

The For loop provides flexibility in terms of the condition and increment statements. You can use relational operators, logical operators, and arithmetic operations to define the condition. Additionally, you can have multiple initialization statements and increment statements separated by commas.

The For loop is commonly used for iterating over a range of values, such as counting from a starting value to an ending value. However, it can be adapted to various looping patterns based on the specific needs of your code.

It's important to ensure that the condition eventually becomes false to avoid infinite looping. Carefully defining the condition and incrementation is crucial to achieve the desired looping behavior.

Related

Loop Statements
Continue Statement
Redo Statement
Break Statement

Example

Basic example:

for(my $a = 10; $a < 20; $a++)
{
   say "value of a: $a";
}
// PRINTS
// value of a: 10
// value of a: 11
// value of a: 12
// value of a: 13
// value of a: 14
// value of a: 15
// value of a: 16
// value of a: 17
// value of a: 18
// value of a: 19

You can also omit the { } entirely such as:

for(my $i = 0; $i < 3; $i++)
    say "Cat $i";
// PRINTS
// Cat 0
// Cat 1
// Cat 2
So there are many ways to apply the For loop.

Count up to from 1 to 10:

for (my $i = 1; $i <= 10; $i++)
{
    say "Value is $i";
}
// PRINTS
// Value is 1
// Value is 2
// Value is 3
// Value is 4
// Value is 5
// Value is 6
// Value is 7
// Value is 8
// Value is 9
// Value is 10

Count down to from 10 to 1:

for (my $i = 10; $i > 0; $i--)
{
    say "Value is $i";
}
// PRINTS
// Value is 10
// Value is 9
// Value is 8
// Value is 7
// Value is 6
// Value is 5
// Value is 4
// Value is 3
// Value is 2
// Value is 1

Count up to from 2 to 20 counting in 2s:

for (my $i = 2; $i <= 20; $i += 2)
{
    say "Value is $i";
}
// PRINTS
// Value is 2
// Value is 4
// Value is 6
// Value is 8
// Value is 10
// Value is 12
// Value is 14
// Value is 16
// Value is 18
// Value is 20

Of course you can check $i (the condition) a few times:

for (my $i = 0; $i >= 0 && $i < 5; $i++)
{
    say "Value is $i";
}
// PRINTS
// Value is 0
// Value is 1
// Value is 2
// Value is 3
// Value is 4

Using multiple inits and increments:

for (my $i = 0, my $j = 3; $i < 10; $i++, $j++)
{
    say "Value is $i -- $j";
}
// PRINTS
// Value is 0 -- 3
// Value is 1 -- 4
// Value is 2 -- 5
// Value is 3 -- 6
// Value is 4 -- 7
// Value is 5 -- 8
// Value is 6 -- 9
// Value is 7 -- 10
// Value is 8 -- 11
// Value is 9 -- 12

Using multiple inits with multiple comparison it will print the same as the one above since it will only continue looping if all the comparisons are true:

// Basically this is equal to using 
// $i < 10 && $j < 20
for (my $i = 0, my $j = 3; $i < 10, $j < 20; $i++, $j++)
{
    say "Value is $i -- $j";
}
// PRINTS
// Value is 0 -- 3
// Value is 1 -- 4
// Value is 2 -- 5
// Value is 3 -- 6
// Value is 4 -- 7
// Value is 5 -- 8
// Value is 6 -- 9
// Value is 7 -- 10
// Value is 8 -- 11
// Value is 9 -- 12

Print all array elements:

my $array = array( "One", "Two", "Three", "Four", "Five" );
for (my $i = 0; $i < Count($array); $i++)
{
    my $item = $array[$i];
    say "Value is " . $item;
}
// PRINTS
// Value is One
// Value is Two
// Value is Three
// Value is Four
// Value is Five

A perl style number to number For:

for( 0  rr  3 )
{
    // Yes the @_ gets set just like in Perl
    say "Value @_";
}
// PRINTS
// Value 0
// Value 1
// Value 2
// Value 3
 
for( my $i in 0  rr 3 )
{
    // Yes the @_ gets set just like in Perl
    say "Value $i";
}
// PRINTS
// Value 0
// Value 1
// Value 2
// Value 3

Another perl style but this time using variables:

my $a = 1;
my $b = 3;
 
for( $a rr $b )
{
    say "Value @_";
}
// PRINTS
// Value 1
// Value 2
// Value 3 

// or
for( my $i in $a rr $b ) // my isnt needed here
{
    say "Value $i"say ;
}
// PRINTS
// Value 1
// Value 2
// Value 3
 
// You can join numbers together at the ..
 
for( 0 rr 3 )
{
    say "Value @_";
}
// PRINTS
// Value 0
// Value 1
// Value 2
// Value 3
 
// or
for( my $i in 0 rr 3 ) // my makes sure it is local scope
{
    say "Value $i";
}
// PRINTS
// Value 0
// Value 1
// Value 2
// Value 3
 
// You can also count down
// You can join numbers together at the ..
 
for( 3 rr 0 )
{
    say "Value @_";
}
// PRINTS
// Value 3
// Value 2
// Value 1
// Value 0
 
// or
for( my $i in 3 rr 0 )
{
    say "Value $i";
}
// PRINTS
// Value 3
// Value 2
// Value 1
// Value 0

Another Perl style but this time using Hex numbers:

for( 0x10 rr 0x1F )
{
    say "Value @_";
}
// PRINTS
// Value 16
// Value 17
// Value 18
// Value 19
// Value 20
// Value 21
// Value 22
// Value 23
// Value 24
// Value 25
// Value 26
// Value 27
// Value 28
// Value 29
// Value 30
// Value 31
 
// or
for( my $i in 0x00 rr 0x08 )
{
    say "Value $i";
}
// PRINTS 
// Value 0
// Value 1
// Value 2
// Value 3
// Value 4
// Value 5
// Value 6
// Value 7
// Value 8
 
// Countdown
for( 0x2A rr 0x1E )
{
    say "Value @_";
}
// PRINTS
// Value 42
// Value 41
// Value 40
// Value 39
// Value 38
// Value 37
// Value 36
// Value 35
// Value 34
// Value 33
// Value 32
// Value 31
// Value 30
 
// or
for( my $i in 0x1B rr 0x10  )
{
    say "Value $i";
}
// PRINTS
// Value 27
// Value 26
// Value 25
// Value 24
// Value 23
// Value 22
// Value 21
// Value 20
// Value 19
// Value 18
// Value 17
// Value 16

Another Perl style but this time using a Step counting up:

// Digits
for( 10 rr 20 by 2 )
{
    say "Value @_";
}
// PRINTS
// Value 10
// Value 12
// Value 14
// Value 16
// Value 18
// Value 20

// Hex
for( my $i in 0x00 rr 0x1B by 0x02 )
{
    say "Value $i";
}
// PRINTS
// Value 0
// Value 2
// Value 4
// Value 6
// Value 8
// Value 10
// Value 12
// Value 14
// Value 16
// Value 18
// Value 20
// Value 22
// Value 24
// Value 26
 
// Var
my $first = 15;
my $second = 21;
for( my $i in $first..$second by 2 )
{
    say "Value $i";
}
// PRINTS
// Value 15
// Value 17
// Value 19
// Value 21

It's worth noting the Range Operator rr supports rs to do stepping too:

// Digits
for( 10 rr 20 rs 2 )
{
    say "Value @_";
}
// PRINTS
// Value 10
// Value 12
// Value 14
// Value 16
// Value 18
// Value 20

// Hex
for( my $i in 0x00 rr 0x1B rs 0x02 )
{
    say "Value $i";
}
// PRINTS
// Value 0
// Value 2
// Value 4
// Value 6
// Value 8
// Value 10
// Value 12
// Value 14
// Value 16
// Value 18
// Value 20
// Value 22
// Value 24
// Value 26

Another Perl style but this time using a Step counting down:

// Digits
for( 15 rr 10 by 2 )
{
    say "Value @_";
}
// PRINTS
// Value 15
// Value 13
// Value 11

// Hex
for( my $i in 0x10 rr 0x00 by 0x02 )
{
    say "Value $i";
}
// PRINTS
// Value 16
// Value 14
// Value 12
// Value 10
// Value 8
// Value 6
// Value 4
// Value 2
// Value 0
 
// Var
my $first = 18;
my $second = 15;
for( my $i in $first rr $second by 2 )
{
    say "Value $i";
}
// PRINTS
// Value 18
// Value 16

There is a shorthand way to do Foreach using a For loop for example:

my $a = ["cat", "dog", "fox"];

for (my $i in $a)
{
    say $i;
}

for (my $i in Range(5))
{
    say $i;
}

// PRINTS
// cat
// dog
// fox
// 0
// 1
// 2
// 3
// 4
// 5

You can use a VB style for loops like so:

my $array = ["The", "Quick", "Brown", "Fox", "Cat", "Dog"];
for(my $i = 0 To UBound($array) - 1)
    say "[$i] $array[$i]";
    
// PRINTS
// [0] The
// [1] Quick
// [2] Brown
// [3] Fox
// [4] Cat
// [5] Dog

You can also step the VB style for loops like so:

my $array = ["The", "Quick", "Brown", "Fox", "Cat", "Dog"];
for(my $i = 0 To UBound($array) - 1 Step 2)
    say "[$i] $array[$i]";
    
// PRINTS
// [0] The
// [2] Brown
// [4] Cat

These VB style for loops can even count backwards like so:

my $array = ["The", "Quick", "Brown", "Fox", "Cat", "Dog"];
for(my $i = UBound($array) - 1 To 0)
    say "[$i] $array[$i]";
    
// PRINTS
// [5] Dog
// [4] Cat
// [3] Fox
// [2] Brown
// [1] Quick
// [0] The

You can even do the reversed VB style loop using steps:

my $array = ["The", "Quick", "Brown", "Fox", "Cat", "Dog"];
for(my $i = UBound($array) - 1 To 0 Step 2)
    say "[$i] $array[$i]";
    
// PRINTS
// [5] Dog
// [3] Fox
// [1] Quick

Using the VB style in simplest form:

For (my $i = 0 To 10)
{
    say $i;
}
// PRINTS
// 0
// 1
// 2
// 3
// 4
// 5
// 6
// 7
// 8
// 9
// 10

For (my $i = 0 To 10 Step 2)
{
    say $i;
}
// PRINTS
// 0
// 2
// 4
// 6
// 8
// 10

 


Contact
Cheryl (uberfox@hotmail.com)
Homepage
http://ubersoft.org