A-Level Computing. An Introduction to Visual Basic Programming

A-Level Computing Summer Work An Introduction to Visual Basic Programming You will need Visual Basic 2010 Express to complete these programming tasks...
Author: Arthur Ryan
4 downloads 1 Views 466KB Size
A-Level Computing Summer Work

An Introduction to Visual Basic Programming You will need Visual Basic 2010 Express to complete these programming tasks. Go here to download it: http://bit.ly/11SqkQf (If it does not download, then download and install the 2012 version, but the screenshots in the tutorials will be different)

cothamschool

ICT & COMPUTING DEPARTMENT

creative

inclusive

confident

respectful

ambitious

Visual Basic Programming - Introduction 1.1 Creating a New Visual Basic Console Application 1.

Select FILE> NEW > PROJECT

2.

Select CONSOLE PROGRAMMING, and then enter the name of your project to in the text box at the bottom of the dialog box.

cothamschool

ICT & COMPUTING DEPARTMENT

creative

inclusive

confident

respectful

1.2 Writing your First piece of code and Running it 3.

In between the text SUB MAIN() and END SUB enter CONSOLE code shown below: Console.WriteLine("Hello and Welcome to My First Visual Basic Program") Console.ReadLine()

Your code window should now look like this:

4.

Run the program by clicking the PLAY symbol on the toolbar

You should now see the following output to the screen:

1.3 Exercises 1.

Change the program so that it displays your name and address underneath the first line of text

2.

Change the program so that a blank line appears between the first line and your name and address

3.

Change the program so that the user has to press ENTER to view each line of text.

ambitious

cothamschool

ICT & COMPUTING DEPARTMENT

creative

inclusive

2.0 Opening Programs using Console Continue using the above program. The following code can be used to open the NOTEPAD application: Process.Start("notepad") 1.

Change your program so that it opens NOTEPAD at the end of the program.

The following code can be used to open Internet Explorer: Process.Start("iexplore") 2.

Change the program so that opens INTERNET EXPLORER after opening NOTEPAD

Exercises 1.

What other programs can you open in this way? Do some research and find out.

2.

Can you find out how to open internet explorer to a website you specify?

confident

respectful

ambitious

cothamschool

ICT & COMPUTING DEPARTMENT

creative

inclusive

confident

respectful

ambitious

02 – Variables Visual Basic Programming Variables are used to store (save) information while the program is running. This information can then be recalled and used by the program at a later time.

1.1 Text Variables In this example program you will store the users name in a variable and then display their name on the screen. An example of the final screen output is shown below:

1.

Create a New Visual Basic Console Program and call it “Variables 1” (refer to previous tutorial for how to do this)

You must setup a variable before it can be used by the program 2.

In between the text SUB MAIN() and END SUB type in the below code to set up a variable called “firstname”. The variable is setup to store text data only.

Dim firstname As String The DIM word tells visual basic that you are declaring a variable. This is the name of the variable. You can call it whatever you like as long as long as it is one word

Tells visual basic what type of data is going to be stored in the variable. The keyword STRING means text data

Next we need to print the text “Input your name” to the top of the screen 3.

Type in the following code to ask the user to enter their name: Console.WriteLine("Input your name")

cothamschool

ICT & COMPUTING DEPARTMENT

creative

inclusive

confident

respectful

ambitious

We now need to sort what user has typed in. We do this by storing the data into the “firstname” variable we created earlier 4.

Type in the following code to store the users answer inside the “firstname” variable you just setup: firstname = Console.ReadLine

Finally we need to output the users name to the screen, by displaying the contents of variable “firstname” 5.

Now type in the following code to output the contents of your variable to the screen:

Console.WriteLine("Your name is " & firstname) Console.ReadLine()

& = and

Your final code should now look like this:

6.

Run the program by clicking the PLAY button on the toolbar to test the code. You screen output should be similar to what is shown below when the program has completed:

1.2 Exercises 1.

Comment your code so that it is clear what is going on

2.

Ask the user to enter their favourite colour and then display it on the screen

3.

Ask the user to enter their age and then display it on the screen

cothamschool

ICT & COMPUTING DEPARTMENT

creative

inclusive

confident

respectful

ambitious

2.1 Number Variables In this example program you will get the user to enter two numbers and then multiply them together and show the result. An example of the final screen output is shown below:

1.

Create a New Visual Basic Console Program and call it “Variables 2”. (refer to previous tutorial for how to do this)

Now we must setup two NUMBER variables to store each of the numbers that the user enters. This means we must declare INTEGER variables rather than the STRING variables we used in the previous example. 2.

