728x90 AdSpace

Latest Article

IF Else Condition in VB Exercise Practice Question





IF Else Condition in Visual Basic Exercise Practice Question An If block allows a program to decide on a course of action based on whether a certain condition is true or false. An If-block looks like following:

            If Condition1 Then
                        Action1
            Else
                        Action2
            End If

This block causes the program to take Action1 if Condition1 is true and Action2 if Condition1 is false.

Exercise:

We will now write a program which will find the larger of the two numbers entered by the user.

  1. Make an interface which looks like follows:





  1. Type in the following code:

Private Sub Command1_Click()
Dim largerNumber As Single
Picture1.Cls

If Val(Text1.Text) > Val(Text2.Text) Then
    largerNumber = Val(Text1.Text)
Else
    largerNumber = Val(Text2.Text)
End If

Picture1.Print "The larger number is"; largerNumber

End Sub

  1. Run the program

We can also have an If-Block which looks like follows:

If Condition1 Then
                        Action1
            ElseIf Condition 2
                        Action2
            Else
                        Action3
            End If

Now redo the previous exercise and write the following code. The amendment in this code is that it checks if the two numbers are equal or not.

            Private Sub Command1_Click()
Picture1.Cls

If Val(Text1.Text) > Val(Text2.Text) Then
    largerNumber = Val(Text1.Text)
    Picture1.Print "The larger number is"; Val(Text1.Text)
   
ElseIf Val(Text2.Text) > Val(Text1.Text) Then
    Picture1.Print "The larger number is"; Val(Text2.Text)
   
Else
    Picture1.Print "The two numbers are equal"

End Sub








IF Else Condition in VB Exercise Practice Question
  • Title : IF Else Condition in VB Exercise Practice Question
  • Posted by :
  • Date : 10:57
  • Labels :





  • Blogger Comments
  • Facebook Comments

0 comments:

Post a Comment

Top