Visual Programming

20

Transcript of Visual Programming

Page 1: Visual Programming
Page 2: Visual Programming

A visual programminglanguage (VPL) is any programminglanguage that lets users createprograms by manipulating programelements graphically rather than byspecifying them textually.

A VPL allows programming withvisual expressions, spatialarrangements of text and graphicsymbols, used either as elementsof syntax or secondary notation.

Page 3: Visual Programming

The Basics: How Programming Works

:How a programming language works, along with basic terminology.

Representing Words, Numbers, and Values with Variables

:How variables store values and represent information, along with how to use variables.

Page 4: Visual Programming

On its own, a computer isn't very smart.

A computer is essentially just a big bunch of small electronic switches that are either on or off. By setting different combinations of these switches, you can make the computer do something:

for example, display something on the screen or make a sound. That's what programming is at its most basic—telling a computer what to do.

Page 5: Visual Programming

s

A programming language acts as a translator between you and the computer. Rather than learning the computer's native language (known as machine language), you can use a programming language to instruct the computer in a way that is easier to learn and understand.

Page 6: Visual Programming

What is a Programming Language?

A programming language acts as a translator between you and the computer. Rather than learning the computer's native language (known as machine language), you can use a programming language to instruct the computer in a way that is easier to learn and understand.

A specialized program known as a compiler takes the instructions written in the programming language and converts them to machine language. This means that as a Visual Basic programmer, you don't have to understand what the computer is doing or how it does it. You just have to understand how the Visual Basic programming language works.

Page 7: Visual Programming

The language you write and speak has structure: for example, a book has chapters with paragraphs that contain sentences consisting of words. Programs written in Visual Basic also have a structure: modules are like chapters, procedures are like paragraphs, and lines of code are like sentences.

When you speak or write, you use different categories of words, such as nouns or verbs. Each category is used according to a defined set of rules. In many ways, Visual Basic is much like the language that you use every day. Visual Basic also has rules that define how categories of words, known as programming elements, are used to write programs.

Page 8: Visual Programming

Written and spoken language also has rules, or syntax, that defines the order of words in a sentence. Visual Basic also has syntax—at first it may look strange, but it is actually very simple. For example, to state "The maximum speed of my car is 55", you would write:

Car.Speed.Maximum = 55

Page 9: Visual Programming

Variables are an important concept in computer programming. A variable is a letter or name that can store a

value. When you create computer programs, you can use variables to store numbers, such as the height of a building, or words, such as a person's name. Simply put, you can use variables to represent any kind of information your program

needs.

Page 10: Visual Programming

There are three steps to using a variable:

Declare the variable. Tell the program the name and kind of variable you want to use.

Assign the variable. Give the variable a value to hold.

Use the variable. Retrieve the value held in the variable and use it in your program.

Page 11: Visual Programming

Declaring a Variable

When you declare a variable, you have to decide what to name it and what data type to assign to it. You can name the variable anything that you want, as long as the name starts with a letter or an underscore. When you use a name that describes what the variable is holding, your code is easier to read. For example, a variable that tracks the number of pieces of candy in a jar could be named totalCandy

Page 12: Visual Programming

You declare a variable using the Dim and As keywords, as shown here.

This line of code tells the program that you want to use a variable named aNumber, and that you want it to be a variable that stores whole numbers (the Integer data type).

Because aNumber is an Integer, it can store only whole numbers. If you had wanted to store 42.5, for example, you would have used the Double data type. And if you wanted to store a word, you'd use a data type called a String. One other data type worth mentioning at this point is Boolean, which can store a True or False value.

VBDim aNumber As Integer

Page 13: Visual Programming

Here are more examples of how to declare variables.

Note:You can create a local variable without declaring the type of the variable by using local type inference. When you use local type inference, the type of the variable is determined by the value that is assigned to it. For more information, see Local Type Inference.

VBDim aDouble As DoubleDim aName As StringDim YesOrNo As Boolean

Page 14: Visual Programming

You assign a value to your variable with the = sign, which is sometimes called the assignment operator, as shown in the following example.

This line of code takes the value 42 and stores it in the previously declared variable named aNumber.

VBaNumber = 42

Page 15: Visual Programming

Declaring and Assigning Variables with a Default Value

As shown earlier, you can declare a variable on one line of code, and then later assign the value on another line. This can cause an error if you try to use the variable before assigning it a value.

For that reason, it is a better idea to declare and assign variables on a single line. Even if you don't yet know what value the variable will hold, you can assign a default value. The code for declaring and assigning the same variables shown earlier would look like the following.

Page 16: Visual Programming

VB

Dim aDouble As Double = 0Dim aName As String = "default string"Dim YesOrNo As Boolean = True

Page 17: Visual Programming

Try it!In this exercise, you will write a short program that creates

four variables, assigns them values, and then displays each value in a window called a message box. Let's begin by creating the project where the code will be stored.

To create the project1. If it is not already open, open Visual Basic from the Windows Start menu. 2. On the File menu, click New Project. 3. In the New Project dialog box on the Templates pane, click Windows Forms Application.4. In the Name box, type Variables and then click OK.

Visual Basic will create the files for your program and open the Form Designer.

Next, you'll create the variables.

Page 18: Visual Programming

To create variables and display their values1.Double-click the form to open the Code Editor.

The Code Editor opens to a section of code called Form1_Load. This section of code is an event handler, which is also referred to as a procedure. The code that you write in this procedure is the instructions that will be performed when the form is first loaded into memory.2. In the Form1_Load procedure, type the following code.

VB

Dim anInteger As Integer = 42Dim aSingle As Single = 39.345677653Dim aString As String = "I like candy"Dim aBoolean As Boolean = True

This code declares four variables and assigns their default values. The four variables are an Integer, a Single, a String, and a Boolean.

Tip: As you typed the code, you may have noticed that after you typed As, a list of words appeared underneath the cursor. This feature is called IntelliSense. It enables you to just type the first few letters of a word until the word is selected in the list. Once the word is selected, you can press the TAB key to finish the word.

Page 19: Visual Programming

3. Beneath the code you wrote in the previous step, type the following.

This code tells the program to display each value that you assigned in the previous step in a new window, using the MsgBox function.

4. Press F5 to run your program. Click OK for each message box as it appears. Note that the value of each

variable is displayed in turn. You can close the form by clicking the x in the upper-right corner of the form. After the program has finished, you can go back and change the values that are assigned in the code—you'll see that the new values are displayed the next time that you run the program.

VB

MsgBox(anInteger)MsgBox(aSingle)MsgBox(aString)MsgBox(aBoolean)

Note:Whenever you represent actual text in a program, you must enclose it in

quotation marks (""). This tells the program to interpret the text as actual text instead of as a variable name. When you assign a Boolean variable a value of True or False, you do not enclose the word in quotation marks, because True and False are Visual Basic keywords with special meanings of their own.

Page 20: Visual Programming