Tuesday, June 28, 2016

Learn VB Script Part 7

Using Loop Statements

Do.. Loop Statement

Do...Loop: Repeats a block of statements while a condition is True or until a condition becomes True.

Syntax:

 Do [{While | Until} condition]
   [statements]
   [Exit Do]
   [statements]
Loop

OR

Do
   [statements]
   [Exit Do]
   [statements]

Loop [{While | Until} condition]


Examples 1
Do...Loop

Counter = 1

    Do While Counter < 4

                        Total =Inputbox("Please enter the total marks in numbers")
                        
                                    If   Total < 50 Then
                                    
                                                MsgBox  "Fail"
                                    
                                    ElseIf  Total >=50 and Total <=60   then
                                    
                                                Msgbox "Second Class"
                        
                                    ElseIf  Total >60 and Total <80   then
                                    
                                                Msgbox "First Class"
                        
                                    ElseIf  Total >=80  then
                                    
                                                Msgbox "Distinction"
                        
                                    Else
                        
                                                Msgbox "Invalid Marks"
                                    
                                    End If

        Counter = Counter + 1

        Loop

Example 2

Counter = 1

            Do 

                        Total =Inputbox("Please enter the total marks in numbers")
                        
                                    If   Total < 50 Then
                                    
                                                MsgBox  "Fail"
                                    
                                    ElseIf  Total >=50 and Total <=60   then
                                    
                                                Msgbox "Second Class"
                        
                                    ElseIf  Total >60 and Total <80   then
                                    
                                                Msgbox "First Class"
                        
                                    ElseIf  Total >=80  then
                                    
                                                Msgbox "Distinction"
                        
                                    Else
                        
                                                Msgbox "Invalid Marks"
                                    
                                    End If

        Counter = Counter + 1

        Loop until Counter > 4

Example 3

   'Exit Do While loop

Do While Counter < 10

   Counter=counter + 1

   If  counter = 6 Then

      Exit Do

   End If

   MsgBox Counter

Loop


Example 4
'Exit Do Until loop

Do
    
   Counter=Counter + 1

     If  Counter = 6 Then

        Exit Do

     End If

   MsgBox Counter
   
Loop Until counter >11




Sunday, June 5, 2016

Learn VB Script Part 6

Using Conditional Statements

Select Case statement

Select Case: Executes one of several groups of statements, depending on the value of an expression. 

Syntax:

Select Case testexpression
   [Case expressionlist-n
      [statements-n]] . . .
   [Case Else expressionlist-n
      [elsestatements-n]]

End Select



Example: 
'Select Case


Colour =Ucase( Inputbox("Please enter the colour of your choice"))

          Select Case Colour
        
                                    Case   "RED"
        
                                                            MsgBox  "Colour selected is Red"
        
                                    Case   "BLUE"
        
                                                            MsgBox  "Colour selected is Red"
        
                                    Case   "GREEN"
        
                                                            MsgBox  "Colour selected is Red"
        
                                    Case   ELSE
        
                                                            MsgBox  "Invalid Colour"

          End Select

Learn VB script Part 5

Using Conditional Statements


If...Then...Else Statement

If...Then...Else: Conditionally executes a group of statements, depending on the value of an expression.

Syntax:

If condition Then statements [Else elsestatements ] 

OR

If condition Then
   [statements]
[ElseIf condition-n Then
   [elseifstatements]] . . .
[Else
   [elsestatements]]
End If 


Example: 
'If...Then...Else

Total = Inputbox("Please enter the total marks in number ")

            If   Total >= 50 Then
            
                        MsgBox  "Pass"
            
            Else 
            
                        Msgbox "Fail"
            

            End If



Example: 
'If....ElseIf

Total = Inputbox("Please enter the total marks in number")

            If   Total < 50 Then
            
                        MsgBox  "Fail"
            
            ElseIf  Total >=50 and Total <=60   then
            
                        Msgbox "Second Class"

            ElseIf  Total >60 and Total <80   then
            
                        Msgbox "First Class"

            ElseIf  Total >=80  then
            
                        Msgbox "Distinction"

            Else

                        Msgbox "Invalid Marks"
            

            End If