Вы находитесь на странице: 1из 8

Selection (Condition) - IF, IF ELSE and CASE

Pseudocode and related VB.NET coding

2012

IF Condition
You can make a choice on your code depending what happens if the condition is met.
Program 1:
Check if username and password is correct in the login form
Pseudocode

VB.NET

PROCEDURE OK_Click()

Private Sub OK_Click(ByVal sender As System.Object, ByVal


e As System.EventArgs) Handles OK.Click
'get username
Dim username As String = txtusername.Text
'get password
Dim password As String = txtpassword.Text

DEFINE username AS string


DEFINE password AS String
READ username
READ password
IF (username=Shalini AND
password=12345) THEN

'check if username and passowrd is correct


If (username = "Shalini" And password = "12345")
Then

MsgBox("Authentication successful")

DISPLAY Authentication successful


ENDIF

End If
End Sub

END PROCEDURE

Prepared by Shalini Fernando

Page 1

Selection (Condition) - IF, IF ELSE and CASE


Pseudocode and related VB.NET coding

2012

Program 2:
Allow a user to enter their age. Then check if the input value is less than 50. If the condition is
true, then display a message Your Age is less than 50.

Pseudocode

VB.NET

PROCEDURE btnAge()

Private Sub btnAge()

DEFINE intAge AS INTEGER

'Declaring Integer type variable called IntAge


Dim intAge As Integer

