Decision Structure
Sequence AàBà CàD
Selection If A àC. Action taken if condition is true.
If … Then statement
Statements execute if certain conditions hold true.
Syntax:
If <condition> Then
Statement 1
Statement 2
End If
If <condition> Then
Statement 1
Statement 2
End If
use relational operators like >, <, =, < >, >=, <=. The outcome of this testing is Boolean expression (true or false)
use relational and math operators simultaneously in the if…then statement. Note: Math operator gets evaluated first.
e.g. If intX + intY > 0 Then
lblMessage.Text = “XYZ”
End if
e.g. If intX + intY > 0 Then
lblMessage.Text = “XYZ”
End if
use function calls with relational operators e.g.
If Cint(txtInput.Text) > 0 Then
lblMessage.Text = “XYZ”
End if
If Cint(txtInput.Text) > 0 Then
lblMessage.Text = “XYZ”
End if
If…Then…Else statement
One group of statements execute if condition is true, another group of statements execute if condition is false.
Syntax:
If <condition> Then
Statement 1
Statement 2
Else
Statement 3
Statement 4
End If
If <condition> Then
Statement 1
Statement 2
Else
Statement 3
Statement 4
End If
If… Then… ElseIf Statement
if…then…else statements.
Syntax:
If <condition> Then
Statement 1
Statement 2
ElseIf
Statement 3
Statement 4
Else
Statement 5
Statement 6
End If
If <condition> Then
Statement 1
Statement 2
ElseIf
Statement 3
Statement 4
Else
Statement 5
Statement 6
End If
Nested If statements
An If statement inside another If statement. The inside statement executes if the first if condition is met.
For a set of statements to execute, both if conditions have to be true.
Logical Operators
Combines two or more relations expressions into a single expression AND Operator: TT=T, TF=F, FT=F, FF=F i.e. both expressions must be true.
Example:
If Age > 50 AND Weight > 190 Then
Do a compete checkup.
End If
If Age > 50 AND Weight > 190 Then
Do a compete checkup.
End If
Other operators: OR, XOR, Not
Short circuit evaluation: AndAlso: Modified And function where the second condition is tested only if the first condition is true.
If <condition 1> AndAlso <condition2> Then: Tests condition 2 only if condition 1 is true
If <condition 1> and <condition 2> Then: Tests both conditions every time.
If <condition 1> AndAlso <condition2> Then: Tests condition 2 only if condition 1 is true
If <condition 1> and <condition 2> Then: Tests both conditions every time.
Short circuit evaluation: OrElse: Modified Or function where the second condition is tested only if the first condition is false. This saves time.
If <condition 1> OrElse <condition2> Then: Skips condition 2 if condition 1 is true
If <condition 1> or <condition 2> Then: Tests both conditions every time.
The select case statement
Instead of using multiple if…then…elseif statements you can use a select case statement.
Syntax:
Select Case <TestExpression (e.g. TestScores)>
Select Case <TestExpression (e.g. TestScores)>
Case ExpressionList
One or more statements
Case ExpressionList
One or more statements
Case Else
One or more statements
End Select
Working with strings
Initialize the string variable using string.empty. Ex.: Dim strName As String = string.empty
You can compare two strings using logical operators e.g.
If strName1=strName2 Then
If strName1>strName2 Then
If strName <> “Joe” Then
Convert the string to upper case or lower case
StrNewName=strName.ToUpper() or strName.ToLower()
ToUpper or ToLower does not modify the original string.
control properties e.g. txtInput.Text.ToUpper()
Helpful in string comparisons e.g. to compare Joe with JOE.
IsNumeric function
Accepts string as an argument and returns true if string contains a number
Ex: IsNumeric (“1223”) returns true (notice “1223” is a string)
Ex: IsNumeric(“1A”) returns false
Accepts string as an argument and returns true if string contains a number
Ex: IsNumeric (“1223”) returns true (notice “1223” is a string)
Ex: IsNumeric(“1A”) returns false
String Length
Returns number of characters in the string
Ex: intNumChars=strName.Length
You can use it with control properties e.g. txtInput.Text.Length
Trimming spaces from strings.
stringVariable.TrimStart() : Trims leading spaces
stringVariable.TrimStart() : Trims leading spaces
stringVariable.TrimEnd(): Trims trailing spaces
stringVariable.Trim(): Trims leading and trailing spaces.
You can use it with control properties e.g. txtInput.Text.Trim()
Substring Method
Substring method returns a part of string.
stringVariable.Substring (start) : Start is the starting position of the substring.
stringVariable.Substring(start, Length): Length is the number of characters to extract.
Substring method returns a part of string.
stringVariable.Substring (start) : Start is the starting position of the substring.
stringVariable.Substring(start, Length): Length is the number of characters to extract.
Remember string positions start at 0 e.g. USA: U is at position 0, S is at position 1, A is at position 2.
Indexof Method
IndexOf searches for a character in a string and returns the position of that character
Ex. stringVariable.IndexOf(“A”) returns the position of A in the string. (In “US of America” it will return 6 - the first instance of “A”). Returns (-1) if character not found.
Ex. stringVariable.IndexOf (“A”, 7) starts the search for A after the 7th position.
Ex. stringVariable.IndexOf(“A”, 7 , 5) starts the search for A after the 7th position and ends it after 5 characters (or the 12th position)
Make sure you know the length of the string before using indexof. Position outside of length range will create runtime error.
Message Box
Message box is a pop-up window that displays a message to users
messageBox.Show (message)
messagebox.show(message, caption)
messagebox.show(message, caption, buttons)
messagebox.show(message, caption, buttons, icon)
messagebox.show(message, caption, buttons, icon, defaultButton)
The outcomes of message box can be stored in a variable e.g.
intResult =MessageBox.Show(“Enter number”, “Error”, messageBoxButtons.YesNo)
if intResult = Windows.Form.DialogResult.No then
Do something
End If
intResult =MessageBox.Show(“Enter number”, “Error”, messageBoxButtons.YesNo)
if intResult = Windows.Form.DialogResult.No then
Do something
End If
Display multiple lines: Control characters
ControlChars.CrLf inserts a new line
Ex. MessageBox.show(“This is line 1” & controlchars.CrLf & “This is line 2”)
ControlChars.CrLf inserts a new line
Ex. MessageBox.show(“This is line 1” & controlchars.CrLf & “This is line 2”)