Integer

Programming Fundamentals
definition

Also known as: int, whole number type

Grade 6-8

View on concept map

A data type that represents whole numbers (positive, negative, or zero) without decimal points. Choosing the right data type prevents bugs.

Definition

A data type that represents whole numbers (positive, negative, or zero) without decimal points. Integers are stored exactly in memory and support arithmetic operations like addition, subtraction, multiplication, and integer division.

๐Ÿ’ก Intuition

Integers are counting numbers in code โ€” things you can count without fractions: age, score, quantity, index.

๐ŸŽฏ Core Idea

Integers are exact and efficient. Use them for counting and indexing. Use floats/decimals when you need fractions.

Example

age = 15 (integer). temperature = -5 (integer). price = 9.99 (NOT an integer โ€” that's a float/decimal).

๐ŸŒŸ Why It Matters

Choosing the right data type prevents bugs. Integer division (7 / 2 = 3) behaves differently from decimal division (7.0 / 2.0 = 3.5). Integers are used for loop counters, array indices, quantities, and any value that must be exact without rounding.

๐Ÿ’ญ Hint When Stuck

When deciding whether to use an integer or a float, ask: does this value ever need a decimal point? If you are counting items, indexing arrays, or tracking scores, use an integer. If you need fractions or precise decimal values, use a float or decimal type.

Formal View

The integer type represents elements of \mathbb{Z} (or a bounded subset depending on bit width). A 32-bit signed integer represents values in [-2^{31}, 2^{31}-1].

๐Ÿšง Common Stuck Point

Integer division truncates in many languages: 7 / 2 gives 3, not 3.5. This catches many beginners by surprise.

โš ๏ธ Common Mistakes

  • Expecting 7 / 2 to give 3.5 when using integer division, which truncates to 3 in many languages
  • Causing integer overflow by exceeding the maximum value a fixed-width integer can hold
  • Using integers for values that need decimal precision, like currency calculations

Frequently Asked Questions

What is Integer in CS Thinking?

A data type that represents whole numbers (positive, negative, or zero) without decimal points. Integers are stored exactly in memory and support arithmetic operations like addition, subtraction, multiplication, and integer division.

When do you use Integer?

When deciding whether to use an integer or a float, ask: does this value ever need a decimal point? If you are counting items, indexing arrays, or tracking scores, use an integer. If you need fractions or precise decimal values, use a float or decimal type.

What do students usually get wrong about Integer?

Integer division truncates in many languages: 7 / 2 gives 3, not 3.5. This catches many beginners by surprise.

Prerequisites

Next Steps

How Integer Connects to Other Ideas

To understand integer, you should first be comfortable with assignment and data types. Once you have a solid grasp of integer, you can move on to string and boolean.