In between the text SUB MAIN() and END SUB type in the following code to set up two number variables called “firstnumber” and “secondnumber”: Dim firstnumber As Integer Dim secondnumber As Integer The keyword INTEGER means store number data

Next we must get the user to enter two different numbers. The first number the user enters is to be stored in the variable “firstnumber”. The second number the user enters is to be stored in the variable “secondnumber”. 3.

Now type in the following code to store two numbers that the user enters: Console.WriteLine("Enter first number. It must be below 100,000") firstnumber = Console.ReadLine Console.WriteLine("Enter second number. It must be below 100,000") secondnumber = Console.ReadLine

The final stage is to multiple the numbers together and then display the result on the screen. 4.

Type in the following code to multiple the two numbers together and prints the result to the screen: Console.WriteLine("The answer is..") Console.WriteLine(firstnumber * secondnumber) Console.ReadLine()

Write out the result of multiplying the values stored in the variables.

* symbol = multiply

cothamschool

ICT & COMPUTING DEPARTMENT

creative

inclusive

confident

respectful

ambitious

Your final code should now look like this:

5.

Run the program by clicking the PLAY button on the toolbar to test the code. You screen output should be similar to what is shown below when the program has completed:

2.2 Extension Tasks 1.

Alter your program so that the two numbers are added together. The symbols for each mathematical operator are listed below •

Addition = +



Subtraction = -



Multiplication = *



Division = /

2.

Alter you program so that a third number is entered and them multiplied by the total of the first two numbers

3.

Alter the above program so that a fourth number is entered. The fourth number should then be taken away from the total of the first three numbers.

cothamschool

ICT & COMPUTING DEPARTMENT

creative

inclusive

confident

respectful

ambitious

Selection Statements VB.NET Programming

Selection statements enable you to set up a section of code that may or may not get executed depending on whether a condition is true or false. IF statements are the first type of selection statements will we will look at. You can follow the below instructions or follow the accompanying video tutorial.

3.1 IF statements In this example program you will store the user’s age in a variable and then tell them whether they are old enough to drive or not depending on their age. An example of the final screen output is shown below:

1.

Create a New Visual Basic Console Program and call it “IF Statements 01” (refer to the first tutorial for how to do this)

The first thing to do is to get and store the users age 2.

In between the text SUB MAIN() and END SUB type in the below code to get the users age and store the answer in a NUMBER variable called “age”: Dim age As Integer Console.WriteLine("please enter your age") age = Console.ReadLine

We must now set up the IF statement that determines which code to execute depending on the users age. If the user is over 16 a message is displayed on the screen. IF statements must always take on the following structure: If condition is true Then Perform commands based on true result End If 3.

Type in the following code to setup an IF statement that tests the users age and then executes specific code that depending on the value: If the value store in variable “age” is greater than 16 then execute the below code

If age > 16 Then Console.WriteLine("You are old enough to drive") End If Close \ End the if statement

cothamschool

ICT & COMPUTING DEPARTMENT

4.

creative

inclusive

confident

respectful

ambitious

Run the program by clicking the PLAY button on the toolbar to test the code. You screen output should be similar to what is shown below when the program has completed:

You will notice if the user is not over 16 then the program does not output any message. We can fix this by including an ELSE statement into the IF statement structure. If condition is true Then perform commands based on true result Else perform commands based on false result End If

5.

Change your IF statement code so that it looks like the code below: If age > 16 Then Console.WriteLine("You are old enough to drive") Else Console.WriteLine("you are too young to drive") End If

6.

Run the program by clicking the PLAY button on the toolbar to test the code. You screen output should be similar to what is shown below when the program has completed:

Now the program outputs a message if the user is over or under 16. But nothing happens if the user is 16. We can fix this by including an ELSEIF statement into the IF statement structure. 7.

Change your IF statement code so that it looks like the code below: If age > 16 Then Console.WriteLine("You are old enough to drive") ElseIf age = 16 Then Console.WriteLine("you are too young, but you have only a year to go!") Else Console.WriteLine("you are too young to drive") End If

cothamschool

ICT & COMPUTING DEPARTMENT

creative

inclusive

confident

respectful

ambitious

Your final code should look like this:

8.

Run the program by clicking the PLAY button on the toolbar to test the code. You screen output should be similar to what is shown below when the program has completed:

3.2 Exercises 1.

Write a new program that asks the user to enter an exam mark from 0 to 100. Display the grade it represents: Merit(60 or more), Pass (40-59), Fail (under 40)

2.

Write a program that decides whether you are let into a nightclub, they are only letting in females that are over 18. Use the AND statement, then use a nested if statement

Suggest Documents