Welcome reader,
We’ll pick up where we left with how to manage the program flow of a software application. We saw how to use the if-else if-else structure. To evaluate a condition, we need comparaison operators. Those are use evaluate a boolean expression, which will either result a true or false. Below, you’ll be able to find them.
|
Operator |
Description |
|---|---|
|
== |
Equality |
|
!= |
Inequality |
|
> |
Greater than |
|
< |
Less than |
|
>= |
Greater or equal |
|
<= |
Less or equal |
|
&& |
Called Logical AND operator. If both the operands are true, then condition becomes true. |
|
|| |
Called the logical OR operator. If any the operands is true, then the condition become true. |
|
! |
Called the logical NOT operator. It uses the reverse logical state of a an operand. Which means that if an operand was initially at true, the NOT operator makes it false during the evaluation |
Thanks to these operators, there is a lot that can be achieved to evaluation an expression. We’ll use them in samples to try to understand how they work. You can use more than just one to evaluate a given condition in your program flow.
float bankAccount=2500;
if(bankAccount>3000){
Console.WriteLine("You have quite a big account");
}else if(bankAccount2000){
Console.WriteLine("You have more than 1000 $ and less than 2000 $ in your account");
}else{
Console.WriteLine("We can't evaluate the value in your account, here's how much you have now : {0} $",bankAccount);
}
bool aBoolValue=true;
if(!aBoolValue){
Console.WriteLine("That's not right ! Is it ?"); //You should see this as false
}
if(aBoolValue){
Console.WriteLine("That is true! If you have guess that the ouput, good job!");
}
int ageOfPerson=18;
if(ageOfPerson == 18)</div>
{
Console.WriteLine("You are consider as an adult in Canada");
}
//next example is a bad example. Please be sure not to reproduce code likethis one
if(ageOfPerson=21)
{
Console.WriteLine("You can vote in any country that you want, or I think ?");
}