Control Structures
Control structures are fundamental elements in programming that allow controlling the execution flow of a program. These structures allow making decisions, repeating code blocks, and handling errors.
In JARU, all control structures, such as if, else, elsif, while, for, etc., must end with the keyword end. This indicates to the compiler that the control structure has ended. Using end is necessary to avoid syntax errors and to maintain a clean and easy-to-understand code structure.
If-then-else Flow
The if-then-else control structure is one of the most basic control structures in programming. It's used to make decisions based on a given condition. The basic syntax is as follows:
if (condition) then
// code to execute if the condition is true
else
// code to execute if the condition is false
end
The elsif clause is an extension of if-then-else that allows evaluating multiple conditions in a single control structure. The code will execute in the first block where the condition is met:
if (condition1) then
// code to execute if condition1 is true
elsif (condition2) then
// code to execute if condition2 is true
else
// code to execute if no previous condition is true
end
Switch-case Flow
The switch-case control structure is a way to control program flow by selecting different paths based on a variable or expression. It's similar to using multiple nested if-else structures, but has the advantage of being more readable and easier to maintain.
switch (variable or expression)
case value1:
// instructions
case value2:
// instructions
default:
// instructions
end
The expression is evaluated and compared with each of the case values. If a match is found, the instructions associated with that case are executed. If no match is found, the default instructions are executed.
In JARU, the switch-case structure can be used with any data type, including strings and dynamic variables.
For Loop
The for structure in JARU is used to perform a controlled loop. The basic syntax is as follows:
for (initialization; condition; increment)
// loop body
end
The initialization is the expression executed at the beginning of the loop, usually a variable that will be used as a counter. The condition is an expression evaluated on each loop iteration; if the condition is false, the loop ends. The increment is the expression executed at the end of each loop iteration.
for (var i = 1; i <= 10; i++)
println(i)
end
In this example, the initialization is var i = 1, the condition is i <= 10, and the increment is i++. Each time the loop executes, the value of variable i is printed and incremented by 1.
You can create an infinite loop with for using the following syntax:
for (;;)
// loop body
end
Examples of for Usage
Iterate a determined number of times:
for (var i = 0; i < 10; i++)
println("Iteration ", i)
end
Iterate over a list:
var myList[5] = [1, 2, 3, 4, 5]
for (var i = 0; i < 5; i++)
println("The element at position ", i, " is ", myArray[i])
end
Iterate in reverse order:
for (var i = 10; i > 0; i--)
println("Iteration ", i)
end
Extended For
A unique feature of JARU is its extended for control structure, which adds filter functionality and alternative code to provide greater flexibility to programmers.
The extended for declaration in JARU follows this structure:
for (var i = start_value; i < end_value; i = i + increment_value; filter_condition)
// code to execute when the filter condition is true
elfor
// alternative code to execute when the filter condition is false
end
Structure Analysis
- Initialization:
var i = start_value;This is where the loop control variable is initialized. - Condition:
i < end_value;This is the condition evaluated before each loop iteration. - Update:
i = i + increment_value;This is the operation performed after each loop iteration. - Filter:
filter_conditionA unique JARU feature that allows setting an additional condition that must be met for the code inside the loop to execute. - Alternative Code:
elforAllows specifying an alternative code block that will execute when the filter condition is not met. - Termination:
endMarks the end of theforblock.
Extended for Examples
// FOR loop with filter (only prints even numbers)
for (var i = 0; i < 10; i = i + 1; i % 2 == 0)
println(i)
end
// FOR loop with filter and alternative (prints "odd" for odd numbers)
for (var i = 0; i < 10; i = i + 1; i % 2 == 0)
println(i)
elfor
println("odd")
end
Extended for Use Cases
Processing only certain elements:
for (var i = 0; i < list.length; i++; list[i] % 2 == 0)
println(list[i])
end
Handling exceptions or special conditions:
for (var i = 0; i < data.length; i++; data[i].isValid())
process(data[i])
elfor
println("Invalid data at index: ", i)
end
Nested loops with extended features:
for (var i = 0; i < users.length; i++)
for (var j = 0; j < users[i].transactions.length; j++; users[i].transactions[j].isFraudulent())
alert("Fraudulent transaction detected for user ", users[i].name)
elfor
processTransaction(users[i].transactions[j])
end
end
The extended for is especially useful when you need to process data collections with complex conditions, avoiding the need to nest multiple if structures inside the loop.
Foreach Loop
The foreach control structure in JARU is used to iterate through elements of a collection, such as a list, map, or array. The basic syntax is as follows:
foreach (var element in collection)
// code to execute for each element
end
Where element is a temporary variable that takes the value of each element in the collection on each loop iteration. The loop body is executed for each element in the collection.
list = [1, 2, 3, 4, 5]
foreach (var number in list)
println(number) // Shows 1, 2, 3, 4 and 5
end
In this case, the variable number would take the value of each element in the list on each iteration, and the code inside the foreach block would execute for each element.
The loop automatically stops once all elements in the collection have been traversed.
While Loop
The while control structure in JARU is a loop that executes while a given condition is met. The condition is evaluated at the beginning of each iteration, and if it's true, the loop body executes. If the condition is false, the loop stops and continues with the next line of code.
while (condition)
// Code block to execute
end
If you want an infinite loop with while, all you have to do is make the condition true:
while (true)
// Code block to execute
end
Example:
var i = 0
while (i < 10)
println("Variable i has value ", i)
i += 1
end
In this example, the condition is i < 10, so the loop executes 10 times, printing the value of variable i on each iteration.
It's important to be careful to include a way to stop the loop, either by changing the condition within the loop body or using an additional control structure, otherwise the loop will run indefinitely.
Until Loop
The until loop in JARU is used to repeat a code block until a certain condition is met.
until (condition)
// code block
end
In this code, the condition is a boolean expression that is evaluated before each loop iteration. If the condition is false, the code block inside the loop will execute and then the condition will be evaluated again. This process will continue until the condition is true, at which point the loop will exit.
Example that prints numbers from 1 to 10:
var i = 1
until (i > 10)
println(i)
i++
end
Another example - counts down from a given number to zero:
var number = 10
until (number == 0)
println(number)
number = number - 1
end
println("Liftoff!")
Difference Between while and until
Both until and while are flow control structures used to implement loops. The main difference is in how the exit condition is evaluated:
- In
until, the loop executes until the specified condition is true - In
while, the loop executes while the condition is true
It's important to note that the code block inside the loop must modify the condition in some way to avoid an infinite loop. Otherwise, the loop will run indefinitely.