Mt. San Jacinto College Computer Information Systems
Minimize header.

CSIS 111B: Fundamentals of Computer Programming: Lessons

Lesson 5

Simple Data Types, Operators and Expressions

Objectives

By the end of this lesson you will be able to:

  • Understand what a variable is and how it is used in computer programming
  • Understand what a constants isand how to use it in computer programming
  • Understand how data types determine size requirements for storing data in memory
  • Describe the various data types
  • Declare and initialize storage structures for storing values in memory
  • Write code, using the name/identifier of the storage location, which reads from and writes to these storage structures
  • Understand usage of regular expressions in coding patterns
  • Understand usage of special characters in coding patterns
  • Understand string interpolation and how to use it in your code
Table of Contents

Introduction to Values, Data Types, Variables, and Constants

In this lesson you will learn how to declare and initialize storage structures for storing values in computer memory. You can read and write to these strucutres by referring to the memory location in your code using the name/identifier of the storage location where the value is stored. One of the most common procedures you will do as a computer programmer is to insert references in your code by using the name of a memory location instead of hard coding the value into the program's code. This makes your code more flexible in that different values can be used without changing the program's code.

Computer's determine how much space to reserve in memory for storing values based on the type of value that is being stored. Meaning the data type that is created will have reserved for it a pre-determined number of bits in memory, based on the number of bits that type of data needs. For instance an integer like the number nine can be represented in memory using only 4 bytes, which you might know is 4 X 8 bits or 32 bits.

A single letter typed from a keyboard (known as a character or char) only required 8 bits in the original microcomputers. At that time microcomputer character encoding schemes were based on a translation table known as "ASCII" (pronounced ass-key). Nowdays microcomputers use 16 bits (or 2 bytes) to represent the Unicode character encoding set. It wouldn't make sense to to reserve 32 bits of computer memory just for storing characters typed from the keyboard, that would be wasting 16 bits of memory every time a char needed to be stored in memory.

Table of Contents

Values

A value is a discrete quantity of data. The members of a type are the values of that type. Computer languages vary in their implementation of primitive value types, but some examples would be null, undefined, true/false, and numeric values, like the following: 1, 3.1459, "Chuck", 'D', true.

Data Types

A data type or simply type is a classification of data which tells the compiler or interpreter how the programmer intends to use the data. Data types specify the type of data that you work with in a program. The data type defines the size of memory needed to store data and the kinds of operations that can be performed on the data. Most computer programming languages support various types of data, for example: real, integer, string, or boolean. A data type provides a set of values from which an expression (i.e. variable, function...) may take its values. The type defines the operations that can be done on the data, the meaning of the data, and the way values of that type can be stored. Data types are grouped into value types and reference types. Value data types store the value on the stack, whereas reference data types write the memory address on the stack which points to where the data value begins in dynamic memory (the heap).

Modern-day programming languages have basic data types built-in to them; these are generally referred to as their primitive data types. Meaning, the programmer does not have to write the code that defines the data type, its allowed operations, etc. Usually a simple statement like:

            int myVariable;

is all that is needed to declare an integer variable to the compiler or interpreter using any modern-day programming language.

Data Types By Programming Language

Click on the links below to view the specific data types for the designated programming language.

C++ Fundamental Types

Data types in C++ are mainly divided into two types:

Primitive Data Types: These data types are built-in or predefined data types and can be used directly by the user to declare variables. example: int, char , float, bool etc. Primitive data types available in C++ are:

  • Integer
  • Character
  • Boolean
  • Floating Point
  • Double Floating Point
  • Valueless or Void
  • Wide Character

Abstract or user defined data type: These data types are defined by user itself. Like, defining a class in C++ or a structure.

JavaScript Built-In Data Types

In JavaScript, a primitive (primitive value, primitive data type) is data that is not an object and has no methods. There are 6 primitive data types: string, number, boolean, null, undefined, symbol (new in ECMAScript 2015).

Most of the time, a primitive value is represented directly at the lowest level of the language implementation.

All primitives are immutable, i.e., they cannot be altered. It is important not to confuse a primitive itself with a variable assigned a primitive value. The variable may be reassigned a new value, but the existing value can not be changed in the ways that objects, arrays, and functions can be altered.

Python Built-In Types

Python has four primitive data types:

  • Integers
  • Float
  • Strings
  • Boolean
