JMA201-01
Fundamentals of Application Development

DEPARTMENT SCHEDULE SYLLABUS RESOURCES
Basic Statements and Order of Operations

Today, we're going to look at some basic Visual Basic syntax.

Syntax

Visual Basic (VB) has several basic syntax rules you need to follow.

  1. Individual lines are usually called statements. A statement is a complete instruction; typically, a statement occurs on one line, followed by an Enter.
  2. A statement in VB almost always occurs within an event handler.
  3. Object Naming: We will be given the ability to name things in VB (for example, variables and functions). When we name things, our names must follow these rules:
  4. White space. Blank spaces and line breaks are ignored (unless they are encountered within a line). We should use this to our advantage to format our pages for readability of our source code. Indent looping sections, and put line breaks around "chunks" of code.

Comments

Comments are lines that contain the single quote character ('). We can place this character anywhere in a line; any text after this line will be ignored. Comments allow us to temporarily prevent a line of code from being executed; they also allow us (or our team members) to build "to do" lists or remind ourselves what we were trying to accomplish. You should get in the habit of writing 1-2 line comments for each structure.

Expressions

An expression is something that needs to be simplified. Expressions can come in many places. For example, if I gave you the problem "What is 5+3+1+4?" you could tell me the answer: 13. You simplified the values joined together with operators (in this case the pluses - we will discuss more operators in the next lecture) to produce one final value. It is important to remember that expressions always simplify down to one value. We will see expressions in many places - within if statements and other statements, for example. Expressions are similar to algebra - the computer is attempting to solve for one value.

Quite a lot of programming involves figuring out how to translate a real world formula into an expression that can be solved. For example, a quick search of the Internet discovered the formula for calculating the Body Mass Index. Here it is:

BMI = (Weight In Pounds times 703) Divided by Height in inches squared

To write a program to solve this based on user input, we would write an expression in a statement. But before we do this, we need to recognize something you may remember from high school math - the Order of Operations for calculations.

Order of Operations

Math calculations are not performed left to right. A frequently forgotten element of programming is the fact that no matter how complex a statement might be, a computer can only solve one portion of it at a time. For example - if we had the calculation:

3+4/2

You should realize you can't just mush it all up and solve it in one pass. You have two separate operations that might be happening here - 3+4, or 4/2, and then you use the answer from the first part to complete the second part. Now, the confusing part is that depending on the order in which we do this, we get a different answer... if we did 3+4 and got 7, then divide that by 2, we get 3.5. However, if we did the division first, we divide 4 by 2 and get 2, plus 3 is 5. So, based on the order in which we complete each individual stage, we get a different answer.

So, we have a saying (a mnemonic) to help us understand how to solve complex equations.

Please Excuse My Dear Aunt Sally

This saying reminds us of the order of operations for math expressions. It stands for:

Our mnemonic tells us which order to solve the problem. We do not just read left to right as if it were an English sentence. Multiplication comes before addition in the mnemonic (My comes before Aunt), therefore, we must multiply before we add.

So, because we understand that the order of operations can change your answer, we need to properly use it in an expression. Returning to our BMI example, we are going to write a bit of pseudocode (English-like instructions) to help us prepare to write a program. First, I'm going to rename values in a more VB-like way - remember, we can't use spaces to represent things! A common programming trick is to squish multiple words together, capitalizing the first letter of each successive word for readability.

usersBMI=(usersWeightInPounds*703) /heightInInches^2

Notice I have multiple sets of parenthesis in this expression. If there are multiple sets of parenthesis, you solve the innermost set first. Order of Operations tells me the first thing to solve would be content in parenthesis:

usersWeightInPounds *703

Next, we would take this answer, plug it back into the formula, and look for the next thing on the list - in this case, we do have an exponent - heightInInches squared:

heightInInches^2

We would then take the end result of these and divide the first answer by the second. So, if I plugged in some values to my expression and watched each successive pass of the expression, we can see the problem being solved. Using these sample values:

We will now plug in these values to our formula. Each bullet point represents a step of the complex equation being solved according to order of operations: