Programming booklet

Page 1

Programming Challenge


VB Programming Simple Output The Console is a special window that we “write to” or “read from”

Statements Used WRITELINE WRITE FOREGROUNDCOLOR BACKGROUNDCOLOR

The Program Module Module1 'A simple Program to display text on the console 'B Monk 'September 2017 Sub Main() Console.WriteLine("Hello World") Console.Write("Hello") Console.WriteLine("World") Console.ForegroundColor = ConsoleColor.Red Console.BackgroundColor = ConsoleColor.Yellow Console.WriteLine("This is coloured text!") Console.ReadLine() End Sub End Module


Programming Challenge cd


VB Programming Simple Variables A variable is an identifier that refers to a location in the computer’s memory and can be used to store data. A variable needs: An identifier A type A value

Statements Used Dim

The Program Module Module1 'A simple Program to create variables 'B Monk 'September 2017 Sub Main() Dim wholenumber As Integer Dim realnumber As Single Dim text As String Dim letter As Char Dim isfound As Boolean Dim birthday As Date wholenumber = 42 realnumber = 3.1415 text = "Hello World" letter = "x" isfound = True birthday = "12/03/1978" Console.WriteLine("A whole number is " & wholenumber) Console.WriteLine("A real number is " & realnumber) Console.WriteLine("text") Console.WriteLine(text) Console.WriteLine("A character is " & letter) Console.WriteLine("Isfound is " & isfound) Console.WriteLine("My birthday is " & birthday) Console.ReadLine() End Sub End Module


Programming Challenge cd


VB Programming Simple Input Input is the process of entering (or reading) data from the keyboard and storing it in a variable. A “storage� variable must be created before the data is entered and must be of a suitable type (integer, single, string, ect)

Statements Used READLINE

The Program Module Module1 'A simple Program to perform simple input 'B Monk 'September 2017 Sub Main() Dim num1 As Integer Dim num2 As Integer Console.WriteLine("Please enter a number: ") num1 = Console.ReadLine() Console.WriteLine("Please enter another number: ") num2 = Console.ReadLine() Console.WriteLine("The first number is " & num1) Console.WriteLine("The second number is " & num2) Console.ReadLine() End Sub End Module


Programming Challenge cd


VB Programming Simple Calculations One of the most important features of a program is the ability to perform calculations. Programs can perform simple arithmetic or complex (above A-Level Maths) calculations

Operator Used + Addition - Subtraction * Multiplication / Division ^ To the power

The Program Module Module1 'A simple Program to perform simple calculations 'B Monk 'September 2017 Sub Main() Dim num1 As Integer Dim num2 As Integer Dim result As Single Console.WriteLine("Please enter a number: ") num1 = Console.ReadLine() Console.WriteLine("Please enter another number: ") num2 = Console.ReadLine() result = num1 + num2 Console.WriteLine("The sum is " & result) result = num1 * num2 Console.WriteLine("The product is " & result) result = num1 ^ 2 Console.WriteLine(num1 & " squared is " & result) Console.ReadLine() End Sub End Module


Programming Challenge cd


VB Programming Formatting the Output Formatting means to change the way something is displayed. You can format variables so that they are shown with a given number of decimal places, symbols, such as £, and as a percentage. When formatting numbers, you create a “style”. This style uses: 0 – To display a number or 0 # - To display a number or nothing For dates d – for day m – for month y – for year Statements Used FORMAT([variable] , [style])

The Program Module Module1 'A simple Program to show how to format data 'B Monk 'September 2017 Sub Main() Dim price As Single Dim birthday As Date price = 3.5 birthday = "12/03/1978" Console.WriteLine(Format(price, Console.WriteLine(Format(price, Console.WriteLine(Format(price, Console.WriteLine(Format(price, Console.WriteLine(Format(price, Console.WriteLine(Format(price,

"0.0")) "##0.0")) "0000.00")) "##0.00")) "£##0.00")) "0.00%"))

Console.WriteLine(Format(birthday, "yyyy")) Console.WriteLine(Format(birthday, "dddd")) Console.ReadLine() End Sub End Module


Programming Challenge cd


VB Programming Random Numbers It is often useful to have the computer generate a random number. Most programming languages give functions to create random numbers. The RND() function generates a real (single) random number >= 0 and <1 Competitive Football Statements Used RND() RANDOMIZE INT

Program1

Program3

Module Module1 'Random Number 'B Monk 'September 2017 Sub Main() Dim num1 As Single

Module Module1 'Random Number 'B Monk 'September 2017 Sub Main() Dim num1 As Single Randomize()