intAge = INPUT (What is your Age?")


READ intAge

'Asking user to input their age and assign the age


value to IntAge variable
intAge = InputBox("What is your Age?")

IF intAge < 50 THEN


DISPLAY Your Age is less than 50
ENDIF
END PROCEDURE

Prepared by Shalini Fernando

'Check if IntAge value is less than 50, if it is less


than 50 go inside the loop
If intAge < 50 Then
'Print Your Age is less than 50 in a label called
lblResult
lblResult.Text = "Your Age is less than
50"
End If
End Sub

Page 2

Selection (Condition) - IF, IF ELSE and CASE


Pseudocode and related VB.NET coding

2012

IF...ELSE Condition
You can make choices in your code depending on what happens if one condition is met, and what
happens if the condition is not met.
Program 1:
Check if username and password is correct in the login form and display messages
Pseudocode

VB.NET

PROCEDURE OK_Click()

Private Sub OK_Click(ByVal sender As System.Object, ByVal


e As System.EventArgs) Handles OK.Click
'get username
Dim username As String = txtusername.Text
'get password
Dim password As String = txtpassword.Text

DEFINE username AS string


DEFINE password AS String
READ username
READ password
IF (username=Shalini AND
password=12345) THEN
DISPLAY Authentication successful
ELSE
DISPLAY Invalid username and
password

'check if username and passowrd is correct


If (username = "Shalini" And password = "12345")
Then

MsgBox("Authentication successful")
Else
'if passowrd is not correct display error
MsgBox("Invalid username or password")
End If
End Sub

ENDIF
END PROCEDURE

Prepared by Shalini Fernando

Page 3

Selection (Condition) - IF, IF ELSE and CASE


Pseudocode and related VB.NET coding

2012

Program 2:
Allow a user to enter their age. First you check if your age is less than 50. If the condition is true,
display a message Your Age is less than 50 and anything else display a message Your age is
more than 50.

Pseudocode

VB.NET

PROCEDURE btnAge()

Private Sub btnAge()

DEFINE intAge AS INTEGER

intAge = INPUT (What is your


Age?")
READ intAge
IF intAge < 50 THEN

'Declaring Integer type variable called IntAge


Dim intAge As Integer
'Asking user to input their age and assign the age value
to IntAge variable
intAge = InputBox("What is your Age?")
'Check if IntAge value is less than 50, go to if ststment
If intAge < 50 Then
'Print Your Age is less than 50 in a lable called lblResult
lblResult.Text = "Your Age is less than 50"

DISPLAY Your Age is less than 50

'If intAge value is more than 50 go to else statment


Else

ELSE
DISPLAY Your Age is more than
50

'Print Your Age is more than 50 in a lable called


lblResult
lblResult.Text = "Your Age is more than 50"
End If

ENDIF

End Sub

END PROCEDURE

Prepared by Shalini Fernando

Page 4

Selection (Condition) - IF, IF ELSE and CASE


Pseudocode and related VB.NET coding

2012

CASE Condition

Youre using Select Case conditions when you have to get a value from the user and respond in
several different ways. Basically, it tests an expression, determine which of several cases it
matches, and execute the corresponding Visual Basic code.
Program 1:
Allow use to calculate GCSE grades based on student marks
Pseudocode

VB.NET

PROCEDURE btnCal_click()

Private Sub btnCal_Click(ByVal sender As System.Object,


ByVal e As System.EventArgs) Handles btnCal.Click

READ Maths marks


SEND Maths mark to function calGrade to
check grade
DISPLAY Maths grade
READ English marks
SEND English mark to function calGrade to
check grade
DISPLAY English grade
READ Science marks
SEND Science marks to function calGrade
to check grade
DISPLAY Science grade
READ Religious marks
SEND Religious mark to function calGrade
to check grade
DISPLAY Religious grade
READ Physics marks
SEND Physics mark to function calGrade
to check grade
DISPLAY Physics grade
END PROCEDURE

FUNCTION calGrade(DEFINE marks as Integer)


as String
CASE OF marks
>=90:
DISPLAY "A*"
Prepared by Shalini Fernando

'send Maths marks to calGrade function to check the


grade
'Display the grade in the label
lblMaths.Text = calGrade(txtMaths.Text)
'send English marks to calGrade function to check
the grade
'Display the grade in the label
lblEnglish.Text = calGrade(txtEnglish.Text)
'send Science marks to calGrade function to check
the grade
'Display the grade in the label
lblScience.Text = calGrade(txtScience.Text)
'send Religion marks to calGrade function to check
the grade
'Display the grade in the label
lblReligious.Text = calGrade(txtReligious.Text)
'send Physics marks to calGrade function to check
the grade
'Display the grade in the label
lblPhysics.Text = calGrade(txtPhysics.Text)
End Sub

'Function to check gades and return the grade


Private Function calGrade(ByVal marks As Integer) As
String
'pass mark into case
Select Case marks
'check mark is greater than or equals to 90, if true
return A*
Case Is >= 90
Return "A*"
'check mark is greater than or equals to 80, if
true return A
Page 5

Selection (Condition) - IF, IF ELSE and CASE


Pseudocode and related VB.NET coding

2012

Case Is >= 80
Return "A"

>=80:
DISPLAY " A

>=70:
DISPLAY " B"
>=60:
DISPLAY " C"

>=50:
DISPLAY " D"

>=40:
DISPLAY " E"

>=30:
DISPLAY " F"

>=20:
DISPLAY " G"

ELSE
DISPLAY "Ungraded"
ENDCASE
END FUNCTION

Prepared by Shalini Fernando

'check mark is greater than or equals to 70, if


true return B
Case Is >= 70
Return "B"
'check mark is greater than or equals to 60, if
true return C
Case Is >= 60
Return "C"
'check mark is greater than or equals to 50, if
true return D
Case Is >= 50
Return "D"
'check mark is greater than or equals to 40, if
true return E
Case Is >= 40
Return "E"
'check mark is greater than or equals to 30, if
true return F
Case Is >= 30
Return "F"
'check mark is greater than or equals to 20, if
true return G
Case Is >= 20
Return "G"
'check mark is less than 20 or anthing else, if
true return Ungraded
Case Else
Return "Ungraded"
End Select
End Function

Page 6

Selection (Condition) - IF, IF ELSE and CASE


Pseudocode and related VB.NET coding

2012

Program 2:
Allow a user to enter their age. Depending on the input value:

if your age is between 1 to 20 display a message "Your Age is between 1 to 20"

if it is between 21 to 40 display a message " Your Age is between 21 to 40"

if it is between 41 to 60 display a message " Your Age is between 41 to 60"

anything else display Your Age is more than 61"

Pseudocode

VB.NET

PROCEDURE btnAge()

Private Sub btnAge()

DEFINE intAge AS INTEGER

intAge = INPUT (What is your Age?")


READ intAge
CASE OF intAge
1 to 20: DISPLAY "Your Age is between 1
to 20"
21 to 40: DISPLAY " Your Age is between
21 to 40"
41 to 60: DISPLAY " Your Age is between
41 to 60"

Prepared by Shalini Fernando

'Declaring Integer type variable called IntAge


Dim intAge As Integer
'Asking user to input their age and assign the age
value to IntAge variable
intAge = InputBox("What is your Age?")
'Read intAge value
Select Case intAge
'If entered user Age is between 1 to 20, print "Your
Age is between 1 to 20"
Case 1 To 20 : lblResult.Text = "Your
Age is between 1 to 20"
'If entered user Age is between 21 to 40, print
"Your Age is between 21 to 40"
Case 21 To 40 : lblResult.Text =
"Your Age is between 21 to 40"
'If entered user Age is between 41 to 60, print
"Your Age is between 41 to 60"
Case 41 To 60 : lblResult.Text =
"Your Age is between 41 to 60"

Page 7

Selection (Condition) - IF, IF ELSE and CASE


Pseudocode and related VB.NET coding
ELSE
DISPLAY "Your Age is more than
61"
ENDCASE
END PROCEDURE

Prepared by Shalini Fernando

2012

'If entered user Age is more than 60, print "Your


Age is more than 60"
Case Else : lblResult.Text = "Your
Age is more than 61"
End Select
End Sub

Page 8

Вам также может понравиться