Loops¶
Loops are used to execute a block of code multiple times. The block of code will be executed until the condition is true. This help us to reduce the code and execute the same block of code multiple times.
In Radon we have 2 types of loops.
for
loop.while
loop.
For loop¶
In for loop we have 2 varients.
for
loop with range.for
loop with sequence of elements.
For loop with range¶
With range we can specify the start, end and step value. If step value is not provided then it will be 1.
Output:
With step value. Here we are printing the even numbers.
Output:
For loop with sequence of elements¶
With sequence of elements we can specify the elements in the loop. The loop will run for each element.
Here we are using Array
of elements.
The loop will run for each element in the Array
.
Output
Here we are using String
. The loop will run for each character in the String
.
Output:
Here we are using HashMap
. The loop will run for each key in the HashMap
.
for_hashmap.rn | |
---|---|
Output:
While loop¶
With while loop we can specify the condition. The loop will run until the condition is true.
Output:
We have used nonlocal
keyword to update the value of i
in the loop.
If we don't use nonlocal
then it will run into infinite loop.
Loop control statements¶
In Radon we have 2 loop control statements.
break
continue
Break¶
With break
we can exit the loop.
For loop example:
Output:
While loop example:
Output:
Continue¶
With continue
we can skip the current iteration and move to the next iteration.
For loop example:
Output:
While loop example:
while_continue.rn | |
---|---|
Output:
That's all about loops in Radon.