In the previous article in this series, you looked at Functions. In this article, let's look at managing the control flow of our Rust program using conditional statements.
What are conditional statements?
When writing some code, one of the most common tasks is to perform a check for certain conditions to be true
or false
. "If the temperature is higher than 35°C, turn on the air conditioner."
By using keywords like if
and else
(sometimes in combination), a programmer can change what the program does based on conditions like the number of arguments provided, the options passed from the command line, the names of files, error occurrence, etc.
So it is critical for a programmer to know control flow in any language, let alone in Rust.
Conditional operators
The following table shows all the frequently used operators for an individual condition:
Operator | Example | Interpretation |
---|---|---|
> |
a > b |
a is greater than b |
< |
a < b |
a is less than b |
== |
a == b |
a is equal to b |
!= |
a != b |
a is not equal to b |
>= |
a >= b |
a is greater than OR equal to b |
<= |
a <= b |
a is less than OR equal to b |
And following is the table for logical operators, they are used between one or more conditions:
Operator | Example | Interpretation |
---|---|---|
|| (Logical OR) |
COND1 || COND2 |
At least one of the condition COND1 or COND2 evaluates to true |
&& (Logical AND) |
COND1 && COND2 |
All conditions evaluate to true |
! (Logical NOT) |
!COND |
Opposite boolean value of what COND evaluates to |
Using if else
To handle the basic flow of Rust code, two keywords are used: if
and else
. This helps you create two "execution paths" based on the state of the provided condition.
The syntax of a simple if block with an alternative execution path is as follows:
if condition {
<statement(s)>;
} else {
<statement(s)>;
}
Let's look at an example.
fn main() {
let a = 36;
let b = 25;
if a > b {
println!("a is greater than b");
} else {
println!("b is greater than a");
}
}
Here, I have declared two integer variables a
and b
with the values '36' and '25'. On line 5, I check if the value stored in variable a
is greater than the value stored in variable b
. If the condition evaluates to true
, the code on line 6 will be executed. If the condition evaluates to false
, due to the fact that we have an else
block (which is optional), the code on line 8 will get executed.
Let's verify this by looking at the program output.
a is greater than b
Perfect!
Let's modify the value of variable a
to be less than value of variable b
and see what happens. I will change a
's value to '10'. Following is the output after this modification:
b is greater than a
But, what if I store the same value in variables a
and b
? To see this, I will set both variables' value to be '40'. Following is the output after this particular modification:
b is greater than a
Huh? Logically, this doesn't make any sense... :(
But this can be improved! Continue reading.
Using 'else if' conditional
Like any other programming language, you can put an else if
block to provide more than two execution paths. The syntax is as follows:
if condition {
<statement(s)>;
} else if condition {
<statement(s)>;
} else {
<statement(s)>;
}
Now, with the use of an else if
block, I can improve the logic of my program. Following is the modified program.
fn main() {
let a = 40;
let b = 40;
if a == b {
println!("a and b are equal");
} else if a > b {
println!("a is greater than b");
} else {
println!("b is greater than a");
}
}
Now, the logic of my program is correct. It has handled all edge cases (that I can think of). The condition where a
is equal to b
is handled on line 5. The condition where a
might be greater than b
is handled on line 7. And, the condition where a
is less than b
is intrinsically handled by the else
block on line 9.
Now, when I run this code, I get the following output:
a and b are equal
Now that's perfect!
Example: Find the greatest
I know that the use of if
and else
is easy, but let us look at one more program. This time, let's compare three numbers. I will also make use of a logical operator in this instance!
fn main() {
let a = 73;
let b = 56;
let c = 15;
if (a != b) && (a != c) && (b != c) {
if (a > b) && (a > c) {
println!("a is the greatest");
} else if (b > a) && (b > c) {
println!("b is the greatest");
} else {
println!("c is the greatest");
}
}
}
This might look complicated at first sight, but fear not; I shall explain this!
Initially, I declare three variables a
, b
and c
with random values that I could think of at that time. Then, on line 6, I check for the condition where no variable's value is same as any other variable. First, I check the values of a
and b
, then a
and c
and then b
and c
. This way I can be sure that there are no duplicate values stored in either variable.
Then, on line 7, I check if the value stored in variable a
is the greatest. If that condition evaluates to true
, code on line 8 gets executed. Otherwise the execution path on line 9 is checked.
On line 9, I check if the value stored in variable b
is the greatest. If this condition evaluates to true
, code on line 10 gets executed. If this condition is also false
, then it means only one thing. Neither variable a
, nor variable b
is the greatest among all 3.
So naturally, in the else
block, I print that the variable c
holds the greatest value.
Let's verify this with the program output:
a is the greatest
And this is as expected. Try and modify the values assigned to each variable and test it out yourself! :)
Conclusion
You learned to use if and else statements. Before you go on making your own AI with lost of if else-if statements (haha), let' learn about loops in Rust in the next chapter of the series.
Stay tuned.
from It's FOSS https://ift.tt/RkZ5sVI
via IFTTT
Tidak ada komentar:
Posting Komentar