Conditional Statements

Flow Control (Conditional Statements)

We can control the flow of our script with conditional statements and looping statements.

Using conditional statements, we can write VBScript code that makes decisions and repeats actions. The following conditional statements are available in VBScript:

1) If…Then…Else Statement
2) Select Case Statement

Making Decisions Using If...Then...Else

The If...Then...Else statement is used to evaluate whether a condition is True or False and, depending on the result, to specify one or more statements to run.

Usually the condition is an expression that uses a comparison operator to compare one value or variable with another.

If...Then...Else statements can be nested to as many levels as you need.

1)  Running a Statement if a Condition is True (single statement)

To run only one statement when a condition is True, use the single-line syntax for the If...Then...Else statement.

Dim myDate
myDate = #2/13/98#
If myDate < Now Then myDate = Now

2)  Running Statements if a Condition is True (multiple statements)
To run more than one line of code, we must use the multiple-line (or block) syntax. This syntax includes the End If statement.

Dim x
x= 20
If x>10 Then

msgbox "x value is: "&x
msgbox "Bye Bye"
End If

3) Running Certain Statements if a Condition is True and Running Others if a Condition is False

We can use an If...Then...Else statement to define two blocks of executable statements: one block to run if the condition is True, the other block to run if the condition is False.

Example:

Dim x
x= Inputbox (" Enter a value")
If x>100 Then
Msgbox "Hello G.C.Reddy"
Msgbox "X is a Big Number"
Msgbox "X value is: "&X
Else
Msgbox "GCR"
Msgbox "X is a Small Number"
Msgbox "X value is: "&X
End If

4)  Deciding Between Several Alternatives

A variation on the If...Then...Else statement allows us to choose from several alternatives. Adding ElseIf clauses expands the functionality of the If...Then...Else statement so we can control program flow based on different possibilities.

Example:
Dim x
x= Inputbox (" Enter a value")


If x>0 and x<=100 Then
Msgbox "Hello G.C.Reddy"
Msgbox "X is a Small Number"
Msgbox "X value is "&x

Else IF x>100 and x<=500 Then
Msgbox "Hello GCR"
Msgbox "X is a Medium Number"

Else IF x>500 and x<=1000 Then
Msgbox "Hello Chandra Mohan Reddy"
Msgbox "X is a Large Number"

Else
Msgbox "Hello Sir"
Msgbox "X is a Grand Number"
End If
End If
End If

5) Executing a certain block of statements when two / more conditions are True (Nested If...)

Example:

Dim State, Region
State=Inputbox ("Enter a State")
Region=Inputbox ("Enter a Region")

If state= "AP" Then
If Region= "Telangana" Then
msgbox "Hello G.C.Reddy"
msgbox "Dist count is 10"

Else if Region= "Rayalasema" Then
msgbox "Hello GCR"
msgbox "Dist count is 4"

Else If Region= "Costal" Then
msgbox "Hello Chandra mohan Reddy"
msgbox "Dist count is 9"

End If
End If
End If
End If

Making Decisions with Select Case

The Select Case structure provides an alternative to If...Then...ElseIf for selectively executing one block of statements from among multiple blocks of statements. A Select Case statement provides capability similar to the If...Then...Else statement, but it makes code more efficient and readable.
Example:

Option explicit
Dim x,y, Operation, Result
x= Inputbox (" Enter x value")
y= Inputbox ("Enter y value")
Operation= Inputbox ("Enter an Operation")

Select Case Operation

Case "add"
Result= cdbl (x)+cdbl (y)
Msgbox "Hello G.C.Reddy"
Msgbox "Addition of x,y values is "&Result

Case "sub"
Result= x-y
Msgbox "Hello G.C.Reddy"
Msgbox "Substraction of x,y values is "&Result

Case "mul"
Result= x*y
Msgbox "Hello G.C.Reddy"
Msgbox "Multiplication of x,y values is "&Result

Case "div"
Result= x/y
Msgbox "Hello G.C.Reddy"
Msgbox "Division of x,y values is "&Result

Case "mod"
Result= x mod y
Msgbox "Hello G.C.Reddy"
Msgbox "Mod of x,y values is "&Result

Case "expo"
Result= x^y
Msgbox "Hello G.C.Reddy"
Msgbox"Exponentation of x,y values is "&Result

Case Else
Msgbox "Hello G.C.Reddy"
msgbox "Wrong Operation"

End Select

0 comments:

Post a Comment