num1 = Rnd() Console.WriteLine(num1) Console.ReadLine() End Sub End Module

Program2 Module Module1 'Random Number 'B Monk 'September 2017 Sub Main() Dim num1 As Single Randomize() num1 = Rnd() Console.WriteLine(num1) Console.ReadLine() End Sub End Module

num1 = Int(Rnd() * 50) Console.WriteLine(num1) Console.ReadLine() End Sub End Module


Programming Challenge cd


VB Programming Selection – The IF THEN Statement One of the most powerful features of any computer program is the ability to make decisions, or to select sections of code to execute, based on a condition.

Statements Used IF [condition] THEN [Statements] END IF

Conditional Operators = Equal <> Unequal > Greater than < Less than >= Greater than or equal <= Less than or equal

The Program Module Module1 'A simple Program to perform simple decision making 'B Monk 'June 2015 Sub Main() Dim num1 As Integer Dim num2 As Integer Console.WriteLine("Please enter a number: ") num1 = Console.ReadLine() Console.WriteLine("Please enter another number: ") num2 = Console.ReadLine() If num1 = num2 Then Console.WriteLine(num1 & " is equal to " & num2) End If If num1 <> num2 Then Console.WriteLine(num1 & " is not equal to " & num2) End If If num1 > num2 Then Console.WriteLine(num1 & " is greater than " & num2) End If If num1 < num2 Then Console.WriteLine(num1 & " is less than " & num2) End If End Sub End Module


Programming Challenge cd


VB Programming Selection – The IF THEN ELSE Statement Often it is necessary to make a decision based on one thing or another, e.g. if you are older than 17 then you can drive, otherwise you can’t.)

Statements Used IF [condition] THEN [Statements] ELSE [Statements] END IF The Program Module Module1 'A Program to demonstrate IF THEN ELSE 'B Monk 'June 2015 Sub Main() Dim num1 As Integer Dim num2 As Integer Console.WriteLine("Please enter a number: ") num1 = Console.ReadLine() Console.WriteLine("Please enter another number: ") num2 = Console.ReadLine() If num1 = num2 Then Console.WriteLine(num1 & " is equal to " & num2) Else Console.WriteLine(num1 & " is not equal to " & num2) End If If num1 > num2 Then Console.WriteLine(num1 & " is greater than " & num2) Else Console.WriteLine(num1 & " is less than or equal to " & num2) End If End Sub End Module


Programming Challenge cd


VB Programming Logical Operators Sometimes, you will need to write complex conditions, such as “If the number is greater than 0, but less than 100 then ……” For these we need to use logical operators (remember logic gates?)

Conditions <Variable> <Conditional Operator> <Operand> Age = 17 Score < 80 MenuChoice <> “X”

Logical Operators Used AND OR

The Program Module Module1 'A Program to demonstrate AND OR logical operators 'B Monk 'June 2015 Sub Main() Dim num1 As Integer Console.WriteLine("Please enter a number: ") num1 = Console.ReadLine() If num1 >= 0 And num1 <= 100 Then Console.WriteLine(num1 & " is a valid percentage") End If If num1 < 0 Or num1 > 100 Then Console.WriteLine(num1 & " is not a valid percentage") End If Console.ReadLine() End Sub End Module


Programming Challenge cd


VB Programming The SELECT CASE statement The SELECT CASE statement is similar to an IF statement, but will allow more than two routes through your program.

Statements Used SELECT CASE <variable> CASE <condition> <block of code> CASE <condition> <block of code> : : End Select

Example Program 1

Example Program 2

Module Module1 'A Program using SELECT CASE 'B Monk 'June 2015 Sub Main() Dim Score As Integer

Module Module1 'A Program using SELECT CASE 'B Monk 'June 2015 Sub Main() Dim Keypress As Char Console.WriteLine("Enter a character from the keyboard") Keypress = Console.ReadLine()

Console.WriteLine("Enter a score") Score = Console.ReadLine() Select Case Score Case Is > 90 Console.WriteLine("Grade A") Case 80 To 89 Console.WriteLine("Grade B") Case 70 To 79 Console.WriteLine("Grade C") Case Else Console.ForegroundColor = ConsoleColor.Red Console.WriteLine("You did not pass the exam")

Select Case Keypress Case "A" To "Z", "a" To "z" Console.WriteLine("You entered a letter") Case "0" To "9" Console.WriteLine("You entered a number") Case "(", ")", "[", "]", "{", "}" Console.WriteLine("You entered a bracket") Case Else Console.WriteLine"(I have no idea what you entered") End Select

End Select Console.ReadLine() End Sub