C# Data Types

C# provides several built-in data types by way of the .NET framework which can be used in your programs. You can also define new data types by defining your own data structure, such as a class or a struct.

C# Built-In Types Table on Microsoft Docs.

In this lesson we focus on some of the most commonly used data types.

Figure 2: C# Data Types
Data Type Size Range Of Values
byte 1 byte 0 to 255
char 2 bytes U+0000 to U+ffff (Unicode characters)
short 2 bytes -32,768 to 32,767
int 4 bytes -2,147,483,648 to 2,147,483,647
long 8 bytes -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
float 4 bytes ±1.5 X 10-45 to ±3.4 X 1038
double 8 bytes ±5.0e-324 to ±1.7e308
bool 2 bytes True or False
string - zero or more unicode characters

All of the data types in the table above are value types except for string, which is a reference type. The variables which are based on value types directly contain the value on the stack. Data types which are of type reference store a reference on the stack which points to a memory address on the heap where the value is actually stored. This will make more sense after you learn about arrays, stacks, heaps, and other data structures later on in this course. For now, just remember that the string type is a reference type and NOT a value type.

The MSDN Web site has a more comprehensive table showing the C# integral data types. which is a subset of the simple types. You might also want to review Types for a more comprehensive overview of C# types.

Table of Contents

Variables

Pictorial analogy of a variable putting the number 23 into a box.
Figure 1: Conceptual representation of a variable.

Variables are a way of creating a temporary area of storage in computer memory for storing data while a program is running. Variables are named storage locations that hold values. We can then retrieve the value stored in memory by using the name (a.k.a. identifier) we assign to it when declaring the variable. Think of a variable as a placeholder or a box of a specific size which we can write a name on to help us to recall the data programmatically at a later time. For instance let's declare a variable named age and then later we'll store a value in it.

To do this we must follow the C# required format, data-type name; like this:

int age;

In the C# language we are required to include one of the C# data types provided by the .NET framework to let the compiler know what kind of value it should expect when we assign a value to it. The data type also determines what kinds of operations can be performed on it. In our example we used int which represents a 32-bit value which can only be a whole number, either positive or negative. By informing the compiler that our variable will be used to store a 32-bit integer value, the compiler now knows how many bits of space it needs to reserve in memory for holding the value when we assign data to it. You can determine a C# variable's data type like this variable_name.GetType();

Should we forget and try to assign text as a value for our variable or another numeric data type like a float data type, say 23.5, the compiler will let us know that this is not allowed while we are writing our program instead of us finding out the hard way, later on after the program has been compiled and being run by our customer. This saves a lot of time and money.

For more information on the data types available for use in the C# language, read the section titled Data Types.

You can also initialize our variable (assign a value to it) at the same time we declare it by adding the C# assignment operator (=) followed by a value, like this:

Dissection of a variable.

Notice how asignment goes from right to left. Whether you are initializing a variable or just modifying its current value, assignments always go from right to left. Once our variable is declared or initialized, we can change the value of our variable at any time by simply writing an assignment statement using the name of the variable, the assignment operator, and a new value, like this:

    //variable = expression
    age = 45;

Beginning in Visual C# 3.0, variables that are declared at method scope can have an implicit type var. An implicitly typed local variable is strongly typed just as if you had declared the type yourself, but the compiler determines the type. The following two declarations of i are functionally equivalent:

        var i = 10; //implicitly typed
        int i = 10; //explicitly typed 

Python is a dynamically typed language like JavaScript in that the interpreter infers the data type at run-time based on the value assigned to the variable. For instance, age = 20 would give the inference that age is an int. However, if we declare age like this age = "20" then the inference is that age should be a string type. You can test this in Python using the type() function, like this: type(variable_name).

In JavaScript the keyword var is used to dynamically declare a variable's data type, for example: var age = 20;. When the var keyword is not used in the declaration of a JavaScript variable, the variable is considered global in scope even if the declaration appears inside of a structure like a for loop.

Creating Variables
Table of Contents

Constants

Constants are immutable values which are known at compile time and do not change for the life of the program. Constants are declared with the const modifier. Only the C# built-in types (excluding System.Object) may be declared as const. Example:

    const int i = 20;

In this example a constant named i is declared which stores an integer data type of the value 20. Once declared the value of the const cannot be changed.

