Discussions

Friday, July 13, 2012

Sample Program #3
















Let us put an evaluation of the calculated sum using condition statement. The evaluations would be, "greater than 10", "less than 10" and "equal to 10" enhancing Sample Program #2.

'LONG METHOD STYLE CODE
Private Sub cmdcal_Click()
  Dim a, b, c As Integer

  a = Val(txtn1.Text)
  b = Val(txtn2.Text)
  c = a + b
  If (c < 10) Then
    txtsum.Text = "The sum is " + Trim(Str(c)) + " and is less than 10"
  ElseIf (c > 10) Then
    txtsum.Text = "The sum is " + Trim(Str(c)) + " and is greater than 10"
  Else
    txtsum.Text = "The sum is " + Trim(Str(c)) + " and is equal to 10"
  End If
End Sub

'SHORT METHOD STYLE CODE
Private Sub cmdcal_Click()
  txtsum.Text = "The sum is " + Trim(Str(Val(txtn1.Text) + Val(txtn2.Text)))
  If (Val(txtn1.Text) + Val(txtn2.Text) < 10) Then
    txtsum.Text = txtsum.Text + " and is less than 10"
  ElseIf (Val(txtn1.Text) + Val(txtn2.Text) > 10) Then
    txtsum.Text = txtsum.Text + " and is greater than 10"
  Else
    txtsum.Text = txtsum.Text + " and is equal to 10"
  End If
End Sub





Discussion:

Here, the condition statement if,elseif and else are connected thus if either of the conditions is true, the computer will no longer enter to the next condition block.