Upload
vineet-behal
View
213
Download
0
Embed Size (px)
Citation preview
7/27/2019 11115_Programming Constructs in VB
1/25
Programming Syntax in VB.NET
7/27/2019 11115_Programming Constructs in VB
2/25
Visual Basic Keywords
Every Programming language uses a set of predefined words. These words
are language-specific and are known as keywords.
Keyword has a specific predefined meaning for the compiler. It is one of
the essential part of a language definition.
Visual Basic provides two types of Keywords such as :-1) Reserved Keywords:- These keywords are those that cannot be used as
names for programming elements, such as variables, methods and classes.
2) Unreserved Keywords:-These keywords can be used as programming
elements, such as variables and procedures.
Examples for Reserved Keywords:-If, Then, Integer, String, Namespace,For, ElseIf, Sub, Long, Double etc
Examples for Unreserved Keywords:- Binary, Preserve, From, IsTrue etc
7/27/2019 11115_Programming Constructs in VB
3/25
Visual Basic Data Types
Data Type Storage Size Value Range
Boolean Depends upon the platform True or False
Byte 1 Byte 0 through 255
Char(single character) 2 Bytes 0 through 65535
Date 8 Bytes 0:00:00 on Jan1,0001 through11:59:59 PM on December
31,9999
Decimal 16 Bytes Very Huge Range
Double 8 Bytes Very Huge Range
Integer 4 Bytes -2147483648 to 2147483647Long 8 bytes Almost 3 times the range of
Integer
String Depends upon Platform Vey Huge Range
7/27/2019 11115_Programming Constructs in VB
4/25
Data Type Conversion
While Working with data types in Visual Basic, there can be a need to
convert variable of one data type into some other data type. So, in that
case we need to use type conversion. While doing conversion we must
ensure that compatibility between two types is there or not.
For achieving this we have various Data type conversion functions such
as:-
CBool
CByte
CChar
CDate
CInt
CStr
7/27/2019 11115_Programming Constructs in VB
5/25
Variables in Visual Basic
A variable is an identifier that denotes a storage area in the memory. This
area can have many values such as:-3,5.2 or Hello World.
We should remember following points while declaring variables in Visual
Basic such as:-
A variable name can only contain Alphabets, digits, and Underscores. A variable name should not begin with a digit or numeric value
A variable name cannot contain a blank space
Keywords cannot be used as variable names
Example for Declaration:-
Dim x As String
Dim Y As Integer
Dim Z As Char
7/27/2019 11115_Programming Constructs in VB
6/25
Constants in VB
We can also assign the values to the variables at the declaration time such
as:-
Dim x As String=Hello
Dim y As Integer=100
Dim Grades As Char=A Constants:- Constants are the name given to the values that do not
change during the execution of a program. It is useful when we have to
use the same value at several places in the program
To declare constant we need to use Const keyword in VB, such as:-
Const Pi=3.14159
7/27/2019 11115_Programming Constructs in VB
7/25
Working with different types of
Operators
Arithmetic Operators
Assignment Operators
Concatenation Operators
Comparison Operators Logical and bitwise Operators
7/27/2019 11115_Programming Constructs in VB
8/25
Operators Description
Arithmetic operators are used to perform arithmetic operations such as
subtraction, multiplication, etc
Example:----+,-,*,/,^,Mod(It will return Remainder)
Assignment operators stores the result of an operation in a variable on
the left side of the operator Example:---- =,*=,+=
Concatenation Operator:-The process of combining two text strings into
one string is called string concatenation.
Example:- &,+
Comparison Operators:-These operators are used to compare two
expressions. We can use these operators to compare numeric values,
strings etc.
Example:-=,,,= etc
7/27/2019 11115_Programming Constructs in VB
9/25
Operators continued..
Logical and Bitwise Operators:-Logical operators are those which are usedwith expressions and produce a Boolean Value.
The Not Operator:-It reverses the logical value of its operand in Boolean valuemeans True to False and False to True.
Example:-
Dim X,Y As BooleanX=Not 5>3(It will return False)
Y=Not 2>7(It will return True)
The And Operator:-It is used to perform logical conjunction of two Booleanexpressions.
Example:-Dim X,Y As Boolean
X=21>14 And 15>9
Y=12>26 And 24>8
7/27/2019 11115_Programming Constructs in VB
10/25
Operators Continued.
The Or operator:-This operator is used to perform a logical operation
between two Boolean expressions. If both the expressions returns false
results then the entire result is false otherwise it will return true.
The Xor Operator:-It performs the logical exclusion on two Boolean
expressions. If only one Boolean expression evaluates to true, then the
final result after applying the Xor operation is true.
Example:-
Dim X,Y As Boolean
X=28>14 Xor 5>9
Y=25>29 Xor14>15
7/27/2019 11115_Programming Constructs in VB
11/25
Operator Precedence
It is a set of rules that specifies the order in which the
operators in Visual Basic are evaluated. In other words,
operator precedence determines which operation is executed
first in an expression involving multiple operators.
According to the rules of precedence, arithmetic and
concatenation operators have higher precedence than the
comparison operators, comparison operators have higher
precedence than logical operators.
The arithmetic operators has highest precedence than all theavailable operators.
7/27/2019 11115_Programming Constructs in VB
12/25
Precedence order
1st Precedence:-Arithmetic Operators in the order such as:- Exponentiation(^)
Unary identity and negation(+,-)
Multiplication and Division(*,/)
Modulus Arithmetic(Mod)
Addition and Subtraction(+,-)
2nd Precedence:-Concatenation Operators +,&
3rd Precedence:-Comparison Operators Equality(=)
Inequality() Less than, greater than()
Greater than or equal to(>=)
Less than or equal to(
7/27/2019 11115_Programming Constructs in VB
13/25
Precedence Order Continued
Lowest Precedence:-Logical Operators
Negation(Not)
Conjunction(And)
Inclusion(Or) Exclusive Disjunction(Xor)
7/27/2019 11115_Programming Constructs in VB
14/25
Selection Statements
Selection Statements are statements that change the flow of a
program based on whether a specific condition is met or not.
Visual Basic supports two types of selection statements:-
The If.Else Statement
The Select..Case Statement
If . Else Statement
This statement is used to test whether a certain condition is
fulfilled or not. If condition is fulfilled, the program control istransferred to the blocks of code inside the ifstatement,
otherwise the program control is transferred to another block
of statement.
7/27/2019 11115_Programming Constructs in VB
15/25
If-Else Continued..
Syntax in VBIf condition [Then]
[statements]
[ElseIf elseifcondition [Then] ]
[Else
[elsestatements] ]End If
Example No. 1:-
Dim x As Integer
x = CInt(TextBox1.Text())
If x Mod 2 = 0 Then
MessageBox.Show(x & "is Even")
Else
MessageBox.Show(x & "is Odd")
End If
7/27/2019 11115_Programming Constructs in VB
16/25
Examples Continued.
Example No.2:-If-else,elseifDim x1 As String
x1 = TextBox1.Text
If x1 = "hello" Then
MessageBox.Show(TextBox1.Text)ElseIf x1 = "bye" Then
MessageBox.Show(TextBox1.Text)
ElseIf x1 = "lpu" Then
MessageBox.Show(TextBox1.Text)
ElseMessageBox.Show("You have Entered something else")
End If
7/27/2019 11115_Programming Constructs in VB
17/25
Iteration Statements
An Iteration Statement executes a statement or set of
statements repeatedly. Visual Basic supports four types of
Iteration Statements:-
The While..End While Statement
The Do.Loop Statement
The For.Next Statement
The For EachNext Statement
7/27/2019 11115_Programming Constructs in VB
18/25
Using WhileEnd While Statement
It continues to execute a set of statements as long as a givencondition is true.
Syntax:-
While condition
[Statements]
End While
Example:-
Dim x = 1
While x
7/27/2019 11115_Programming Constructs in VB
19/25
DoWhile/Until Loop Statement
It also executes a group of statements repeatedly. But as it is alreadyknown that Do loop will work for at least one time even if the condition isnot true. So it will also work accordingly.
Syntax:-
Do
[Statements]Loop while [condition]
Example:-
Dim x = 12
Do
ListBox1.Items.Add(x)
Loop While x
7/27/2019 11115_Programming Constructs in VB
20/25
Do-Until Loop
As Do-While Works until condition is true and when the condition is false
it will terminate, but we also have Do-Until loop which works until the
condition is false. When the condition becomes true it will get terminated.
Syntax:-
Do[Statements]
Loop Until [condition]
Example:-
Dim X As String
Do
X = InputBox("Correct Password Please")
Loop Until X = "Ranger"
7/27/2019 11115_Programming Constructs in VB
21/25
For.Next Statement
The ForNext statement is used when you have to execute a group a
statements repeatedly for a specified number of times.
Syntax:-
For counter [As datatype]=start to end [step step]
[statements]Next [counter]
Example 1:-
For i As Integer = 8 To 1 Step -2
ListBox1.Items.Add(i)
Next i
7/27/2019 11115_Programming Constructs in VB
22/25
For..Next Statement Continued..
Example 2:-
For i As Integer = 8 To 1 Step -2
If i = 4 Then
Continue For
End If
ListBox1.Items.Add(i)
Next I
Example 3:-
For i As Integer = 8 To 1 Step -2
If i = 2 Then
Exit For
End If
ListBox1.Items.Add(i)
Next i
7/27/2019 11115_Programming Constructs in VB
23/25
For Each..Next Statement
For Each.Next statement iterates through all the items in alist which may be an array or collection of objects. Thisstatement works in the same way as the ForNext statement.
The Benefit of using For Each..Next statement is that we
dont need to pass the starting and ending limits. It will iteratethrough the collection/list automatically.
Syntax:-
For Each element [As datatype] in group
[Statements]
[Exit For]
[Statements[
Next [element]
7/27/2019 11115_Programming Constructs in VB
24/25
For Each Next Continued
Example1:-
Dim num As Integer() = {30, 40, 50, 60, 70}
For Each i As Integer In num
ListBox1.Items.Add(i)
Next
Example 2:-
Dim num As Integer() = {30, 40, 50, 60, 70}
For Each i As Integer In num
If i = 40 Then
Continue ForEnd If
ListBox1.Items.Add(i)
Next
7/27/2019 11115_Programming Constructs in VB
25/25
For EachNext Continued
Example 3:-
Dim num As Integer() = {30, 40, 50, 60, 70}
For Each i As Integer In num
If i = 40 Then
Exit ForEnd If
ListBox1.Items.Add(i)
Next