Operators are used to do mathematical operations, comparison operations, logic operators and assigning value operations for example if you do: num1 = 10 then you have used the = (equals) operator to assign the value of 10 to the variable num1. There are many different operators as can be seen in the four categories below.
STEP 1 | ARITHMETIC OPERATORS
As the name suggests, arithmetic operators are used to perform calculations. Popular arithmetic operators can be seen in the table below.
STEP 2 | COMPARISON OPERATORS
As the name suggests, comparison operators are used compare values. Popular comparison operators can be seen in the table below.
One equals sign, means make it equal to
Two equal signs mean is it equal to ?
STEP 3 | ASSIGNING OPERATORS
As the name suggests, assigning operators are used assign values to variables. Popular assigning operators can be seen in the table below.
STEP 4 | LOGIC OPERATORS
As the name suggests, logic operators are used like logic gate, they are frequently used in IF statements and can be used as stand alone operators. Popular logic operators can be seen in the table below.
Logical AND - &&
Logical OR - ||
Logical NOT - !
SECTION 5 | OPERATOR EXAMPLES
Operators are fundamental building blocks in programming. They allow us to manipulate data, make decisions, and structure our code in meaningful ways. As you continue your coding journey, you'll encounter and use these operators frequently, making them essential tools in your developer toolkit. Here are some examples of operators in use, copy and try them out in VSC and experiment, for example change the age value to less than 18 and see what happens.
QUESTION 1:Which operator could you use to determine if a given value is odd or even, and how could you use it?
TASK Create a program to: 1: Ask the user to enter a number 2: Write a line of code to calculate the DIV of the number given and save the answer in a new variable 3: Output to the user the answer in a user friendly string concatenated with the numeric answer.
TRY Have a go at using some of the other operators to see how they work, don't be afraid to experiment.