Math Operators and Getting User Input in Visual Basic

Math Operators and Getting User Input in Visual Basic

Math operators are very much popular in any programming language. They are such as addition, subtraction, multiplication, division, power, mod and etc. They are used to do the basic math operations on the operands declared in the code or entered by the user.

In this article first we will be getting familiar with Getting User Input in the code. Then we will work on the math operators.

How to Get ‘User Input’ from the Console?

  1. Declare a variable (integer, character, string or etc.)
    For the initial value of your variable, it is always good to assign it to Nothing. The keyword Nothing in Visual Basic is equal to the keyword Null in other programming Languages.
  2. Make a message prompt on the Console for the user to enter something.
  3. Assign your variable to Console.ReadLine()
    This line will assign whatever is entered by the user to the variable the user has declared.

Below you can find 2 different examples; the first one asking the name of the user and the second one asking the age of the user. You can type in your name and age whatever you desire to.

Getting the user to enter his/ her name to the Console

Output:

UserName

Getting the user to enter his/ her age to the Console

Output:

UserAge

Math Operators in Visual Basic

In the previous article Variables in Visual Basic 2010 we discussed about 4 basic math operators (addition +, subtraction -, multiplication * and division /). In this article first we will be introducing 2 new math operators which are common and useful. Then we will be working with some shorthand coding.

Math Operator (Power) (getting use of User Input Data)

For using the power math operator in your code you need to use the (^) sign which is Ctrl + 6.

Output:

Math Operators Power

Math Operator (Modulo) (getting use of User Input Data)

Modulo operator is used to find out the remainder when two numbers are divided by each other. For using modulo operator in your code, you need to use the keyword Mod in the code.

Output:

Math Operators Modulo

 

Some Key Points in Math Operators:

Using Short Hand Coding:

The Following Code;
num1 = num1 + num2
AND;
num1 += num2
are the same. Furthermore; they both add num1 and num2. Check the Code below;

Output:

Short Hand Code

Note: The Short Hand Code defined above (num1 += num2) can be carried with any other math operator such as -, *, / and etc. You can check them by trying out your own codes.

Feel free to post your comments and ask your questions on www.codejow.com

Previous Variables in Visual Basic 2010
Next PHP Tutorials 7: String and Logical Operators

About author

Bahonar Javid
Bahonar Javid 43 posts

<p>* Computer Application Student<br /> * Co-Founder of www.codejow.com<br /> * Love to code and Learn new languages<br /> * Enjoy playing football</p>

View all posts by this author →

You might also like