You are on page 1of 15

Decision Structures and

Commands
If Statements

• An If statement implements branching logic based


on a testable condition.
Organization of If Statement program
logic

• Two techniques can be used:


a) Pseudo code
b)Flow chart
If statement pseudo code

Refers to a combination of VB and English to help you


organize program logic.
Example:
If Weather=Warm Then
Go golfing
Mow the grass
Sit on the porch
Else
Read a book
Drink hot chocolate
End If
example

• John prefers to go to church on the weekend.


• Design a pseudo code for the above scenario…
• If day=weekend then
• Go to church
• End if
If Statement flowchart

Refers to a logic diagram that can also help you


organize program logic.
The general format of the If statement:

If <condition> Then
(condition is true - do all of the actions coded in this
branch)
Else
(condition is false - do all of the actions coded in this
branch)
End If
If Statement rules

a) A block If statement must always end with an End If


statement.
b) The key word Then must appear on the same line as the
word If.
c) The key words Else and End If must appear on lines by
themselves.
d) The Else branch is optional (see example below). Only
use an Else statement if there is a False branch.
e) IF you have more than one condition, introduce the logic
statement AND in your code.
Example

Paul prefers to go to church and play football on the


Sunday and Saturday but tends to hang out with friends
on any other day.
If day = Sunday and day = Saturday then
Go to church
Play football
Else
Hangout with friends
End if
Example
Create a program that captures the following grading
system:
Score Grade

90 – 100 A

80 – 89 B

70 – 79 C

60 – 69 D

0 – 59 F

Above 100 Not Applicable


Write a VB Program that captures the student names,
marks for Assignment one and Assignment two. If
Assignment one is greater than or equal to 15 and
Assignment two is greater than or equal to 20, then the
program should display the total of Assignment one and
Assignment two. Else if assignment one is less than or
equal to 14 and assignment two is less than or equal to
19 then the program should display the message “The
student has a Supplementary”.
coding
Dim assignment1decimal As Decimal
Dim assignment2decimal As Decimal
Dim totalscoredecimal As Decimal
assignment1decimal = Assignment1TextBox.Text
assignment2decimal = Assignment2TextBox.Text
If assignment1decimal >= 15 And assignment2decimal >= 20 Then
totalscoredecimal = assignment1decimal + assignment2decimal
TotalscoreTextBox.Text = totalscoredecimal.ToString
ElseIf assignment1decimal <= 14 And assignment2decimal <= 19 Then
MessageBox.Show("The student has a supplemetary")
End If

End Sub
Creating a Login Form
1. Create a login form that accepts the password as “123”.
2. If the password is correct then the system should open
Form1 and hide the current form(login form).
3. Else if the password is not entered then the system should
display the message “Enter password to proceed”
4. Else if the password entered is incorrect then the system
should display the message “Incorrect Password”
Using If Functions to create Passwords
Password coding

If passwordTextBox.Text = "123" Then


Form20.Show()
Me.Hide()
Else
MessageBox.Show("Password is not correct")
End If

You might also like