- Lesson:vb1 Basics
- Please login to keep your results
- Welcome,
Intro
Visual Basic or VB is one of Microsoft’s most important languages. When it is used to manipulate Microsoft Office its called VBA (visual basic for applications)
- Objects are often evident.
- VB.net is very popular.
- Case insensitive, Space & line break insensitive (but the IDE will correct for you)
Getting Started
- vb online click here
- On the JIS network
- At home, Visual Studio is a very large download… (more info to come)
- Other Online editors, IdeaOne
Hello World
Your code must go immediately after the “Sub Main()” line
Console.WriteLine("Hello World!")
Console.ReadLine() 'This is to keep the screen open so you can see the result
Then Run your code
Great! you have written your first vb command
Question: What does the ‘ character do in vb? – Hint check the code above
User Input
Dim name As String
Console.Write("What is your name? ")
name = Console.ReadLine()
Console.WriteLine("Hello, " & name)
Console.ReadLine()
Theory
Name is a variable, it could be hold the words “Peter”, “Joanne” or “Paul”. A variable is stored in the memory of a computer
Questions
What’s the difference between name and “name”
Which keyword is needed for the user to tell the computer something?
Which character is used to stick two strings together in VB?
Research why does VB use DIM?
Conditional Love
or Insult If
If name = "me" Then
Console.WriteLine(name & ",you are awesome")
End If
Console.ReadLine()
Theory
Hey you have
- used an if statement (a conditional)
- used the comparison operator = (you can also use number with <, > )
Which of the major programming principles uses IF ?
Else
If name = "me" Then
Console.WriteLine(name & ",you are awesome")
Else
Console.WriteLine(name & ", you suck")
End If
Key Words
- you have joined a message string
"you suck"
with a stringname
this is calledto concatenate
- the concatenation operator in vb is &
Elseif
Allows you to make more complex choices
If name = "me" Then
Console.WriteLine(name & ", you are awesome")
ElseIf name = "pete" Then
Console.WriteLine(name & ", you are a real good buddy")
ElseIf name = "sarah" Then
Console.WriteLine(name & ", you are a real nice person")
Else
Console.WriteLine(name & ", you suck")
End If
See Also: [https://www.dotnetperls.com/select-vbnet](Select Case)
AND Teenager
Task
Your task is to tell people if they are teenagers. Use the following code to help you.
Dim age As Integer
Console.Write("How old are you? ")
age = Console.ReadLine()
If age > 40 And age < 80 Then
Console.WriteLine("You are middle aged")
End If
Theory: Boolean Logic
You will need to use the Boolean operator AND (always in Sentence case in vb). Other operators are OR, AND NOT
- Note that vb needs you to input a number not string
OR Friends
Using Or
is similar to using Else If
Task
Send a different insult to you friends
If Name = "John" Or Name = "Paul" Or Name = "Ringo" Then
Console.Writeline("hey you could be a Beetle ")
End If
Theory
see also Select Case
You have used
- the Boolean operator OR
AI Chat Bot
Read about this AI (Artificial Intelligence) Receptionist.
Build your own AI Chatbot. You can search the web to find online examples to give you ideas.
The Turing Test
If a human being cannot tell the difference between a computer and a real person it has passed the Turing Test.
More information
Submit your vb Code here:
Loop the loop
Computers are very good at repeating things
console.writeline("hey I can count")
For x = 1 to 10
console.writeline(x)
Next
Tasks
- Count up to Ten Thousand
- Write the 2 times table up to 24
- Reverse the 2 times table (24 to 2)
- Do any times table (ask the user)
- Write x squared up to 12 * 12
- Write x cubed up to 12 * 12 * 12
- Write the powers of 2 up to 2 ^ 10
- Write 1/x (1 over x) up to x = 10
Extreme Tasks
1. Write fibonacci Series upto 100
2. Write the Series of prime numbers upto 100
3. Write the Square root of x without using the maths library (difficult)
'count in colour
Console.ForegroundColor = ConsoleColor.Green
'slow counting
System.Threading.Thread.Sleep(5)
While
While loops keep going until a condition is met. They can go on forever (but the one below doesn’t)
Dim i As Integer = 0
While i < 100
Console.WriteLine(i)
i = i + 1
End While
- create i and give it a value
- while starts the loop
- print i
- make i bigger (increment x)
Bottom Check
In this version of the While loop, the conditional statement is at the bottom
Dim i As Integer = 0
Do
Console.WriteLine(i)
i = i + 1
Loop While i < 100
Compare the Top & Bottom Check loops. Is possible that the statements in the loop never get executed? How many times must they be executed.
Random Numbers
Random numbers change each time you write them because of the .next. So create a second variable if you want to use it again
Dim r1 As Random = New Random
Dim r2 as integer
r2 = r1.Next(1, 13)
Subs
Subs make code easier to read and understand. They also help us reuse code.
Sub mySquare():
'Code to draw a square goes here
End Sub
'Call the Sub later by using
mySquare()
Some Subs also need an argument to be added. This ‘blue’ sub needs a string to be passed so it knows what to print.
Sub blue(mystring)
Console.ForegroundColor = ConsoleColor.Blue
Console.WriteLine(mystring)
Console.ResetColor()
End Sub
'In your Sub Main()
blue("You fell in the Water")
Functions look like subs but they actually return a value (they have a return keyword in them at the end)
Submit Warning Sub
Question:
Without scrolling up to read the definitions, write down what a sub is an what are the advantages of using them.