Sunday, 6 October 2013

VB Lesson 3: If and Select Statements

Yo, lesson 3 of VB now. Exciting times. The examples used in this post may cross over with lesson 2, because, as explained earlier, I crammed most of the lessons I'd missed into one whole session and so there's sort of a blurred line when it comes to which program goes with which lesson. But that doesn't matter, as each lesson is supposed to build on the knowledge from the last, so really it just shows the ideals of teaching and learning at their very best.

Visual Basic Lesson 3:

Once again we didn't really go through the worksheet for this one, but that wasn't due to not having it, it was just because it is easier to understand this lesson through practical examples.

Sometimes, when a user is inputting data into a program, they won't always give information which is applicable to a particular piece of code and so you need a statement which can differentiate between the useful data and the useless data. For example, if a program is designed to check whether a person can drive in the UK, it would be pointless if the program always stated "You can legally start driving" as it's Output, despite a user giving their age as one which is under 17. Therefore you need a statement which can choose between two or more bits of coding and give the correct Output. This is where selection statements come into play.

Selection statements are for situations when a condition has to be (be true) in order for the program to execute a selected bit of code.

For example:
Dim age As Integer

age = InputBox("How old are you?")

If age < 17 Then
MsgBox"Too young"
ELSE
MsgBox"You can start driving!"
End if

Things to note here:
 You must always start and end an "If statement"
You must give a condition such as "if age < 17" and once the condition is finished add "then" or "else" etc...

For this lesson Lalin set me some examples of his own, which tied in with lesson 2.

For example there is the relational operator program with an If statement:

There is the circle area program:

And the surface area of a washer program:

These programs all encompass at least one element of the past three lessons. 

Now I believe I have covered everything in lesson 3, if not I'm sure it'll be covered in lesson 4, because, as I have previous stated many times, I did do a bunch of the lessons all in one go and so some of this may be in a muddle.

Until next time :).

No comments:

Post a Comment