Operators
Operators are needed to check the relationship between two values.
Prerequisite
This page assumes you have completed the Coding Intro lesson.
Intro
This lesson will bring you closer to understanding coding and how the use of operators really depends on what you need the code to do for your program. Not only that, the use of operators in multiple conditions can really bring a program to life!
Assignment
The = operator is used for assignments. It takes whatever is on the right side and assign its value to the left side.
Tip: Examples below display the operators but do not create variables. Don’t forget to add the necessary variables before they are used.
x = 50;
y = 43 + x;
s = "Hello World";
Equality
The == operator is used to check if two sides are equal. Note how = and == are very different in coding!
if(x == y){
await BrainPad.System.Println("They are the same");
}
To check for not equal, we use != operator.
if(x != y){
await BrainPad.System.Println("They are not the same");
}
More or Less
The greater-than > and less-than < symbols compare two values to see which one is more or less. To check if the value is greater-than or equal-to, use >= operator. Similarly, you can use <= to check if a value is less-than or equal-to.
if(x >= y){
await BrainPad.System.Println("x is greater than or equal to y");
}
Math
These work exactly how you would expect. The only exception is adding strings. 5+5 will result in 10 as expected but adding “5”+”5″ will result in 55. That is because the language is adding the sting (the text) and it does not care about its content. “5”+”hello” will result in 5hello. It will not even add spaces!
Operator | Name |
---|---|
+ | Add |
– | Subtract |
* | Multiply |
/ | Divide |
% | Modulus, the remainder of a division. |
Logical
Logical operators are useful to check multiple evaluations. For example, you can check if x is more than 10 AND is also less than 40 using &&. Note how && is used in C# instead of &. These are different and you will likely always use &&. Python uses the word and.
if(x > 10 && x < 40){
await BrainPad.System.Println("x is greater than 10 and less than 40");
}
The opposite of and operator is the or operator. In C# the logical or operator is || (double pipe symbol) and in Python it is or. Can you guess how to make the || symbol using your keyboard?
Do not confuse the pipe symbol | with lower case L and upper case I. The key we need is found all the way to the right above to the Enter key, and it includes the the backslash \ symbol. Just like any other key containing two characters where the one you want is the top character, use shift and hit this key twice to get the double pipe.
if(x > 10 || y > 3){
await BrainPad.System.Println("x is greater than 10 or y is greater than 3");
}
Bitwise
These operators handle values on bit level. This is beyond what we need in these lessons and we will not cover them.
Operator | Name |
---|---|
& | AND |
I | OR |
^ | XOR |
~ | NOT |
>> | Right shift |
<< | Left shift |
What’s Next?
You should have a nearly complete idea of the core features of coding. The only thing left is to start creating functions to encapsulate code blocks.
BrainStorm
You can use nested if statements to check multiple conditions. If we can “nest” an if statement inside another if statement, why use the and operator? This code below behaves exactly like the code used for logical and operator.
if(x > 10){
if(x < 40){
await BrainPad.System.Println("x is greater than 10 and less than 40");
}
}
We can see that there are multiple ways of writing the same code. While the BrainPad may behave the same, there are ways to write code that is easier to read and understand.