LOGO

Computer Programming 101: Variables and Data Types

October 23, 2011
Computer Programming 101: Variables and Data Types

Fundamentals of Computer Programming: Variables and Datatypes

Previously, we explored the concepts behind Object Oriented Programming and its origins. Now, it’s time to establish a foundation in the core principles of computer programming, presented in a manner independent of any specific programming language.

This discussion mirrors the introductory coursework typically encountered by computer science students in their initial semester. The intention is to make these concepts accessible to individuals with no prior programming experience.

Understanding Variables

At the heart of any programming language lie variables and datatypes. These are essential building blocks for storing and manipulating information within a computer program.

Variables can be thought of as named storage locations in the computer’s memory. They allow us to assign values, such as numbers or text, that can be used and modified throughout the program’s execution.

Exploring Datatypes

A datatype defines the kind of value a variable can hold. Different datatypes are suited for representing different types of information.

Common datatypes include:

  • Integers: Whole numbers (e.g., -5, 0, 10).
  • Floating-point numbers: Numbers with decimal points (e.g., 3.14, -2.5).
  • Strings: Sequences of characters, representing text (e.g., "Hello", "Programming").
  • Booleans: Represent truth values, either true or false.

The specific datatypes available can vary slightly depending on the programming language being used, but these are universally fundamental.

Why are Datatypes Important?

Specifying the correct datatype for a variable is crucial. It ensures that the computer allocates the appropriate amount of memory and performs operations correctly.

For example, attempting to perform mathematical operations on a string would typically result in an error, highlighting the importance of datatype consistency.

We will continue to build upon these fundamental concepts in subsequent lessons before moving on to practical coding examples. This staged approach aims to provide a solid understanding of the underlying principles.

Variables and Data Types:

Every program fundamentally relies on variables to function. These variables serve as storage locations for dynamic information. Consider entering your name into a web form – that name is then held as a variable.

However, variables aren't uniform. Most programming languages incorporate a diverse range of variable types. Let's examine a few common ones, along with their abbreviations where applicable:

Character (char): This represents a single character, such as Z, $, 7, or #. While standalone character variables aren’t frequently used, they form a core component of the language’s structure.

String: A string is a sequence of characters – any length will do. Referring back to the web form example, your inputted name would be saved as a String variable.

Integer (int): This data type represents a whole number, meaning it contains no fractional part. For instance, 82 is a valid integer, but 82.55 is not.

Floating-point number (float): This represents a number that can include digits after a decimal point. 82.00 is considered a floating-point number, even though it could also be represented as the integer 82. Floats require more memory, hence the distinction instead of a single “number” type.

Boolean (bool): A variable designed to hold a true or false value (or equivalently, 0 or 1, on or off). This is a simple, yet frequently used datatype – become familiar with it!

Array: Arrays are essentially lists containing other variables. Different languages offer various array types, but fundamentally, they are sequential collections of variables. An example would be 9,10,11,12,13, which could be stored as an array (with a length of 5) of integer variables. Each variable within the array can be accessed using an index; importantly, the first item is at index 0. Utilizing arrays simplifies the handling of variable collections within a program, enabling operations like counting elements or applying the same action to each item (an iteration, which we will discuss later). It’s also worth noting that a string is, in essence, an array of characters.

That was a lot of information to process. Feel free to revisit this section if needed. If anything remains unclear, please let me know in the comments.

basics-of-computer-programming-variables-datatypes-1.jpg

Strong vs. Weak Typing in Programming Languages

Programming languages are broadly categorized based on their typing systems: strongly-typed and weakly-typed. Strongly-typed languages, like Java, enforce strict rules regarding variable types.

These languages necessitate explicit declaration of a variable's type during creation. Attempts to perform invalid operations on a variable will result in errors.

For instance, a strongly-typed language will flag an error if you attempt to combine an integer with a string using mathematical addition. It questions the logical consistency of adding a numerical value to textual data.

Despite a human's understanding that a string “5” holds the same numerical value as an integer with the value of 5, the language maintains its type integrity.

Conversely, weakly-typed languages adopt a more permissive approach. They will attempt to execute operations regardless of type compatibility, without raising immediate objections.

The outcome of such operations in a weakly-typed language is unpredictable. The result of “5+5” could be 10, or it could be “55”, demonstrating the lack of strict type enforcement.

While weakly-typed languages may appear simpler initially, they can introduce subtle bugs and unexpected behavior that are challenging to debug. This is due to the implicit type conversions that occur.

Key Differences Summarized

  • Strong Typing: Requires explicit type declarations and strict adherence to type rules.
  • Weak Typing: Allows implicit type conversions and more flexible operations.
  • Error Handling: Strong typing catches type errors early, while weak typing may lead to runtime surprises.

Ultimately, the choice between a strongly-typed and weakly-typed language depends on the specific project requirements and developer preferences.

Understanding Assignment vs. Equality

This concept isn't related to political ideologies. Instead, it’s a fundamental distinction that often trips up those new to programming, and it’s important to clarify it now. A key difference exists between assigning a value and checking for equality. Both of the following statements might appear to mean the same thing – “A is equal to 5” – but they do not:

A = 5;

A == 5;

Do you recognize the contrast? The first example demonstrates assignment. It instructs the program to assign the value of 5 to the variable A. Essentially, you are defining the variable’s value.

The second statement, however, represents a test for equality. It poses a question – “Is A equal to 5?” – and the result will be a boolean value, either true or false. This subtle difference can lead to errors in your code as you progress.

This concludes today’s discussion. If any part of this explanation was unclear, please feel free to ask questions in the comments section. I’m happy to rephrase or provide alternative explanations. Our next lesson will cover functions and return values, followed by an exploration of loops and iteration.

Image Credits: ShutterStock 1, 2

#computer programming#programming 101#variables#data types#beginner programming#coding basics