0% found this document useful (0 votes)
9 views

Datatypesin

Advanced java

Uploaded by

Kajal Kalsule
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Datatypesin

Advanced java

Uploaded by

Kajal Kalsule
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 38

VB.

NET Data Type


• A Data Type refers to which type of data or
value is assigning to a variable or function so
that a variable can hold a defined data type
value
• Syntax:
• Dim Variable_Name as DataType
Boolean A Boolean type depends on the implementing
platform
Byte 1 byte
Char 2 bytes
Date 8 bytes
Decimal 16 bytes

Double 8 bytes
Integer 4 bytes
Long 8 bytes
Object Object size based on the platform such as 4
bytes in 32-bit and 8 bytes in 64-bit platform

SByte 1 byte
Short 2 bytes
Single 4 bytes
String String Datatype depend on the implementing
platform
Type Conversion Functions in VB.NET

• CBool(expression): It is used to convert an expression into a Boolean data type.


• CByte(expression): It is used to convert an expression to a Byte data type.
• CChar(expression): It is used to convert an expression to a Char data type.
• CDate(expression): It is used to convert an expression to a Date data type.
• CDbl(expression): It is used to convert an expression into a Double data type.
• CDec(expression): It is used to convert an expression into a Decimal data type.
• CInt(expression): It is used to convert an expression to an Integer data type.
• CLng(expression): It is used to convert an expression to a Long data type.
• CObj(expression): It is used to convert an expression to an Object data type.
• CSByte(expression): It is used to convert an expression to an SByte data type.
• CShort(expression): It is used to convert an expression to a Short data type.
VB.NET Variables Declaration
• The declaration of a variable is simple that requires a variable name and data type
followed by a Dim.
• A Dim is used in Class, Module, structure, Sub, procedure.
Syntax:
Dim [Variable_Name] As [Defined Data Type]

Dim:- It is used to declare and allocate the space for one or more variables in
memory.
• Variable_Name:- It defines the name of the variable to store the values.
• As :- It is a keyword that allows you to define the data type in the declaration
statement.
• Data TypeIt defines a data type that allows variables to store data types such as
Char, String, Integer, Decimal, Long, etc.
• ValueAssign a value to the variable.

For Example:-
Dim Roll_no As Integer
Dim Emp_name As String
Dim Salary As Double
initialize a variable at the time of
declaration
• Dim Roll_no As Integer = 101
• Dim Emp_name As String = " A S Patil "
Getting Values from the User in VB.NET

• Console class provides the Readline() function


in the System namespace. It is used to take
input from the user and assign a value to a
variable.
For example:
• Dim name As String
• name = Console.ReadLine()
• Or name = Console.ReadLine
Declaration of Constants

• const is a keyword that is used to declare a


variable as constant. The Const statement can
be used with module, structure, procedure,
form, and class.
• Syntax:
• Const constname As datatype = value
For Example:
Const num As Integer = 10
Scope of Variable in VB.NET

• Procedure Scope
• Module Scope
• Public Scope
Procedure (local) scope

• A local variable is a type of variable defined


within a procedure scope, block, or function.
• It is available with a code inside the
procedure, and it can be declared using
the Dim or static statement.
• Not accessible from outside of the local
method.
Module Scope

• All existing procedures can easily identify a variable that is


declared inside a module sheet is called a module-level
variable.
• Is is visible to all procedures within that module only, but it is
not available for other module's procedures.
• The Dim or private statement at the top of the first procedure
declaration can be declared the module-level variables.
• These variables cannot be declared inside any procedure block
Private num As Integer // A private module-level variable
Dim name As String // Another private module-level variable
Global (Public) Scope

• A global variable is a variable that is used to access


the variables globally in a program.
• It can be accessed by all the procedures or
modules available in a program.
• To access the variables globally in a program,
use the friend or public keyword with a variable in
a module or class at the top of the first procedure
function.
• Global scope is also known as the Namespace
scope
VB.NET Operators
• Arithmetic Operators
Operators Example
^ Y ^ X (X to the power Y)

+ X+Y

- X-Y

* X*Y
/ X/Y
\ X\Y
Mod X Mod Y
Comparison Operators
Operator Example
= (A = B)
<> (A <> B), check Non-Equality
> (A > B); if yes, TRUE,Else FALSE

< (A < B); if the condition is true, returns TRUE else FALSE

>= A >= B

<= A <= B

Is result = obj1 Is obj2

IsNot Result = obj1 IsNot obj2


Like The Like Operator is used to check the pattern expression of string
variable; And if the pattern matched, the result is True; otherwise,
it returns False.result = string Like the pattern, the pattern
represents the series of characters used by Like Operator.
Logical and Bitwise Operators
Operator Description Example
And The And Operator represents, whether both the operands are true; (A And B), result = False
the result is True.
Or It is an Or Operator that returns a true value; if anyone operand is (A Or B), result = True
true from both the operands.
Not The Not Operator is used to reverse the logical condition. For Not AOr
example, if the operand's logic is True, it reveres the condition and Not(A And B) is True
makes it False.

Xor It is an Exclusive OR Operator that represents, whether both the A Xor B is True
expression is true or false, the result is True; otherwise, the result is
False.

