Share via


Small Basic Curriculum: Lesson 1.2: Statements, Properties, and Operations

Small Basic > Curriculum >** **Online > Lesson 1.2: Statements, Properties, and Operations

Estimated time to complete this lesson: 1 hour. Allow more time to practice and gain deeper understanding.

Statements, Properties, and Operations

In this lesson, you will learn about:

  • Statements in Small Basic programs;
  • Properties of the TextWindow object; and
  • Operations of the TextWindow object.

Statements in Small Basic Programs

To instruct the computer to do what you want it to do, you write a statement. A program can be created by writing just one statement or more than one statement in a particular sequence.

For example, you can instruct the computer to open a text window and write "Hi, Everyone" in it. To give this instruction, you write the following statement in the Editor:

http://msdn.microsoft.com/gg557983.New%20Picture(en-us,MSDN.10).png

One ore more statements make up a program in Small Basic. The computer runs a program by reading and understanding each statement line by line.

For example, the above screen shows the statement: TextWindow.WriteLine("Hi, Everyone")

This statement tells the computer to write the line of text, Hi, Everyone, in the text window.

Showing and Hiding the Text Window

The text window is an object, and you can instruct the computer to perform operations on that object. For example, you can display the TextWindow object by using the Show operation.

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

First, type TextWindow.Show() in the Editor, and then click Run on the Toolbar. The result of your program is that the text window appears.

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

This is the output you will see: 

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

Similarly, you can hide the TextWindow object by using the Hide operation.

Properties of the TextWindow Object

The TextWindow object has a specific set of properties, they are: ForegroundColor, BackgroundColor, Title, CursorTop, CursorLeft, Top, and Left. You can use these properties to change how and where the TextWindow object appears.

TextWindow.Title = "Fun with Small Basic Programming"TextWindow.Left = 300TextWindow.Top = 300TextWindow.BackgroundColor = "Gray"TextWindow.ForegroundColor = "Red"TextWindow.CursorLeft = 30TextWindow.CursorTop = 20

This is the output you will see: 

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

In the previous screen, we talked about an object called TextWindow. Small Basic contains many more objects. These objects have certain properties, operations, or events attached to them.

To visualise, Properties are something the object has and Operations are things that the Object does.

Let’s understand the properties and operations of a text window and how we can use them.

In the code that appears here, you set the background color of the TextWindow object to gray and the foreground color to red. Similarly, you specify a title for the text window, the position of the cursor in the text window, and the location where you want the window to appear on the screen.

To better understand this code, let’s check the output of your program after you run it!

As you see in the output, the default text Press any key to continue… appears in red with a gray background. This is because you set the BackgroundColor property of the TextWindow to Gray and the ForegroundColor property to Red.

Notice that the title of the text window is Fun with Small Basic Programming. You specified this title by using the Title property of the TextWindow object. 

The CursorTop property indicates the row position of the cursor in theTextWindow , and the CursorLeft property indicates the column position of the cursor. You had set these values by using the CursorTop and CursorLeft properties. 

Similarly, the Top property and the Left property indicate the top position and the left position of the TextWindow on the screen.

Operations of the TextWindow Object

For the TextWindow object, you can specify the following operations:

  • Show
  • Hide
  • Write
  • WriteLine
  • Read
  • Pause
  • Clear

Let’s explore some of these operations…

In addition to the Show and Hide operations, you can also perform other actions or operations on the TextWindow object.

Writing Text in the Text Window

You have already learned how to show and hide the text window. Now let’s see how you can write text in the TextWindow object.

TextWindow.Write("Hi, everyone!")TextWindow.Write("How are you doing?")

 

This is the output you will see: 

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

You can write text in the text window by using the Write operation of the TextWindow object.

As you see, this operation wrote both sentences on the same line with no space between them. But don’t worry: you can use a different operation to show these sentences on separate lines.

TextWindow.WriteLine("Hi, everyone!")TextWindow.WriteLine("How are you doing?")

 

This is the output you will see: 

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

To write the text on separate lines, you use the WriteLine operation.

Write and WriteLine are both operations of the TextWindow object. If you use the WriteLine operation, each line of text appears on a separate line.

Reading a Line of Text

Would you like the computer to ask for your name and a friend’s name and then say "Hello" to both of you? Let’s see how to make that happen. First, let's start with asking for your name, and then displaying it.

TextWindow.Write("Enter your name: ")name = TextWindow.Read() TextWindow.WriteLine("Hello " + name + ".")

 

If you use the Read operation, the computer reads and remembers what a user has typed. If you use the WriteLine operation, the computer displays the information from the user. Next, see if you can figure out how to program Small Basic how to ask for your name and the name of a friend, and then have it display both names.

This is the output you will see: 

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

The Read operation of the TextWindow object takes no input. This operation instructs the computer to wait while the user types in text and then presses ENTER. After the user presses ENTER , the program reads what the user has typed and stores it in memory. You can then use the Write operation or the WriteLine operation to display the stored information.

The Pause and Clear Operations

  • In the following example if you use the Pause operation, the computer waits for user input before returning the final output.

Let’s write a program to better understand these operations.

TextWindow.WriteLine("Hi") TextWindow.Pause()TextWindow.Clear() TextWindow.WriteLine("Hello")

 

After you clear "Hi" away, this is the output you will see: 

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

When the program starts, the computer reads the first statement, which contains the first WriteLine operation. The computer opens the text window and writes the word “Hi” in it.

The computer then reads the second statement, which contains the Pause operation. The computer pauses and waits for user input.

After the user provides input, the computer reads the third statement, which contains the Clear operation. The computer clears the word “Hi” from the text window.

Finally, the computer reads the last statement, which contains the second WriteLine operation. The computer writes the word “Hello” in the text window.

Let’s Summarize…

Congratulations!

Now you know how to:

  • Write statements for programs in Small Basic.
  • Change various properties of the TextWindow object, such as its title and its location.
  • Use various operations of the TextWindow object, such as Show and WriteLine.

Show What You Know

Write a program to display a text window and perform the following steps:

  • Set the top position of the text window to 100, and set its left position to 200.
  • Write a statement that makes “Small Basic Programming” appear in the title bar of the text window.
  • Set the top position of the cursor to 10 and left position to 20.
  • Set the foreground color of the text to yellow.
  • Display the sentence, “Welcome to the world of Small Basic programming.”

To see the answers to these questions, go to the  Answer Key page or Import using its Program ID: TJN263.

Next Lesson

 

PowerPoint Downloads