0% found this document useful (0 votes)
12 views28 pages

OOP LEC 2 Variables Datatypes Operators and If Statement

The document outlines the learning objectives and outcomes for a course on variables, data types, operators, and conditional statements in VB.Net. It covers various data types, variable declaration and initialization, type conversion functions, and different types of operators including arithmetic and comparison operators. Additionally, it includes examples and assessments to reinforce the concepts taught in the course.

Uploaded by

Kawaii Kath
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views28 pages

OOP LEC 2 Variables Datatypes Operators and If Statement

The document outlines the learning objectives and outcomes for a course on variables, data types, operators, and conditional statements in VB.Net. It covers various data types, variable declaration and initialization, type conversion functions, and different types of operators including arithmetic and comparison operators. Additionally, it includes examples and assessments to reinforce the concepts taught in the course.

Uploaded by

Kawaii Kath
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Week 2

Variables, Datatypes,
Operators and
Conditional Statement
Abrahem P. Anqui
Instructor
Learning Objective

 Compare and contrast procedural/functional


approach to object-oriented programming
approach.
 Design, implement, test and debug programs
using OOP concepts like abstraction,
encapsulation, inheritance and polymorphism.
 Demonstrate intermediate knowledge of top-
down, structured problem-solving
methodologies.
 Implement intermediate knowledge of high-
level object-oriented programming languages.
Learning Outcome

 Apply and use constants and variables


 Use integer data types
 Use the Boolean data type
 Use floating-point data types
 Use the char data type
 Apply arithmetic operators
 Explain the proper usage of data and its variables.
 Define data types and how it is being used.
Data Types

 Data types refer to an extensive system used for


declaring variables or functions of different types.
The type of a variable determines how much space
it occupies in storage and how the bit pattern
stored is interpreted.
 VB.Net provides a wide range of data types.