AndAlso It is a logical AND Operator that performs short-circuit operation A AndAlso B = False
on the variables, and if both the operands are true, the result is True
else the result is False.

OrElse It is a logical OR Operator that perform short-circuit operation on A OrElse B = True


Boolean data. If anyone of the operand is true, the result is True
else the result is False.

IsFalse The IsFalse Operator is used to determine whether an expression is


False.
IsTrue The IsTrue Operator is used to determine whether an expression is
True.
Assignment Operators
Operator Example
= X = 5, X assign a value 5
X = P + Q, (P + Q) variables or value assign to X.
+= X += 5, which means
X= X+5 ( 5 will add and assign to X and then result saved to
Left X operand)
-= X -= P, which is same as X = X - P

*= X *= P, which is same as X = X - P

/= X /= P, which is same as X = X - P

\= X \= P, which is same as X = X - P

^= X ^= P, which is same as X = X ^ P

&= Str &= name, which is same a


Concatenation Operators

Operator Example

& Result = Wel & come,


Result = Welcome

+ Result = Wel + come,


Result = Welcome
VB.NET Control Statements

• If-Then Statement
• If-Then Else Statement
• If-Then ElseIf Statement
• Select Case Statement
• Nested Select Case Statements
If-Then Statement

• Syntax:
• If condition Then
• [Statement or block of Statement]
• End If
• Module if_statement2
• Sub Main()
• Dim no1, no2 As Integer
• Console.WriteLine("Enter any two number:")
• no1 = Console.ReadLine()
• no2 = Console.ReadLine()
• If no1 > no2 Then
• Console.WriteLine("First number is greater than second nu
mber") End If
• If no1 < no2 Then
• Console.WriteLine("Second number is greater than First
number")
• End If
• Console.WriteLine("press any key to exit...")
• Console.ReadKey()
• End Sub End Module
If-Then-Else Statement

• Syntax:
• If (Boolean_expression) Then
• 'This statement will execute if the Boolean con
dition is true
• Else
• 'Optional statement will execute if the Boolea
n condition is false
• End If
If-Then-ElseIf statement
• Syntax
• If(condition 1)Then
• ' Executes when condition 1 is true
• ElseIf( condition 2)Then
• ' Executes when condition 2 is true
• ElseIf( boolean_expression 3)Then
• ' Executes when the condition 3 is true
• Else
• ' executes the default statement when none of the ab
ove conditions is true.
• End If
Select Case Statement
• Select Case [variable or expression]
• Case value1 'defines the item or value that you want to match
.
• // Define a statement to execute

• Case value2 'defines the item or value that you want to match
.
• // Define a statement to execute

• Case Else
• // Define the default statement if none of the conditions is tru
e.
• End Select
Programs
• Write a program to use the If-Then-ElseIf Statement for calculating the
division obtained by the student. Also, take the marks obtained by the
student in 5 different subjects from the keyboard.
• Write a program to print the larger and smaller of the two numbers.
• Write a program to check whether the number is even or odd.
• Write a program to display the Days name using the select case statement
in VB.NET.
• Write a program to perform an arithmetic operation using the Select case
statement in VB.NET.
• Write a simple program to print a table in the VB.NET.
• Write a simple program to print subject names using For Each loop in
VB.NET.
• Write a program to print the sum of digits of any number using while End
loop in VB.NET.
Loops in Vb.Net
1. For Next Loop
• Imports System
• Module Number
• Sub Main()
• Console.Write(" The number starts from 1 to 10
" & vbCrLf)
• For i As Integer = 1 To 10 Step 1
• Console.WriteLine(" Number is {0} ", i)
• Next
• Console.WriteLine(" Press any key to exit... ")
• Console.ReadKey()
• End Sub
• End Module
For Each Loop

• Syntax:
• For Each var_name As [ DataType ] In Collectio
n_Object
• [ Statements to be executed]
• Next
Example
• Imports System
• Module For_Each_loop
• Sub Main()
• Dim An_array() As Integer = {1, 2, 3, 4, 5}
• Dim i As Integer
• For Each i In An_array
• Console.WriteLine(" Value of i is {0}", i)
• Next
• Console.WriteLine("Press any key to exit...")
• Console.ReadLine()
• End Sub
• End Module
While End Loop

• Syntax:
• While [condition]
• [ Statement to be executed ]
• End While
• Imports System
• Module while_ex
• Sub Main()
• Dim x As Integer
• x=1
• While x <= 10
• Console.WriteLine(" Number {0}", x)
• x=x+1
• End While
• Console.WriteLine(" Press any key to exit...")
• Console.ReadKey()
• End Sub
• End Module
Do Loop
• Do
• [ Statements to be executed]
• Loop While Boolean_expression
• // or
• Do
• [Statement to be executed]
• Loop Until Boolean_expression

Do While loop executes the body until the given condition


becomes false.
Do Until -The Do Statement executes as long as Until
condition becomes true.
• Imports System
• Module Do_loop
• Sub Main()
• Dim i As Integer = 1
• Do
• Console.WriteLine(" Value of variable I is : {0}", i)
• i=i+1
• Loop While i <= 10
• Console.WriteLine(" Press any key to exit...")
• Console.ReadKey()
• End Sub
• End Module

You might also like