Share via


Small Basic Curriculum: Lesson 1.3: Variables

Small Basic > Curriculum >** **Online > Lesson 1.3: Variables

Estimated time to complete this lesson: 1 hour

Variables

In this lesson, you will learn how to:

  • Define and name a variable.
  • Use variables to store text or numbers.
  • Use arrays to store multiple values.

What is a Variable?

You can use a variable to store different kinds of information, such as text or a number. A variable can contain different values at different points in time. Most variables can contain only one value at a time. However, special variables, which are called arrays, can contain more than one value. Let’s look at a program in which you create a variable to store the user’s name.

TextWindow.Write("What is your name? ")
name = TextWindow.Read()
TextWindow.WriteLine("Hello, " + name + ".")

 

In this example, your program asks the user to type his or her name. Your program uses a variable that is called "name“ to store the information.

When you run your program, it displays “Hello” and then the information in the variable.

Click the buttonhttp://msdn.microsoft.com/gg597501.5(en-us,MSDN.10).png on the Toolbar.

This is the output you will see: 

http://msdn.microsoft.com/gg558113.2(en-us,MSDN.10).png

A variable temporarily stores a value that your program can use later. If you run a program again but assign a different value to the variable, the new value in the variable replaces the old value.

When you run a program, the value in the variable is also used as you specify in the code. You can reuse a variable as many times as your program requires.

How to Name a Variable?

You should follow these rules and guidelines when you create your variables.

  • You should always start variable names with a letter.
  • You can use letters, digits, and underscores in the names of your variables.
  • You should name your variables so that they describe the values that they store.
  • When you name your variables, you should not include certain reserved words, such as If, For, and Then.
number_1 = 20
number_2 = 30
number_sum = number_1 + number_2
TextWindow.WriteLine(number_sum)

 

This is the output you will see: 

http://msdn.microsoft.com/gg558113.4(en-us,MSDN.10).png

You can identify your variables more easily if you give them suitable names.

Storing Numerical Values In a Variable

To better understand the variables that store numbers, let’s write a simple program that calculates the area and perimeter of a rectangle.

TextWindow.Title = "Area and Perimeter"
TextWindow.Write("How long is the rectangle? ")
length = TextWindow.ReadNumber()
TextWindow.Write("How wide is the rectangle? ")
width = TextWindow.ReadNumber()
area = length * width
perimeter = 2 * length + 2 * width
TextWindow.WriteLine("The area of the rectangle is " + area + ".")
TextWindow.WriteLine("The perimeter of the rectangle is " + perimeter + ".")

 

The program asks the user to specify the length and width of the rectangle. When the user presses ENTER, the program calculates and displays the area and perimeter values of the rectangle.

This is the output you will see: 

http://msdn.microsoft.com/gg558113.6(en-us,MSDN.10).png

In the previous example, you used a variable to store text, which is also known as a string. Now let’s see an example of how to store numerical values in variables.

In this example, you use the Write operation to ask the user to specify the length and width of a rectangle. You also create variables that you name length and width, and you use the ReadNumber operation to instruct the computer to store the user’s answers in those variables. Then you create variables that you name area and perimeter, and you instruct the computer to calculate the area and perimeter of the rectangle and store those values in the appropriate variables. Finally, you use the WriteLine operation to display the results.

After you write the program, you can run it by clicking Run on the Toolbar or by pressing F5.

Storing Multiple Values In a Variable

You can store multiple values of the same type in a single variable by using an array. An array is a type of variable that can hold more than one value at a time. Let’s look at an example that uses an array.

TextWindow.Title = "Array Object"
TextWindow.Write("What is the name of the first student? ")
students[1] = TextWindow.Read()
TextWindow.Write("What is the name of the second student? ")
students[2] = TextWindow.Read()
TextWindow.Write("What is the name of the third student? ")
students[3] = TextWindow.Read()
Textwindow.WriteLine("Is ‘Robin' here? " + Array.ContainsValue(students, "Robin"))

 

In this example, you create an array that you name students, and you store three different names in it. You can then retrieve the stored values by using the various operations of the Array object.

This is the output you will see:

http://msdn.microsoft.com/gg558113.8(en-us,MSDN.10).png

In the example on this slide, you define an array that will contain names of students. Then you use an operation of the Array object to search the array for a specific value.

Let’s Summarize…

Congratulations!

Now you know how to:

  • Create and name variables, and write statements that contain variables.
  • Use variables to store text or numbers.
  • Use arrays to store multiple values of the same type.

Show What You Know

Write a program that calculates the area and circumference of a circle based on its diameter:

  • Ask the user to specify the diameter of a circle.
  • Create a variable that is called diameter, and store the value from the user in it.
  • Create a variable that is called radius, calculate the circle’s radius, and store the result in that variable.
  • Create variables that are called area and circumference, calculate the area and circumference of the circle, and store the values in those variables.
  • Display the area and circumference of the circle.

To see the answers to these questions, go to the Answer Key page.

Next Lesson

       

PowerPoint Downloads