Professional Documents
Culture Documents
Start a new Project and select Console Application. Type the following code:
module module1
sub main()
console.writeline("Hello World!")
console.readKey()
end sub
end module
Tasks
0.1 – Write a program that displays the message “Hello Dave”
0.2 – Write a program that displays the message “My name is Dave and I live in Brussels” (replace Dave
and Brussels with your own information)
0.3 – Write a program that displays the lyrics to your national anthem. Each line of the song should be
on a fresh line.
Variables
A variable is used to temporarily store a piece of data.
For example:
In the code above the variable is called number1 and the value it is storing is 10. Variables can hold
any type of data. Using variables makes it easier for people to understand what is going on. In Visual
Basic you need to define a variable before you can use it. To do this you type Dim before the variable
name the first time you use it. You should then say what type of data you think it is. In this
example number1 is an integer therefore the code to define a variable called number1 as an integer
is Dim number1 as Integer
For example:
Variables
Often, in VBA, we’d like to dedicate a place in memory to a variable – a dedicated value your code can
modify. These should always be declared at the beginning of a program. For example if we want to
declare the variable “x” as an integer, we type in the following line:
Dim x As Integer
3.1 Types of Variables
The following table summarizes the most common types of variables you’ll need to declare:
Constant variable
Constant is a value that cannot be reassigned. A constant is immutable and cannot be
changed. There are some methods in different programming languages to define a
constant variable. Most uses the const keyword. Using a const keyword indicates that
the data is read-only. We can use the const keyword in programming languages such
as C++, java script.
Program/Source Code:
The source code to check the given number is even or odd is given below. The given
program is compiled and executed successfully.
Module Module1
Sub Main()
Dim num As Integer = 0
End Module
Output:
Enter number: 122
Given number is EVEN
Press any key to continue . . .
Program/Source Code:
The source code to find the largest number between the two numbers is given
below. The given program is compiled and executed successfully.
Module Module1
Sub Main()
result = If(
(num1 > num2 AndAlso num1 > num3), num1,
If((num2 > num1 AndAlso num2 > num3), num2, num3)
)
Console.WriteLine("Largest Number is: {0}", result)
Console.ReadLine()
End Sub
End Module
Output:
Enter number1: 10
Enter number2: 30
Enter number3: 20
Largest Number is: 30
Program/Source Code:
The source code to calculate the perimeter of the circle is given below. The given
program is compiled and executed successfully.
Module Module1
Sub Main()
End Module
Output:
Explanation:
In the above code, we calculated the perimeter of the circle, here we used the value of PI
that is "3.14". After that, we printed the value of perimeter on the console screen.