Console.ReadLine() End Sub End Module

End Module


Programming Challenge cd


VB Programming The FOR statement – an unconditional loop A FOR loop is an unconditional loop that repeats a block of code a set number of times. A variable keeps count of the number of times the loop has been executed.

Statements Used FOR <variable> = <start> to <end> : Statement : Next Example Program 1 Module Module1 'Example unconditional loops 'B Monk 'June 2015 Sub Main() Dim counter As Integer

Example Program 2 Module Module1 'Example unconditional loops 'B Monk 'June 2015 Sub Main() Dim index As Integer

For counter = 1 To 10 Console.WriteLine("Hello") Next Console.ReadLine() End Sub End Module

The STEP command (in example program 3) tells the FOR loop how much to increase the counter variable each time it goes around the loop.

For index = 6 To 12 Console.WriteLine("index is " & index) Next Console.ReadLine() End Sub End Module

Example Program 3 Module Module1 'Example unconditional loops 'B Monk 'June 2015 Sub Main() Dim value As Integer For value = 10 To 0 Step -1 Console.WriteLine("The loop counter is " & value) Next Console.ReadLine() End Sub End Module


Programming Challenge cd


VB Programming The DO Loop statement – a conditional loop A conditional loop continues to repeat a block of code until a condition is met. There are two type of conditional Statements Used

WHILE loop – Test before

REPEAT Loop – Test after

Example – Test After Module Module1 'Conditional loop test after 'B Monk 'June 2015 Sub Main() Dim myoption As Integer Do Console.WriteLine("Please enter a number between 1 and 10") myoption = Console.ReadLine Loop Until myoption >= 1 And myoption <= 10 Console.WriteLine("You have entered a valid option") Console.ReadLine() End Sub End Module

Example – Test Before Module Module1 'Conditional loop test before 'B Monk 'June 2015 Sub Main() Dim myoption As Integer Console.WriteLine("Please enter a number between 1 and 10") myoption = Console.ReadLine Do While myoption < 1 Or myoption > 10 Console.WriteLine("Invalid entry, please enter a number between 1 and 10") myoption = Console.ReadLine Loop Console.WriteLine("You have entered a valid option") Console.ReadLine() End Sub


Programming Challenge cd


VB Programming Arrays An array is a set of variables, which all have the same identifier. Each variable in the array is called an element and is distinguished from the other elements by an index. NOTE: each element in the array must be of the same data type. An advantage of using arrays is that it makes it possible for the same instructions (in a loop) to process many different variables just by varying the index. Array: Teachers

Arrays can be used to store lists and tables.

Statements Used DIM list(10) AS INTEGER

Example Module Module1 'A Program using Arrays 'B Monk 'June 2015 Sub Main() Dim grade(10) As Integer Dim index As Integer For index = 0 To 10 Console.WriteLine("Please enter grade " & index) grade(index) = Console.ReadLine Next Console.WriteLine(grade(4)) For index = 0 To 10 Console.WriteLine(grade(index)) Next Console.ReadLine() End Sub End Module

0

Monk

1

Gonachon

2

Snowden

3

Bool

4

Fast

5

Grant


Programming Challenge cd


VB Programming String Manipulation A string is an array of characters: 1st

2nd

3rd

4th

5th

6th

7th

8th

9th

10th

A

B

C

1

2

3

a

b

$

&

The first character is A, the 5th is 2 and the 9th is $ Statements Used LEN (String) LEFT(String, length) RIGHT(String, length) MID(String, position, length)

Example Module Module1 'A Program using String Manipulation 'B Monk 'June 2015 Sub Main() Dim mystring As String Dim stringlength As Integer Dim firstchar As Char Dim lastchar As Char Dim midchar As Char Console.WriteLine("Please enter a word") mystring = Console.ReadLine stringlength = Len(mystring) Console.WriteLine("There are " & stringlength & " characters in your string") firstchar = Left(mystring, 1) lastchar = Right(mystring, 1) Console.WriteLine("The first letter is " & firstchar) Console.WriteLine("The last letter is " & lastchar) midchar = Mid(mystring, 5, 1) Console.WriteLine("The 5th character is " & midchar) Console.ReadLine() End Sub End Module


Programming Challenge cd


VB Programming Text file - Input Storing data in variables is fine while the program is being executed, but the data will be lost when the program terminates. To keep data, it can be written to a file and read from the file at a later date. If the data is text, then it can be stored as a text file. VB offers a range of tools to handle text files. These functions are stored in the System.Environment namespace. You must create a textfile called ‘test’ in your My Documents before writing out the sample code.

