Tuesday, May 10, 2011

Control structures - 1.Branching - 1. IF Statement


    
  Branching statements are used to cause certain actions within a program if a certain condition is met.

·         Syntax

 If <condition > Then statement
     
            If Balance - Check < 0 Then Print "You are overdrawn"
     
Here, if and only if Balance - Check is less than zero, the statement “You are overdrawn” is printed.

    If  Then   End If blocks to allow multiple statements:
      Syntax

IF <condition> then
      statement1
      statement2
      ................
      statementn
end if

eg1:-   Private Sub Command1_Click()
number = Text1.Text
If number Mod 2 = 0 Then
                                    MsgBox ("Even")
End If
End Sub

            If Balance - Check < 0 Then
              Print "You are overdrawn"
              Print "Authorities have been notified"
            End If

      In this case, if Balance - Check is less than zero, two lines of information are printed.



Nested IF
     
eg2     
IF Boolean Expression Then
            Statement1
            Statement2
            Statement2
            ....................
            ....................
            Statementn
Else
IF Boolean Expression Then
                        Statement1
                        Statement2
                        Statement2
                        ....................
                        ....................
                        Statementn
Else

                        Statement1
                        Statement2
                        Statement2
                        ....................
                        ....................
                        Statementn
            End If
End if


Private Sub Command1_Click()
number = Text1.Text
If number Mod 2 = 0 Then
            MsgBox ("Even")
Else
            MsgBox ("odd")
End If
End Sub


If Balance - Check < 0 Then
              Print "You are overdrawn"
              Print "Authorities have been notified"
            Else
              Balance = Balance - Check
            End If
     
Here, the same two lines are printed if you are overdrawn (Balance - Check < 0), but, if you are not overdrawn (Else), your new Balance is computed.

    Or, we can add the ElseIf statement:
     
            If Balance - Check < 0 Then
              Print "You are overdrawn"
              Print "Authorities have been notified"
            ElseIf Balance - Check = 0 Then
              Print "Whew! You barely made it"
              Balance = 0
            Else
              Balance = Balance - Check
            End If
     
Now, one more condition is added.  If your Balance equals the Check amount (ElseIf Balance - Check = 0), a different message appears.

·         In using branching statements, make sure you consider all viable possibilities in the If/Else/End If structure.  Also, be aware that each If and ElseIf in a block is tested sequentially.  The first time an If test is met, the code associated with that condition is executed and the If block is exited.  If a later condition is also True, it will never be considered.


   

.

No comments:

Post a Comment