Programming Data Types

Programming Data Types

In this tutorial we will discuss the various data types available in programming and how they are used.  My biggest problem in programming was to connect the theory to the practical. The theory of programming is always covered in great detail however it is not often linked with the practical parts of it. So here we are trying to bridge the gap. In MooICT you will notice we have plenty of programming projects in various different languages and we are using various different data types such as strings, integers, floats or Booleans. It’s time to cover them in detail here so we can have a better understanding of what they are and what can they do.

The table below will go over each of the data types and give you a brief understanding of it. Off course we do not expect you to know it all from here but it will be a stepping stone to your next great adventure if you will. Get to know the tools in programming and pretty soon you will carve your own master piece.

Data Type

Description

Example

String Variable that can contain words and numbers. Holds sequences of unsigned 16-bit (2-byte) code points that range in value from 0 through 65535. Each code point, or character code, represents a single Unicode character. A string can contain from 0 to approximately two billion (2 ^ 31) Unicode characters.

Note –

Strings can hold numbers but they are presented as characters not numeric values for example if you have

numberA = 3;

numberB = “3”;

console.WriteLine ( numberA+numberB);

What will this total variable hold?

Result will be 33, why?

Because the compiler looks at numberA and says ok this has a number and it looks into numberB and says it has a character (every String is displayed inside a quotation easy to spot) so it will output 3 and “3” together, side by side instead of adding them to 6.

Visual Basic –

Dim name as String = “James Franko”

Dim website as String =  “www.mooict.com

C#

Public String name = “James Franko”

Public String website = “www.google.com

Change the string

name = “Captain Deadpool”

website = “deadpool.com”

The above method is acceptable in C# and in visual basic.

Integer Whole numbers

Holds signed 32-bit (4-byte) integers that range in value from -2,147,483,648 through 2,147,483,647.

Note –

Integers cannot hold decimal numbers they can only hold whole numbers such as 1, 2,3 or -2, -4

If you have a number that contains any decimal points such as 2.3 or 0.44 it will throw an error. For decimal numbers you will need to use Double, Float, Single or Real variable.

Visual Basic –

Dim age as Integer = 25

Dim numberA as Integer = 3

Dim numberB as Integer = 3

Dim total as Integer = numberA + number

Now total will be equals to 6 since number A and b have value of 3. If either one of those variables were strings then it will return an error.

C#

Int age = 25

Int numberA = 3

Int number = 3

Int total = numberA + numberB

Returns 6 in total.

Double The Double data type provides the largest and smallest possible magnitudes for a number. In simple terms this is a variable that can hold very large positive/negative decimal number.

It can hold -1.79769313486231570E+308 through -4.94065645841246544E-324 for negative numbers and  from 4.94065645841246544E-324 through 1.79769313486231570E+308 for positive numbers.

double: 5.0 × 10-324 to 1.7 × 10308

C#  Sharp

Double money = 500.45

Double weight = 56.99

Double interestRate = 6.8

Double total = money * interestRate

(Returns 3403.04)

VB – Visual Basic

Dim money as Double = 500.45

Dim weight as Double = 56.99

Dim interestRate as Double = 6.8

Dim dub As Double = 4.0R

Syntaxes are all similar.

Character (Char) Char is a variable that can store between 0 to 65535. Now you might be thinking it’s similar to the integer variable however each number in char actually represents a CHARACTER such as A, B, C or a, b, c etc. This is A 16-BIT variable saved size comes to a 2 byte.  VB

Dim associatedChar As Char ‘

Returns “A”.

associatedChar = Chr(65)’

Returns “a”.

associatedChar = Chr(97)’

Returns “>”.

associatedChar = Chr(62)’

Returns “%”.

associatedChar = Chr(37)’

Returns “X”.

C# Sharp

char value = ‘a’; C# Loop// Loop over all lowercase letters.

for (char c = ‘a’; c <= ‘z’; c++){  Console.Write(c);  Console.Write(‘ ‘);}

Float The float type is stored as a four-byte, single-precision, floating-point number. It represents a single-precision 32-bit IEEE 754 value. This type is useful for applications that need large numbers but do not need precise accuracy.  float: 1.5 × 10-45 to 3.4 × 1038 C# Sharp

int x = 3;float y = 4.5f;

Visual basic uses double and singles which contains similar functionality as single.

Boolean Contains only two different values. Holds one of two possible values. Simply True or False, Yes or No, On or Off, etc. You can see here how the computer would allocate very little memory for a Boolean declaration. Visual Basic

Dim awake As Boolean

awake = false

awake = true

C# sharp

Bool awake;

awake = true;

awake = false;

Time/Date This variable can hold the date and time for an action needed in the software. It can hold 0:00:00 on January 1, 0001 through 11:59:59 PM on December 31, 9999.

You can do various tasks with this data type such as calculating the current date in an application or even creating a countdown to an event.

Visual Basic

Dim thisDate As DatethisDate = Today

C sharp

DateTime date1 = DateTime.Now;DateTime date2 = DateTime.UtcNow;DateTime date3 = DateTime.Today;

Byte 0 through 255 (unsigned). The default value of byte is always 0. You are able to give it a value between 0 and 255. Bytes are generally used to save space in an large array (mainly in an array of integers) since they are 4 times smaller than other variables or data types. Visual Basic

Dim b As Byte

b = 30

B can’t be -30 or 256 because bytes only can go between 0 to 255.

C# Sharp Example

byte myByte = 255;

myByte variable cannot hold value such as 3.0 because it has a decimal number in it. You can however convert that 3.0 to a byte given the following command

myByte = (byte)3.0; This method will convert the double number 3.0 to a single byte 3

Single Single data types can only hold 7 number values; if you attempt to add a 12 digit number it will round it up to 7 numbers. Single data types are fine for simple programs that do not include complex mathematical coding. Single is a number with one decimal place at all times. For example 2.456.

Microsoft Explanation of Single –

Holds signed IEEE 32-bit (4-byte) single-precision floating-point numbers ranging in value from -3.4028235E+38 through -1.401298E-45 for negative values and from 1.401298E-45 through 3.4028235E+38 for positive values. Single-precision numbers store an approximation of a real number.

C# Sharp Example

Single value = .2f;

Single result1 = value * 10f;

Single result2 = 0f;

 

Visual basic uses double and singles which contains similar functionality as single

Arrays The array is a complex data type which handles a collection of elements. Each of the elements can be accessed by an index. All the elements of an array must be of the same data type. Arrays can hold multiple values of the same data types such as multiple strings, integers or even objects. Arrays are very important in programming doesn’t matter which language you are learning. Understanding arrays early can only be of benefit. C# Sharp Example

int[] numbers = new int[5];

numbers[0] = 3;

numbers[1] = 2;

numbers[2] = 1;

numbers[3] = 5;

numbers[4] = 6;

int len = numbers.Length;

for (int i=0; i<len; i++)

{
Console.WriteLine(numbers[i]);

Visual Basic Example

Dim numbers(4) As Integer Dim numbers = New Integer() {1, 2, 4, 8

}

}




Comments are closed.