2.1 - selection structure

11
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() DEFINE username AS string DEFINE password AS String READ username READ password IF (username=”Shalini” AND password=”12345”) THEN DISPLAY “Authentication successful” ENDIF END PROCEDURE 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 'check if username and passowrd is correct If (username = "Shalini" And password = "12345") Then MsgBox("Authentication successful") End If End Sub Prepared by Shalini Fernando Page 1

Upload: trey

Post on 01-Feb-2016

222 views

Category:

Documents


0 download

DESCRIPTION

yuk

TRANSCRIPT

Page 1: 2.1 - Selection Structure

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()

DEFINE username AS string

DEFINE password AS String

READ usernameREAD password

IF (username=”Shalini” AND password=”12345”) THEN DISPLAY “Authentication successful”

ENDIF

END PROCEDURE

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

'check if username and passowrd is correct If (username = "Shalini" And password = "12345") Then MsgBox("Authentication successful") End If

End Sub

Prepared by Shalini Fernando Page 1

Page 2: 2.1 - Selection Structure

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()

DEFINE intAge AS INTEGER

intAge = INPUT (“What is your Age?")READ intAge

IF intAge < 50 THEN

DISPLAY “Your Age is less than 50”

ENDIFEND PROCEDURE

Private Sub btnAge()

'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, 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

Prepared by Shalini Fernando Page 2

Page 3: 2.1 - Selection Structure

Selection (Condition) - IF, IF … ELSE and CASE Pseudocode and related VB.NET coding 2012

Prepared by Shalini Fernando Page 3

Page 4: 2.1 - Selection Structure

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()

DEFINE username AS string

DEFINE password AS String

READ usernameREAD password

IF (username=”Shalini” AND password=”12345”) THEN DISPLAY “Authentication successful”ELSE DISPLAY “Invalid username and password”

ENDIF

END PROCEDURE

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

'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

Prepared by Shalini Fernando Page 4

Page 5: 2.1 - Selection Structure

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()

DEFINE intAge AS INTEGER

intAge = INPUT (“What is your Age?")READ intAge

IF intAge < 50 THEN

DISPLAY “Your Age is less than 50”

ELSE DISPLAY “Your Age is more than 50”

ENDIF

END PROCEDURE

Private Sub btnAge()

'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"

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

'Print “Your Age is more than 50” in a lable called lblResult lblResult.Text = "Your Age is more than 50" End If

End Sub

Prepared by Shalini Fernando Page 5

Page 6: 2.1 - Selection Structure

Selection (Condition) - IF, IF … ELSE and CASE Pseudocode and related VB.NET coding 2012

Prepared by Shalini Fernando Page 6

Page 7: 2.1 - Selection Structure

Selection (Condition) - IF, IF … ELSE and CASE Pseudocode and related VB.NET coding 2012

CASE Condition

You’re 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()

READ Maths marksSEND Maths mark to function calGrade to check grade DISPLAY Maths grade

READ English marksSEND English mark to function calGrade to check grade DISPLAY English grade

READ Science marksSEND Science marks to function calGrade to check grade DISPLAY Science grade

READ Religious marksSEND Religious mark to function calGrade to check grade DISPLAY Religious grade

READ Physics marksSEND 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*"

Private Sub btnCal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCal.Click

'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

Prepared by Shalini Fernando Page 7

Page 8: 2.1 - Selection Structure

Selection (Condition) - IF, IF … ELSE and CASE Pseudocode and related VB.NET coding 2012

>=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

Return "A*"

'check mark is greater than or equals to 80, if true return A Case Is >= 80 Return "A"

'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

Prepared by Shalini Fernando Page 8

Page 9: 2.1 - Selection Structure

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()

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"

Private Sub btnAge()

'Declaring Integer type variable called IntAgeDim 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"

Prepared by Shalini Fernando Page 9

Page 10: 2.1 - Selection Structure

Selection (Condition) - IF, IF … ELSE and CASE Pseudocode and related VB.NET coding 2012

ELSE DISPLAY "Your Age is more than 61"

ENDCASEEND PROCEDURE

Case 41 To 60 : lblResult.Text = "Your Age is between 41 to 60"

'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

Prepared by Shalini Fernando Page 10