-
Notifications
You must be signed in to change notification settings - Fork 81
Loop execution
In Jai loops are one of the most powerful features, they are both powerful and concise.
The for loops apply to situations where the number of iterations is known before the execution.
For loops are used widely to iterate over arrays.
An iteration over arrays semantic, somewhat similar to the 'C' style that uses indexes, it is still possible.
lines: [..] string;
lines = read_lines(file_name);
lines_number := lines.count;
// Perform N operations iterating over a range of numbers
i := 0;
for 1..lines_number print_line(lines[i++]);
// Perform N operations iterating over a range of numbers and assigning a
// name to the iterator
for i:1..rows_number print_line(rows[i-1]);
Instead in Jai, we can leverage a simple concept of iterator and so it is possible to use the keywords 'it' and 'it_index' to get the items of the arrays. This is possible because in Jai the arrays are data structures that not only store the data but also the number of the contained items.
ROW_SIZE :: 10;
row_cells : [ROW_SIZE] int;
accum := 0;
// Old stye C
// Iterate over the array using an index and a range operator
for i: 0..ROW_SIZE-1 row_cells[i]= ++accum;
// Alternative Jai iterator-index way
// Iterate over the array using the 'it_index' keyword
// 'it_index' is a zero based number from the first to the last element of the array
for rows_cells row_cells[it_index] = ++accum;
// Preferred Jai iterator native way
// Iterate over the array using the 'it' keyword
// Here 'it' means 'row_cells[it_index]'
for rows_cells it = ++accum;
// Alternative Jai iterator renaimg way
// Iterate over the array using a renamed iterator
// Here 'row' still means 'row_cells[it_index]'
for row: rows_cells {
row = ++accum;
}
The while loops apply to situations where the number o iterations depend on external conditions
These documents were verified using Grammarly free browser plugin for Chrome. Please use some spell checker before submitting new content.
- Variables and assignments
- Language data types
- Simple user-defined data types
- Expressions and operators
- Type-casting
- Pointers
- Declarations
- Arguments / Parameters
- Return values
- Overloading / Polymorhism
- Advanced features
- Lambdas
- Arrays
- Strings
- Composition of Structs
- Metaprogramming
- Templates / Generics