Operators and Operands

Keep in mind that section 2 (Basic CS Concepts) is just about generalized concepts that can be applied to many different programming scenarios. You won’t start coding until section 4, which covers Java. But in order to use Java, or any programming language for that matter, it helps to understand the foundational concepts first.

Operators – operators are what you use with operands. Some examples of operators include +, -, *, ** (exponentiation), /, // (floor division), and % (modulo).

Operands – the things used with operators. In this example:

5 + 2

+ is the operator and 5 and 2 are the operands.

Modulus – use modulus to get the remainder of something. It is usually a percent sign in most languages.

To see if a number is even, do something like the following:

if (x % 2 == 0){
    xEven = true;
}

= vs. == – one equal sign means to set something to a value. It’s called the assignment operator. When you write x = 4, you are making x equal to 4, regardless of what is used to be (if anything at all).

When you write this:

x == 4

You are checking to see if x is 4 or not. It will yield a Boolean value. If x is 4, it will evaluate to true. If not, it will be false. It’s the equality operator, which is a type of comparison operator. If you mix these two concepts up, your program might exhibit strange behavior or give you syntax errors.

Floor division – if you go to the store and you want to buy something that costs $4, and you have $5 in your wallet, how many can you buy? Well, a calculator would tell you 1.25, but common sense would say you can only buy one because you can’t break something into multiple pieces and then only buy part of it. Floor division will give you the quotient with no remainder. It’s like the opposite of modulus. It is often denoted with double slashes.

If you run the following Python code:

print(5 // 4)

It will output 1.

Statement – a piece of code that performs an action.

Expression – something that gets evaluated.

Evaluation – examples of things that need to be evaluated are lines like the following:

y = x + 5

Or

answer = y ** 2

← Previous | Next →

Basic CS Topic List

Main Topic List

Leave a Reply

Your email address will not be published. Required fields are marked *