Python and JavaScript do not have the concept of a constant, instead global variables in which values cannot be changed are used instead.

Table of Contents Table of Contents

String Data Type

In computer programming, a string data type is a data type which contains an array of char (character) data types.

Using Strings In JavaScript
String Properties

The string type in C# has two properties:

Chars[index]
Gets the Char object at a specified position in the current String object.
Length
The number of characters in the current string.
String Methods

The string type in C# has numerous methods: Clone, Compare, CompareOrdinal, CompareTo, Concat, Contains, Copy, CopyTo, Create, EndsWith, EnumerateRunes, Equals, Format, GetEnumerator, GetHashCode, GetTypeCode, IndexOf, IdexOfAny, Insert, Intern, IsInterned, IsNormalized, IsNull Or Empty, IsNullOrWhiteSpace, Join, LastIndexOf, LastIndexOfAny, Normalize, PadLeft, PadRight, Remove, Replace, Split, StartsWith, Substring, ThCharArray, ToLower, ToLowerInvariant, ToString, ToUpper, ToUpperInvariant, Trim, TrimEnd, and TrimStart (as of .NET Framework 4.8).

Int Data Type

In computer programming, a int data type is a data type which contains an integer value.

Using Int In JavaScript
Table of Contents

Operators

You have already seen a couple of C# operators in action (= and new). In this section we will discuss C# operators in more depth.

Operators are symbols that specify which operations (math, indexing, function call, etc.) to perform on the operands in an expression. Operands can be variables, constants, literals, etc. Operators can be organized by function or by number of operands it operates on. If we organize operators by function, each operator would  fall into one of three categories, 1) arithmetic, 2) boolean, or 3) relational. Examples of arithmetic operators include: +, -, *, /, and %. If we organize operators by the number of operands they operate on then the groupings would be: 1) unary operators, 2) binary operators, and 3) ternary operators.

Operators can be organized depending on the number of operands involved, like this:

Unary operators: The unary operators work with only one operand. Examples include ++x (prefix increment), x++ (postfix increment), or new.

Binary operators: Binary operators take two operands. Examples include x + y or x > y

Ternary operators: Ternary operators take three operands. There is just one ternary operator in C# ?:. Pseudo code example:

condition ? expression_if_true : expression_if_false;

Operators can also be organized by how they are used, like this:

Arithmetic operators can be used to create expressions like:

x = 1 + 1;

Conditional operators can be used to creae expressions like:

if (x < 2)

Logical operators can be used to create expressions like:

if (x<2 && y == 3)

Often expressions involve more than one operator. In this case the compiler needs to determine which operator takes precedence over the other(s). Figure 3 lists the C# operators in order of precedence (pretty much the same in all computer programming languages). The higher an operator is located in the table, the higher the precedence. Operators with higher precedence are evaluated before operators with lower precedence. Operators that appear in the same row have equal precedence.

Figure 3: C# Operators
Category Operators
Primary x.y f(x) a[x] x++ x-- new
typeof checked unchecked
Unary + - ! ~ ++x --x (T)x
Multiplicative * / %
Additive + -
Shift << >>
Relational and type testing <> <= >= is as
Equality == != === !==
Logical AND &
Logical XOR ^
Logical OR |
Conditional AND &&
Conditional OR ||
Conditional ternary ?:
Assignment = *= /= %= += -= <<= >>= ^= |=

The unary increment operator (++) adds 1 to the value of the an identifier (variable). Similarly, the decrement (--) operator subtracts 1 from the value of the identifier. The unary increment and decrement operator can be used as prefixes or suffixes. For example:

    int x = 10;
    x++; //value of x is now 11
    ++x; //value of x is now 12
        

However, when used as part of an assignment the results can be unexpected. When the unary increment and decrement operators are used as prefixes (++x), the current value of the identifier is returned after (post) the increment or decrement operation. When used as a suffix (x++), the value of the identifier is returned before (pre) the increment decrement operation is complete. To better understand how this works let's look at another example:

    int x = 10;
    int y = x++ //value of y is 10 (assigned pre-increment)
    int z = ++x //value of z is 12 (assigned post-increment)
        

In this example, because y is assigned the value of x pre-increment y's value is 10. However x is increment after the assignment to y so x's value is now 11. Then, z is assigned the value of x post-increment so both x and y now have the value of 12. Fun stuff, eh?

