I bring to you the 3^{rd} tutorial on Introduction to Programming using Microsoft Studio IDE tutorial 3, did you miss tutorial 1 and tutorial 2 don't worry below are the links to study it from basic introduction.
Note
that at the basic introduction in Tutorial1
I
solve the exercise at the end of the tutorial and its just for you to
be sure of what you are doing. Lets roll.
Operators Using Microsoft Visual Basic |
Objectives Of This Tutorial 3 are:
- Understand what operators are in visual basic
- Gain experience with three types of operators in visual basic
- Know the rules for operator precedence and associativity
Introduction To Operators In Microsof Visual Basic.
An
operator is a symbol used to carry out an action. A variable or a
value involved in an operation is called an operand. An operator may
have one or two operands. Those operators that work with only one
operand are called unary operators. And those operators that
work with two operands are called binary operators.
There are three types of operators:
- Arithmetic operators
- Relational operators
- Logical operators
Arithmetic Operators
Arithmetic
operators are operators used in calculations. The following is a
table of arithmetic operators.
Operator | Symbol |
Addition | + |
Subtraction | - |
Multiplication | * |
Division (floating point) | / |
Division (integer) | \ |
Modulus | Mod |
Exponentiation | ^ |
Unary minus | - |
Unary plus | + |
Task
Write
a program (ArithmeticOpTest) that receives two numbers from
the user and then output results for each arithmetic operator applied
on the two numbers.
Relational (Comparison) Operators
Relational
operators are used to compare values. The result of the comparison is
always Boolean (true or false). Relational operators are summarized
in the following table:
Operator | Symbol |
Equal to | = |
Not equal to | <> |
Less than | < |
Less than or equal to | <= |
Greater than | > |
Greater than or equal to | >= |
Resource link:
Task
Write
a program (RelationalOpTest) that receives two numbers from
the user and then output results for each relational operator applied
on the two numbers.
Logical Operators
Unlike
relational expressions which are used to compare values, logical
operators are used to compare Boolean expressions. Logical operators
work base on Truth tables. The result of the comparison is always
Boolean. Logical operators are summarized in the following table:
Logical Operators |
And |
Or |
AndAlso |
OrElse |
Xor |
Not |
Questions:
- What is the difference between And and AndAlso logical operators?
- What is the difference between Or and OrElse logical operators?
Given
that:
Dim
gender as Char = “F”
Dim
grade as Double = 57.5
Dim
age as Byte = 70
What
is the result of the following expressions?
gender=”F” And age>=65 | |
grade >39 And grade <70 | |
Not (gender=”M”) | |
gender = “M” Xor gender = “F” |
Operator Precedence and Associativity
Solve
this equation:
When
several operations occur in an expression, each part is evaluated and
resolved in a predetermined order called operator precedence.
When
operators of equal precedence appear together in an expression, the
operators are evaluated from left to right. This is called
associativity.
Summary
of Operator Precedence
Rank | Operator |
1 | ( ) |
2 | ^ |
3 | + - (unary) |
4 | * / |
5 | \ |
6 | Mod |
7 | + - (binary) |
8 | & |
9 | < <= > >= = <> |
10 | Not |
11 | And AndAlso |
12 | Or OrElse |
13 | Xor |
14 | = += -= *= /= \= ^= &= |
Task:
Rewrite
each of the following mathematical expression in their visual basic
form:
Mathematical expression | Visual Basic equivalent |
Exercise:
- Write a program (threenums) that inputs three integers from the user and displays the sum, average and product of the numbers.
- Write a program (circle) that inputs from the user, the radius of a circle and prints the diameter, circumference and area. Use 3.14159 as the value for π.
Remember:
- The area of a triangle is given by . Write a program (triangle) that prompts a user for the values of the base and height of a triangle. The program should use these values to calculate and output the area of the triangle to the console.
Nice Blog Post !
ReplyDelete