TutorChase logo
Login
OCR GCSE Computer Science Notes

8.1.3 Arithmetic and Boolean Operators

Arithmetic and Boolean operators are essential tools in programming, allowing developers to perform calculations, compare values, and control logical flow within a program.

What Are Arithmetic Operators?

Arithmetic operators are used to perform basic mathematical operations on values or variables. These operators help process numerical data and are foundational for solving problems using code.

Common Arithmetic Operators

Here are the key arithmetic operators you need to know:

  • Addition (+): Adds two numbers together.

    • Example: 5 + 3 results in 8.

  • Subtraction (-): Subtracts one number from another.

    • Example: 10 - 4 results in 6.

  • Multiplication (*): Multiplies two numbers.

    • Example: 7 * 2 results in 14.

  • Division (/): Divides one number by another. The result is a floating-point number if the division is not exact.

    • Example: 8 / 2 results in 4.0; 9 / 2 results in 4.5.

Each of these operators can be used with variables or literal values in programming to manipulate data.

Special Arithmetic Operators

Practice Questions

Take your grades to the next level!

UPGRADING TO PREMIUM UNLOCKS
AI Tutor
AI-powered study assistant
instant feedback and guidance
Predicted Papers
Examiner-style predicted papers
based on recent exam trends
Practice Questions
All exam practice questions
by topic for each subject
Study Notes
All detailed revision notes
written by expert teachers
Cheat Sheets
Quick revision summaries
perfect for last-minute review
Past Papers
Complete collection
of practice and past exam papers
Email
Password
Confirm Password
Already have an account?

FAQ

When you mix DIV (integer division) and normal division / in the same expression, the programming language’s rules of precedence and data types come into play. DIV will return an integer (whole number), while / returns a floating-point (decimal) result. If not handled carefully, mixing these can lead to unexpected data types and results. For example, in some languages, using / with integers automatically converts the result to a float, which might cause issues if the rest of the code expects an integer. Also, because DIV happens before addition and subtraction in precedence, you might accidentally calculate the integer division first when you meant the division to happen after another operation. To avoid errors, it’s important to use parentheses to control the order of operations and be clear about whether you want integer or decimal results in each part of the expression.

Although Boolean operators like AND, OR, and NOT are designed to work with Boolean values (True or False), many programming languages treat certain numbers as equivalent to Boolean values in conditional expressions. Typically, 0 is treated as False, and any nonzero number is treated as True. This behavior allows expressions like if (x AND y) where x and y are numbers rather than explicit Boolean values. However, this can make code harder to read and understand, especially for beginners. It’s generally better practice to use explicit comparisons (like x != 0) so the logic is clear. Some languages will throw an error or warning if Boolean operators are used incorrectly with numbers, while others will silently convert them. Understanding how your chosen language handles truthy and falsy values is key to avoiding logic errors when mixing numbers and Boolean operators.

When NOT is combined with AND or OR in a complex Boolean expression, it reverses the truth value of the condition it is applied to, which can change the entire logic of the statement. For example, NOT (A AND B) means the expression is true if at least one of A or B is false, effectively turning the whole condition into the opposite of A AND B. This is a key concept known as De Morgan’s Laws in logic, which state that NOT (A AND B) is equivalent to (NOT A) OR (NOT B) and NOT (A OR B) is equivalent to (NOT A) AND (NOT B). Understanding these laws is important for simplifying or rewriting complex logical conditions. If you don’t use parentheses correctly, NOT may only apply to one part of the expression rather than the whole condition, leading to unintended results. Always use parentheses to clarify the intended logic.

Operator precedence determines the order in which different operators are evaluated in an expression. If you use arithmetic and Boolean operators together without parentheses, the programming language will follow its predefined precedence rules, which can lead to unexpected results if not carefully considered. For example, arithmetic operators like multiplication and division are evaluated before addition and subtraction, while Boolean NOT has higher precedence than AND, which itself has higher precedence than OR. In mixed expressions, arithmetic operators are usually evaluated before Boolean operators unless parentheses are used to explicitly control the order. A misunderstanding of these rules can cause a program to perform calculations in an incorrect sequence, leading to logical errors or incorrect output. Therefore, it’s a best practice to use parentheses even when you know the precedence, to make your code’s logic clearer to both the computer and human readers reviewing your work.

The symbol == is a comparison operator that checks if two values are equal, while = is an assignment operator that assigns a value to a variable. This difference is crucial when writing expressions that include Boolean operators. For example, if you accidentally write if x = 5 AND y = 10: instead of if x 5 AND y 10:, the program will attempt to assign values instead of comparing them, which may cause a syntax error or unintended behavior depending on the programming language. Using = inside a condition might even overwrite variable values, breaking the program’s logic. Always remember that comparisons for equality in conditional statements require ==, while = is only used outside of conditions to set variable values. Mixing these up is a common error for beginners, so careful attention to operator use is essential for writing accurate Boolean expressions.

Hire a tutor

Please fill out the form and we'll find a tutor for you.

1/2
Your details
Alternatively contact us via
WhatsApp, Phone Call, or Email