Estimated time to complete this lesson: 1 hour
In this lesson, you will learn how to:
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 button on the Toolbar.
This is the output you will see:
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.
You should follow these rules and guidelines when you create your variables.
number_1 = 20 number_2 = 30 number_sum = number_1 + number_2 TextWindow.WriteLine(number_sum)
You can identify your variables more easily if you give them suitable names.
To better understand 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.
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.
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.
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.
Congratulations!
Now you know how to:
Write a program that calculates the area and circumference of a circle based on its diameter:
To see the answers to these questions, go to the Answer Key page.
Next Lesson