Jump to content

Guide to Game Development/The Programming Language/VB.NET/Basic math operators and Concatenation

100% developed
From Wikibooks, open books for an open world
Guide to Game Development/The Programming Language/VB.NET
Variables Basic math operators and Concatenation If Statement

The four basic math operations

[edit | edit source]

The four basic operations can be used between: two constants, a variable and a constant and two variables.

+ - * /

Examples:

Dim Num1, Num2, Num3 as Integer 
Num2 = 10
Num3 = 4

Num1 = Num2 + Num3
Console.WriteLine(Num1)
Num1 = 10-5
Console.WriteLine(Num1)
Num1 = Num2 * 3
Console.WriteLine(Num1)
Num1 = 12 / Num2
Console.WriteLine(Num1)

This code could be condenced to not need a Num1 like so:

Dim Num2 as Integer = 10
Dim Num3 as Integer = 4

Console.WriteLine(Num2 + Num3)
Console.WriteLine(10-5)
Console.WriteLine(Num2 * 3)
Console.WriteLine(12 / Num2)

Output:

14
5
30
3

Powers

[edit | edit source]

Powers can be done by the symbol ^ Example use:

Console.WriteLine(2^10)

Output:

1024

Other mathematical operators

[edit | edit source]

For additional math operators, click here.

String Concatenation

[edit | edit source]

This is the joining of two strings to make one. To do this you can either use + or &. Example of joining two strings:

Dim Str1 as String = "Hello " & "World" + "!"
Console.WriteLine(Str1)
Console.WriteLine("Hello " & "World" + "!")

Output:

Hello World!
Hello World!

You can also do this with numbers and strings:

Dim Num1 as Integer = 6
Dim Str1 as String = "Your number: " & Num1 & " + 4 = " & Num1 + 4
Console.WriteLine(Str1)

Output:

Your number 6 + 4 = 10


References

[edit | edit source]