Wednesday, February 9, 2011

If Statements

■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 dedGrade1, decGrade2, decBetterGrade As Decimal    
  
    'Convert text box to double    
  
    decGrade1 = CDec(txtSecondGrade.Text)    
  
    decGrade2 = CDec(txtSecondGrade.Text)    
  
    'Find results    
  
    If decGrade1 > decGrade2 Then     
  
      decBetterGrade = decGrade1    
  
    Else     
  
      decBetterGrade = decGrade2    
  
    End If    
  
    'Display results    
  
    txtBestGrade.Text = "Your best grade is " & decBetterGrade & "."   
  
   End Sub  
  
 End Class  

■An if statement where it's either or
 Public Class Form1
  
   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  
     'Declare variables    
  
     Dim decGPA As Decimal
  
     If decGPA < 3.5 Then
  
       MessageBox.Show("You did not make the dean's list")
  
     Else
  
       MessageBox.Show("You made the dean's list")
  
     End If
  
   End Sub
  
 End Class  

■An if statement where many decisions are made
 Public Class Form1
  
   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  
     'Declare variables    
  
     Dim decGrade As Decimal
  
     If decGrade >= 91 Then
  
       MessageBox.Show("You get an A")
  
     ElseIf decGrade >= 82 Then
  
       MessageBox.Show("You get a B")
  
     ElseIf dedGrade >= 70 Then
  
       MessageBox.Show("You get a C")
  
     ElseIf decGrade >= 60 Then
  
       MessageBox.Show("You get a D")
  
     Else
  
       MessageBox.Show("You get an F")
  
     End If
  
   End Sub
  
 End Class  

No comments:

Post a Comment