Statements Used Import system.Environment GetFolderPath() FileOpen(channel, file, mode) LineInput(channel) FileClose(channel)

Example Imports System.Environment Module Module1 'A programm to using text files 'B Monk 'June 2015 Sub Main() Dim path As String Dim filename As String Dim fileline As String 'initialisation path = GetFolderPath(SpecialFolder.MyDocuments) filename = "\test.txt" FileOpen(1, path & filename, OpenMode.Input) Do fileline = LineInput(1) Console.WriteLine(fileline) Loop Until EOF(1) FileClose(1) Console.ReadLine() End Sub End Module


Programming Challenge cd


VB Programming Text file - Output Storing data in variables is fine while the program is being executed, but the data will be lost when the program terminates. To keep data, it can be written to a file and read from the file at a later date. If the data is text, then it can be stored as a text file. VB offers a range of tools to handle text files. These functions are stored in the System.Environment namespace.

Statements Used Import system.Environment GetFolderPath() FileOpen(channel, file, mode) PrintLine(channel) FileClose(channel)

Example Imports System.Environment Module Module1 'A programm to using text files 'B Monk 'June 2015 Sub Main() Dim path As String Dim filename As String Dim fooditem As String 'initialisation path = GetFolderPath(SpecialFolder.MyDocuments) filename = "\test2.txt" FileOpen(1, path & filename, OpenMode.Output) Console.WriteLine("Please enter a food item") fooditem = Console.ReadLine Do While fooditem <> "X" PrintLine(1, fooditem) Console.WriteLine("Please enter a food item") fooditem = Console.ReadLine Loop FileClose(1) Console.ReadLine() End Sub End Module


Programming Challenge cd


VB Programming Text file - Append In Computing, append means “add to�. If you open a file in append mode, text lines will be added to the end of the file.

Statements Used Import system.Environment GetFolderPath() FileOpen(channel, file, mode) PrintLine(channel) FileClose(channel)

Example Imports System.Environment Module Module1 'A programm to using text files - Append 'B Monk 'June 2015 Sub Main() Dim path As String Dim filename As String Dim fileline As String Dim fooditem As String 'initialisation path = GetFolderPath(SpecialFolder.MyDocuments) filename = "\test2.txt" FileOpen(1, path & filename, OpenMode.Append) Console.WriteLine("Please enter a food item") fooditem = Console.ReadLine Do While fooditem <> "X" PrintLine(1, fooditem) Console.WriteLine("Please enter a food item") fooditem = Console.ReadLine Loop FileClose(1) Console.ReadLine() End Sub

End Module


Programming Challenge cd


VB Programming Text file – Creating a CSV file A CSV (Comma Separated Variables) file has a number of values on one line, each separated by a comma.

Statements Used Import system.Environment GetFolderPath() FileOpen(channel, file, mode) LineInput(channel) FileClose(channel) Imports System.Environment Module Module1 'A programm to using text files 'B Monk 'June 2015 Sub Main() Dim path As String Dim filename As String Dim fooditem As String Dim foodqty As String 'initialisation path = GetFolderPath(SpecialFolder.MyDocuments) filename = "\shoppinglist.csv" FileOpen(1, path & filename, OpenMode.Output) Console.WriteLine("Please enter a food item") fooditem = Console.ReadLine Do While fooditem <> "X" Console.WriteLine("Please enter the quantity.") foodqty = Console.ReadLine PrintLine(1, fooditem & "," & foodqty) Console.WriteLine("Please enter a food item") fooditem = Console.ReadLine Loop FileClose(1) Console.ReadLine() End Sub End Module


Programming Challenge cd


VB Programming Functions A functions is similar to a procedure, expect that a function always returns a results and is never called directly. Values (parameters) are passed into the functions within the brackets.

Statements Used FUNCTION <identifier> (Byref <parameter> AS <type>*) AS <type> : : END FUNCTION

The Program Module Module1 'How to use Functions 'B Monk 'June 2015 Sub Main() Dim value As Integer Dim power As Integer Console.WriteLine("Please enter a number") value = Console.ReadLine Console.WriteLine(sqr(value)) Console.WriteLine("Please enter a power") power = Console.ReadLine Console.WriteLine(Powerof(value, power)) Console.ReadLine() End Sub Function sqr(ByVal number As Integer) As Integer 'Function to square the number 'Passing 1 paramenter sqr = number * number End Function Function Powerof(ByVal number As Integer, ByVal index As Integer) As Integer 'Function to find the power of the number aised to the index 'Passing 2 parameters Powerof = number ^ index End Function End Module


Programming Challenge cd


VB Programming


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.