• Boolean • Decimal
• Byte • Long
• Char
• Date
• String
• Integer
• Double
Sample Program
If ScriptEngine = "VB" Then
Module DataTypes
bl = True
Sub Main() Else
Dim b As Byte bl = False
Dim n As Integer End If
Dim si As Single
Dim d As Double If bl Then
Dim da As Date Console.Write(c & " and," & s & vbCrLf)
Dim c As Char Console.WriteLine("declaring on the day
of: {0}", da)
Dim s As String
Console.WriteLine("We will learn VB.Net
Dim bl As Boolean seriously")
b = 1 Console.WriteLine("Lets see what
n = 1234567 happens to the floating point
si = 0.12345678901234566 variables:")
d = 0.12345678901234566 Console.WriteLine("The Single: {0}, The
da = Today Double: {1}", si, d)
c = "U“ End If
Console.ReadKey()
s = "Me"
End Sub
End Module
The Type Conversion Functions in VB.Net

VB.Net provides the following in-line type


conversion functions:
Functions Description
CBool(expression) Converts the expression to Boolean data
type.
CByte(expression) Converts the expression to Byte data
type.
CChar(expression) Converts the expression to Char data
type.
CDate(expression) Converts the expression to Date data
type
CDbl(expression) Converts the expression to Double data
type.
CDec(expression) Converts the expression to Decimal data
type.
CInt(expression) Converts the expression to Integer data
Variables in VB.net

A variable is nothing but a name given to a


storage area that our programs can
manipulate. Each variable in VB.Net has a
specific type, which determines the size and
layout of the variable's memory; the range of
values that can be stored within that memory;
and the set of operations that can be applied
to the variable.
Variable Declaration in VB.Net

The Dim statement is used for variable


declaration and storage allocation for one or
more variables. The Dim statement is used at
module, class, structure, procedure or block
level.
Syntax for variable declaration in VB.Net is −
[ < attributelist > ]
[ accessmodifier ] [[ Shared ] [ Shadows ] | [ Static ]] [ ReadOnly ]
Dim [ WithEvents ] variablelist
 Where,
 attributelist is a list of attributes that apply to the variable. Optional.
 accessmodifier defines the access levels of the variables, it has values
as - Public, Protected, Friend, Protected Friend and Private. Optional.
 Shared declares a shared variable, which is not associated with any
specific instance of a class or structure, rather available to all the
instances of the class or structure. Optional.
 Shadows indicate that the variable re-declares and hides an identically
named element, or set of overloaded elements, in a base class. Optional.
 Static indicates that the variable will retain its value, even when the
after termination of the procedure in which it is declared. Optional.
 ReadOnly means the variable can be read, but not written. Optional.
 WithEvents specifies that the variable is used to respond to events
raised by the instance assigned to the variable. Optional.
 Variablelist provides the list of variables declared.
 Each variable in the variable list has the following syntax
and parts −
variablename[ ( [ boundslist ] ) ] [ As
[ New ] datatype ] [ = initializer ]

 Where,
 variablename − is the name of the variable
 boundslist − optional. It provides list of bounds of each dimension of
an array variable.
 New − optional. It creates a new instance of the class when the Dim
statement runs.
 datatype − Required if Option Strict is On. It specifies the data type of
the variable.
 initializer − Optional if New is not specified. Expression that is
evaluated and assigned to the variable when it is created.
Some valid variable declarations along with
their definition are shown here −
Dim StudentID As Integer
Dim StudentName As String
Dim Salary As Double
Dim count1, count2 As Integer
Dim status As Boolean
Dim exitButton As New System.Windows.Forms.Button
Dim lastTime, nextTime As Date
Variable Initialization in VB.Net
Variables are initialized (assigned a value) with
an equal sign followed by a constant
expression. The general form of initialization is
−variable_name = value
Dim pi As Double
for example,pi = 3.14159

You can initialize a variable at the time of


declarationDim
asStudentID
follows −As Integer = 100
Dim StudentName As String = "Bill Smith"
Example

Module variablesNdataypes
Sub Main()
Dim a As Short
Dim b As Integer
Dim c As Double
a = 10, b = 20, c = a + b
Console.WriteLine("a = {0}, b = {1}, c = {2}", a, b, c)
Console.ReadLine()
End Sub
End Module
Accepting Values from User

The Console class in the System namespace


provides a function ReadLine for accepting input
from the user and store it into a variable. For
example, Dim message As String
message = Console.ReadLine

 The following example demonstrates it −


Module variablesNdataypes
Sub Main()
Dim message As String
Console.Write("Enter message: ")
message = Console.ReadLine
Console.WriteLine()
Console.WriteLine("Your Message: {0}", message)
Console.ReadLine()
End Sub
End Module
VB.Net - Operators

 An operator is a symbol that tells the


compiler to perform specific mathematical or
logical manipulations. VB.Net is rich in built-
in operators and provides following types of
commonly used operators −
 Arithmetic Operators
 Comparison Operators
 Logical/Bitwise Operators
 Bit Shift Operators
 Assignment Operators
Arithmetic Operators

Following table shows all the arithmetic


operators supported by VB.Net. Assume
variable A holds 2 and variable B holds 7,
then −
Operator Description Example

^ Raises one operand to the power of another B^A will give 49

+ Adds two operands A + B will give 9

- Subtracts second operand from the first A - B will give -5

* Multiplies both operands A * B will give 14

/ Divides one operand by another and returns a floating point B / A will give 3.5
result

\ Divides one operand by another and returns an integer result B \ A will give 3

MOD Modulus Operator and remainder of after an integer division B MOD A will give 1
Comparison Operators

Following table shows all the comparison


operators supported by VB.Net. Assume
variable A holds 10 and variable B holds 20,
then −
Operator Description Example

= Checks if the values of two operands are equal or not; if yes, (A = B) is not true.
then condition becomes true.
<> Checks if the values of two operands are equal or not; if (A <> B) is true.
values are not equal, then condition becomes true.
> Checks if the value of left operand is greater than the value of (A > B) is not true.
right operand; if yes, then condition becomes true.
< Checks if the value of left operand is less than the value of (A < B) is true.
right operand; if yes, then condition becomes true.
>= Checks if the value of left operand is greater than or equal to (A >= B) is not true.
the value of right operand; if yes, then condition becomes
true.
<= Checks if the value of left operand is less than or equal to the (A <= B) is true.
value of right operand; if yes, then condition becomes true.
Apart from the above, VB.Net provides three more
comparison operators, which we will be using in
forthcoming chapters; however, we give a brief
description here.
 Is Operator − It compares two object reference variables and
determines if two object references refer to the same object
without performing value comparisons. If object1 and object2
both refer to the exact same object instance, result is True;
otherwise, result is False.
 IsNot Operator − It also compares two object reference
variables and determines if two object references refer to
different objects. If object1 and object2 both refer to the exact
same object instance, result is False; otherwise, result is True.
 Like Operator − It compares a string against a pattern.
Logical/Bitwise Operators

 Following table shows all the logical operators supported by


VB.Net. Assume variable A holds Boolean value True and
variable B holds Boolean value False, then −
Operato Description Example
r
And It is the logical as well as bitwise AND operator. If (A And B) is
both the operands are true, then condition False.
becomes true. This operator does not perform
short-circuiting, i.e., it evaluates both the
expressions.
Or It is the logical as well as bitwise OR operator. If (A Or B) is
any of the two operands is true, then condition True.
becomes true. This operator does not perform
short-circuiting, i.e., it evaluates both the
expressions.
Not It is the logical as well as bitwise NOT operator. Not(A And B)
Use to reverses the logical state of its operand. If is True.
a condition is true, then Logical NOT operator will
Operator Description Example
Xor It is the logical as well as bitwise Logical Exclusive OR A Xor B is
operator. It returns True if both expressions are True or True.
both expressions are False; otherwise it returns False.
This operator does not perform short-circuiting, it
always evaluates both expressions and there is no
short-circuiting counterpart of this operator.
AndAlso It is the logical AND operator. It works only on Boolean (A AndAlso B)
data. It performs short-circuiting. is False.

OrElse It is the logical OR operator. It works only on Boolean (A OrElse B) is


data. It performs short-circuiting. True.

IsFalse It determines whether an expression is False.

IsTrue It determines whether an expression is True.


Assignment Operators

 There are following assignment operators supported by


VB.Net −
Operator Description Example
= Simple assignment operator, Assigns values C = A + B will
from right side operands to left side operand assign value of A
+ B into C
+= Add AND assignment operator, It adds right C += A is
operand to the left operand and assigns the equivalent to C =
result to left operand C+A
-= Subtract AND assignment operator, It C -= A is
subtracts right operand from the left operand equivalent to C =
and assigns the result to left operand C-A
*= Multiply AND assignment operator, It C *= A is
multiplies right operand with the left operand equivalent to C =
and assigns the result to left operand C*A
/= Divide AND assignment operator, It divides C /= A is
left operand with the right operand and equivalent to C =
assigns the result to left operand (floating C/A
\= point division)
Divide AND assignment operator, It divides left C \= A is
operand with the right operand and assigns the equivalent to C
result to left operand (Integer division) = C \A

^= Exponentiation and assignment operator. It C^=A is


raises the left operand to the power of the right equivalent to C
operand and assigns the result to left operand. = C ^ A

&= Concatenates a String expression to a String Str1 &= Str2 is


variable or property and assigns the result to same as
the variable or property. Str1 = Str1 &
Str2
Operators Precedence in VB.Net

Operator precedence determines the


grouping of terms in an expression. This
affects how an expression is evaluated.
Certain operators have higher precedence
than others; for example, the multiplication
operator has higher precedence than the
addition operator −
For example, x = 7 + 3 * 2; here, x is
assigned 13, not 20 because operator * has
higher precedence than +, so it first gets
multiplied with 3*2 and then adds into 7.
Here, operators with the highest precedence
appear at the top of the table, those with the
lowest appear at the bottom. Within an
expression, higher precedence operators will
be evaluated first.
Assessment: Tracing Output

Dim a, b, c, d, e, f, g As g=b
Double
b=a
a = 8.0
a=g
b = 3.0
c = 4.0 Console.WriteLine(a)
d = 2.0 Console.WriteLine(b)
e = 1.0 Console.WriteLine(c)
f=a-b+c/d*e Console.WriteLine(d)
Console.WriteLine(f)
Console.WriteLine(e)
f = (a - b) + ((c / d) * e)
Console.WriteLine(f)
Console.WriteLine(f)
g = (a - (b + c)) / (d * e) Console.WriteLine(f)
Console.WriteLine(g)
Assessment

Create a VB.net program that will


compute the following result:
 Sum

 Difference

 Product

 Quotient

 Remainder

You might also like