Skip to main content

Section 2.4 Data type

Learning Objective: Student will be able to...
  • Identify the correct data type for a piece of data

However, once you get that information from the user you need to store it somewhere for later use. Where? This is where the need for variables comes to play. Variables can be thought of as data containers that store data values that can be changed from time to time. There are many types of variables that hold different types of data. The table below shows a list of some of the most common.

Table 2.4.1.
Integer A whole number that is not a fraction. Such as 2,67, -89, or 56
Real A number that is a fraction. Such as -1.45, 5.55, 3.00
String A sequence of characters. Such as “Hello”, “What is your name”
Boolean A true or false value

Exercises Exercises

1. Matching Problem, Date Types.

Learning Objective: Student will be able to...
  • Declare and assign variables

Well now we know where to store our data but how do we implement that in our program? Most languages will require you to declare a variable before you use it. The declaration may look like this

Datatype variable_name → Integer amount

The example above shows a variable declaration in which the variable data type is indicated in this case Integer and the variable name is indicated in this case “amount”. This allows your program to know that the variable “amount” will store data that is an integer. Storing another data type in “amount” like a String will cause an error.

Did you know: not all languages require variable declaration before use. For example Python, does not require variable declaration

Now let’s go back to our example. Let’s prompt the user to provide the number of students for the 3 classes and store them in variables

Exercises Exercises

1.

Why did we use integer?

Activity 2.4.1.

Integer class1
Integer class2
Integer class3

Display “Enter the number of students in class 1”
class1 = Input (the single equal sign indicates assignment)
Display “Enter the number of students in class 2”
class2 = Input (assignment)
Display “Enter the number of students in class 3”
class3 = Input (assignment)

Integer class1 Integer class2 Integer class3 Display “Enter the number of students in class 1” class1 = Input Display “Enter the number of students in class 2” class2 = Input Display “Enter the number of students in class 3” class3 = Input (the single equal sign indicates assignment)

Activity 2.4.2. Activity Coding Exercise.

Save and Run the below program.

Answer.
That is data assignment
You have attempted of activities on this page.