If this example boggles you brain, I encourage you to copy this console application code into Visual Studio and try it out for yourself:

    using System;

    namespace IncrementDecrementOperators
    {
        class Program
        {
            static void Main(string[] args)
            {
                int x = 10;
                int y = x++; //value of x is now 11
                Console.WriteLine("x = " + x + ", y = " + y);
                int z = ++x; //value of x is now 12
                Console.WriteLine("x = " + x + ", z = " + z);
            }
        }
    }
        
Summary of Assignment, Comparison, and Logical Operators
Addition Vs. Concatenation
Table of Contents

Regular Expressions

Table of Contents

Special Characters

In the C# language you can use the @ symbol as a verbation identifier and the $ sign character for string interpolation (starting with version 6). In the String Variables code example below you will use string interpolation when displaying the output and you will see the verbatim identifier used in Lesson 11 File I/O to assist with overriddng the default nature of the backslash character to be treated as an escape character. Interpolated strings are replacements for the composite format strings supported by the .NET Framework.

The @ special character serves as a verbatim identifier. It can be used in the following ways:

  1. To enable C# keywords to be used as identifiers. The @ character prefixes a code element that the compiler is to interpret as an identifier rather than a C# keyword. The following example uses the @ character to define an identifier named for that it uses in a for loop.
        string[] @for = { "John", "James", "Joan", "Jamie" };
        for (int ctr = 0; ctr < @for.Length; ctr++)
        {
           Console.WriteLine($"Here is your gift, {@for[ctr]}!");
        }
        // The example displays the following output:
        //     Here is your gift, John!
        //     Here is your gift, James!
        //     Here is your gift, Joan!
        //     Here is your gift, Jamie!
  2. To indicate that a string literal is to be interpreted verbatim. The @ character in this instance defines a verbatim string literal. Simple escape sequences (such as "\\" for a backslash), hexadecimal escape sequences (such as "\x0041" for an uppercase A), and Unicode escape sequences (such as "\u0041" for an uppercase A) are interpreted literally. Only a quote escape sequence ("") is not interpreted literally; it produces a single quotation mark. Additionally, in case of a verbatim interpolated string brace escape sequences ({{ and }}) are not interpreted literally; they produce single brace characters. The following example defines two identical file paths, one by using a regular string literal and the other by using a verbatim string literal. This is one of the more common uses of verbatim string literals.
        string filename1 = @"c:\documents\files\u0066.txt";
        string filename2 = "c:\\documents\\files\\u0066.txt";
    
        Console.WriteLine(filename1);
        Console.WriteLine(filename2);
        // The example displays the following output:
        //     c:\documents\files\u0066.txt
        //     c:\documents\files\u0066.txt

String interpolation provides a more readable and convenient syntax to create formatted strings than a string composite formatting feature. The following example uses both features to produce the same output:

    string name = "Mark";
    var date = DateTime.Now;

    // Composite formatting:
    Console.WriteLine("Hello, {0}! Today is {1}, it's {2:HH:mm} now.", name, date.DayOfWeek, date);
    // String interpolation:
    Console.WriteLine($"Hello, {name}! Today is {date.DayOfWeek}, it's {date:HH:mm} now.");
    // Both calls produce the same output that is similar to:
    // Hello, Mark! Today is Wednesday, it's 19:40 now.
        
Table of Contents

String Variables Code Example

C#

Here is how the code in our Program.cs file will look. Place the code inside the Main() method and run it.

        string x, y, z;

        Console.Write("Enter x: ");
        x = Console.ReadLine();
        Console.Write("Enter y: ");
        y = Console.ReadLine();
        Console.Write("Enter z: ");
        z = Console.ReadLine();

        Console.WriteLine($"x={x}, y={y}, z={z}");
Code Dissected
sample code dissection showing variable declaration, assignment of input values to variables, and output of varaible values.
Table of Contents

Summary

In this lesson you learned what a variable is and how it is used in computer programming, what a constants is and how to use it in computer programming, how data types determine size requirements for storing data in memory, how to declare and initialize storage structures for storing values in memory, how to write code using the name/identifier of the storage location, how to read from and writes to these storage structures, about regular expressions used in coding patterns, about usage of special characters in coding patterns, and what string interpolation is and how to use it in your code.

Table of Contents