Tutorial 7 (Conditions and loops) - Aerll/rpp GitHub Wiki
An if statement is simply a block of code, which is executed only when the condition results in true
. This is especially useful when we want to generalize our code.
First let's talk about conditions. In r++ there are 6 comparison operators:
-
==
- equal -
!=
- not equal -
<
- less than -
<=
- less than or equal -
>
- greater than -
>=
- greater than or equal
Each of these results in a value of type bool
, which can be either true
or false
.
Here's an example:
bool b1 = 1 == 1; // b1 = true
bool b2 = 1 != 1; // b2 = false
On top of that r++ has 3 logical operators:
-
and
- logical AND -
or
- logical OR -
not
- logical NOT
Operators and
and or
require values of type bool
on each side, and operator not
requires a single value of type bool
on the right side. Logical AND results in true
only when both values are true
, logical OR results in true
when at least one of the values is true
, logical NOT flips the result.
Here's an example:
bool b1 = 1 == 1 and 2 == 2; // b1 = true
bool b2 = 1 != 1 and 2 == 2; // b2 = false
bool b3 = 1 == 1 or 2 == 2; // b3 = true
bool b4 = 1 != 1 or 2 == 2; // b4 = true
bool b5 = not 1 == 1; // b5 = false
bool b6 = not 1 != 1; // b6 = true
Now that we know about conditions, we can take a look at how to create an if statement:
if (condition)
...
end
Let's break it down:
- every if statement starts with the
if
keyword followed by parenthesis and ends with theend
keyword -
condition
- this is where we put our condition, it can be any expression resulting in typebool
-
...
- this is where we write our usual r++ code, it will be executed only whencondition
evaluates totrue
We can also nest these statements freely:
if (condition)
...
if (condition)
...
end
if (condition)
if (condition)
...
end
end
end
Just don't forget to put an end
to each of them.
Here's a simple example that will show you the if statement in action:
int n = 0;
if (n == 0)
warning("Hello");
end
This will display a warning Hello
in the console. However if you change the value of n
to anything else, the message won't appear.
A loop is a block of code, which will be executed repeatedly for as long as the breaking condition evaluates to true
. This condition is hidden from us, but there's a way to break out of the loop manually if needed. Loops are used mainly for iterating over elements of an array, which we will talk about in another tutorial.
In r++ there's only a single type of loop. It's a range based for
loop, which means it will iterate over all values of a given range. The step for this iteration is always 1
or -1
, which solely depends on if a range is ascending or descending. Here's how to create a loop:
for (name = 0 to 10)
...
end
Let's break it down:
- every loop starts with the
for
keyword followed by parenthesis and ends with theend
keyword -
name = 0
- declares a local variable of typeint
, which will be incremented by1
every time the program reaches anend
-
0 to 10
- defines a range for the loop to iterate over, it works similarly to a range literal, however it's exclusive to loops -
...
- this is where we write our usual r++ code, it will be executed repeatedly untilname
goes out of range
Just like with if statements, we can nest loops freely with other loops or if statements:
for (x = 0 to 5)
...
if (condition)
...
end
if (condition)
for (y = 0 to 5)
...
end
end
end
Now that we know how a loop is structured, we can try some simple examples:
// ascending
for (i = 0 to 5)
warning(i.str());
end
// descending
for (i = 5 to 0)
warning(i.str());
end
This will display warnings from 0
to 5
and then from 5
to 0
in the console.
Let me also quickly introduce str()
, which is a novelty here. This function converts the value of any variable to a string, which is useful for testing.
Note:
str()
is supported by all types.
Very rarely you may find yourself wanting to break out of the loop early or skip an iteration under some circumstances. In r++ this can be achieved with keywords break
and continue
.
Let's have a quick look at examples of both:
for (i = 0 to 5)
if (i == 3)
break;
end
warning(i.str());
end
This will display warnings from 0
to 2
. Whenever i
is 3
the program reaches break
, which makes it jump out of the loop immediately, therefore it won't output the value 3
in the console.
Now if we do the same but replace break
with continue
:
for (i = 0 to 5)
if (i == 3)
continue;
end
warning(i.str());
end
In this case the program will display warnings from 0
to 5
excluding 3
. Now whenever i
is 3
the program reaches continue
, which makes it jump to the end
of the loop and skip everything in between. Unlike break
the loop continues to run until i
goes out of range.