Jump to content

Programming Fundamentals/Variables/Arithmetic Operators

From Wikiversity

The basic arithmetic operations are addition, subtraction, multiplication, and division. Arithmetic is performed according to an order of operations.[1] This activity introduces arithmetic operators and order of operations. This activity will help you understand how to use arithmetic operators to perform accurate calculations.

Objectives

[edit | edit source]
  • Understand and recognize arithmetic operators.
  • Understand order of operations.

Prerequisites

[edit | edit source]
  • Review Pressbooks: Programming Fundamentals - Arithmetic Operators.
  • Review MathsIsFun: Order of Operations.
  • An integer data type represents whole numbers with no fractional parts.[2]
  • An operator is a character or symbol used to perform an arithmetic, logical, or bitwise operation on one or more operands.[3]
  • String concatenation is the joining of character strings end-to-end.[4]
  • An assignment statement sets and/or re-sets the value stored in the storage location(s) denoted by a variable name; in other words, it copies a value into the variable.[5]
  • In integer division and modulus, the dividend is divided by the divisor into an integer quotient and a remainder. The integer quotient operation is referred to as integer division, and the integer remainder operation is the modulus.[6][7]
  • The order of operations (or operator precedence) is a collection of rules that reflect conventions about which procedures to perform first in order to evaluate a given mathematical expression.[8]
  • The order of operations used throughout mathematics, science, technology and many computer programming languages is:[9]
    1. exponentiation and root extraction
    2. multiplication and division
    3. addition and subtraction

Introduction

[edit | edit source]
Data types
Data types

Review the flowchart example on the right.

Questions

[edit | edit source]
  • What flowcharting symbol is used to indicate the beginning and ending of a program?
  • What flowcharting symbol is used to indicate annotations or comments?
  • What flowcharting symbol is used to indicate variable declaration?
  • What flowcharting symbol is used to indicate input or output?
  • What flowcharting symbol is used to indicate a process, calculation, or assignment?
  • What key word is used to declare an integer variable?
  • What operator is used for assignment?
  • What operator is used for string concatenation?
  • What operator is used for addition, subtraction, multiplication, division, and modulus?

Activity

[edit | edit source]

With a partner, perform the following:

  1. Using a visual programming language, create a program matching the flowchart on the right.
  2. Save the program.
  3. Test the program to verify that it works correctly.
  4. Trade places, so that both partners have an opportunity to "drive" the visual programming environment.
  5. Copy the program and change it so that it demonstrates order of operations and operator precedence.
  6. Save the program.
  7. Test the program to verify that it works correctly.

Applications

[edit | edit source]
  • Identify steps to follow when writing programs using arithmetic operations.
  • Identify opportunities to use integer division and the modulus operator.
  • Discuss your activity experience with your classmates. What surprised you? What have you learned that you can apply to your own school or work environment?

References

[edit | edit source]