Operators

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 assigns its value to the left side.

Tip: The examples below display the operators but do not create variables. Don’t forget to add the necessary variables before they are used (this is automated in Python but C# and JS need var).

Python

x = 50
y = 43 + x
s = "Hello world"

C#

x = 50;
y = 43 + x;
s = "Hello World";

JS

let x = 50;
let y = 43 + x;
let s = "Hello World";

Equality

The == operator is used to check if two sides are equal. Note how = and == are very different in coding!

Python

if x == y:
    BrainPad.System.Println("They are the same")

C#

if(x == y){
    BrainPad.System.Println("They are the same");
}

JS

if(x == y){
    await BrainPad.System.Println("They are the same");
}

To check for not equal, we use the != operator.

Python

if x != y:
    BrainPad.System.Println("They are not the same")

C#

if(x != y){
    BrainPad.System.Println("They are not the same");
}

JS

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 the >= operator. Similarly, you can use <= to check if a value is less-than or equal-to.

Python

if x >= y:
    BrainPad.System.Println("x is greater than or equal to y")

C#

if(x >= y){
    BrainPad.System.Println("x is greater than or equal to y");
}

JS

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!

OperatorName
+Add
Subtract
*Multiply
/Divide
%Modulus, the remainder of a division.

Logical

Logical operators are useful for checking 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.

Python

if x > 10 and x < 40:
    BrainPad.System.Println("x is greater than 10 and less than 40")

C#

if(x > 10 && x < 40){
    BrainPad.System.Println("x is greater than 10 and less than 40");
}

JS

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.

Python

if x > 10 or y > 3:
    BrainPad.System.Println("x is greater than 10 or y is greater than 3")

C#

if(x > 10 || y > 3){
    BrainPad.System.Println("x is greater than 10 or y is greater than 3");
}

JS

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 a bit level. This is beyond what we need in these lessons and we will not cover them.

OperatorName
&AND
IOR
^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? The code below behaves exactly like the code used for logic and operator.

Python

if x > 10:
    if x < 40:
        BrainPad.System.Println("x is greater than 10 and less than 40")

C#

if(x > 10){
    if(x < 40){
        BrainPad.System.Println("x is greater than 10 and less than 40");
    }
}

JS

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.

Content Licensing
Newsletter

Twitter Feed
Hot off the press!