Showing posts with label If statment. Show all posts
Showing posts with label If statment. Show all posts

Tuesday, February 1, 2011

If Statements in Visual Basic

An if statement that makes one decision:

 Public Class Form1  
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Declare variables
Dim num1, num2, largerNum As Double
'Convert text box to double
num1 = CDbl(txtFirstNum.Text)
num2 = CDbl(txtSecondNum.Text)
'Find results
If num1 > num2 Then
largerNum = num1
Else
largerNum = num2
End If
'Display results
txtResults.Text = "The larger number is " & largerNum & "."
End Sub
End Class


An if statement where it's either or:


 Public Class Form1  
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Declare variables
Dim message As String
message = "On a bad day, I have mood swings - but on a good day, I have the whole mood playground – Charles Rosenblum"
If txtmessage.Text.ToUpper = "Y" Then
MessageBox.Show(message, "Quote")
End If
MessageBox.Show("At first, I only laughed at myself. Then I noticed that life itself is amusing. I've been in a generally good mood ever since. – Marilyn vos Savant", "Bad Mood Quote")
End Sub
End Class


An if statement where many decisions are made:

 Public Class Form1  
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Declare variables
Dim gpa As Double = CDbl(txtGpa.Text)
Dim honors As String
If gpa >= 3.9 Then
honors = " summa cum laude."
ElseIf gpa >= 3.6 Then
honors = " magna cum laude."
ElseIf gpa >= 3.3 Then
honors = " cum laude."
ElseIf gpa >= 2 Then
honors = "."
End If
txtResults.Text = "You graduated" & honors
End Sub
End Class