Showing posts with label if else. Show all posts
Showing posts with label if else. Show all posts

Tuesday, April 13, 2010

Answers to Assignments for If Else Statement

Please find the answer to the assignment on If Else statements.

1. Write a script to print the greatest of three integers.


Look at the script below.

Var1 = CINT(Inputbox("Enter value for Var1: "))
Var2 = CINT(Inputbox("Enter value for Var2: "))
Var3 = CINT(Inputbox("Enter value for Var3: "))

if Var1=Var2 and Var1=Var3 then
aaaamsgbox"All the values are same: "&Var1
elseif Var1=Var2 and Var1>Var3 then
aaaamsgbox"Var1 & Var2 i.e. " &Var1& " is greater than "&Var3
elseif Var1=Var3 and Var1>Var2 then
aaaamsgbox"Var1 & Var3 i.e. " &Var1& " is greater than "&Var2
elseif Var2=Var3 and Var2>Var1 then
aaaamsgbox "Var2 & Var3 i.e. " &Var2& " is greater than "&Var1
elseif Var1>Var2 and Var1>Var3 then
aaaamsgbox "Var1 i.e. "&Var1& " is greater than "&Var2& ", "&Var3
elseif Var2>Var1 and Var2>Var3 then
aaaamsgbox "Var2 i.e. "&Var2& " is greater than "&Var1& ", "&Var3
else
aaaamsgbox "Var3 i.e. "&Var3& " is greater than "&Var1& ", "&Var2
end if

In the above script we are joining the values and some meaning ful string to the message and this is done using the '&' which is to join the strings.

2. Write a script to print the least of the three integers.

For this please think and write. you have to only make some small change.

Back to If Else Statement page

VB Scripting - Else If Statement

Previous11121314151617181920Next


The If Else statement we discussed previously executes one block of code if the value is greater than 80 or executes the other piece of code if the condition is not satisfied. Now think what you will do if you have different ranges.

To do this you need Else If statement.



What Else if does is that it creates and if statement within an if statement and goes on and in this manner you can check different conditions in a single if Statement.

Syntax for Else If Statement



If [condition] Then
Code
Elseif [condition] Then
Code
Elseif [condition] Then
Code
Elseif [condition] Then
Code
Else
Code
End If


Example to show how Else If Statement is used


Now let write a script to see how this works. We will calculate the average for three subjects and based on the percentage we will display the average and the appropriate messages.


'Calculating the average for three subjects
mt = Inputbox("Enter the marks scored for Manual Testing:")
qtp = Inputbox("Enter the marks scored for QTP:")
sql = Inputbox("Enter the marks scored for SQL:")
'Calculating average
avg=(mt+qtp+sql)/3
'Displaying the average to the user
msgbox "The average marks scored is: "&avg
'Branching statements to display appropriate message
If avg >= 90 Then
msgbox "You scored "&avg&" percentage and you will get a Job"
ElseIf avg >= 80 and avg < 90 Then
msgbox "You scored "&avg&" percentage and you have to put in more effort"
ElseIf avg >= 70 and avg < 80 Then
msgbox "You scored "&avg&" percentage and you are not up to the mark"
Else
msgbox "You scored "&avg&" percentage and you have to repeat the course"
End If



What happened why did this result came as 100.333333333333 and the first message will be displayed which is wrong.

Let’s debug the script line by line.

mt=3, qtp=0, sql=1
avg=(mt+qtp+sql)/3
avg=(3+0+1)/3
avg 301/3
avg=100.333333333333


Let me explain you what happened so that we will be clear as to what happened and how to rectify the code.

Even though we say that in the condition avg=(mt+qtp+sql)/3 we have used addition (+) operator but just to remind that the default type of Inputbox is string because we have not explicitly declared the variables and that’s why by default what ever values you have entered in the input box is taken as string and in context of this + is treated as a concatenation operator which means that it will join the three values as 3+0+1 to make 301 and not add them up to 4. That is the reason we are getting a different result.

In order to rectify this issue we need to use a conversion type (Type Cast) which in our case actually converts the default data type to integer type. For this we need to use CINT. CINT can be used with input box or while on the expression calculating the average (In our case). You cannot use CINT for some variables while accepting values and for some on the expression. You can either use CINT on the expression or while taking input on variables.


Let’s rewrite the code and see what happens

'Calculating the average for three subjects
mt = CINT(Inputbox("Enter the marks scored for Manual Testing:"))
qtp = CINT(Inputbox("Enter the marks scored for QTP:"))
sql = CINT(Inputbox("Enter the marks scored for SQL:"))
'Calculating average
avg=(mt+qtp+sql)/3
'Displaying the average to the user
msgbox "The average marks scored is: "&avg
'Branching statements to display appropriate message
If avg >= 90 Then
msgbox "You scored "&avg&" percentage and you will get a Job"
ElseIf avg >= 80 and avg < 90 Then
msgbox "You scored "&avg&" percentage and you have to put in more effort"
ElseIf avg >= 70 and avg < 80 Then
msgbox "You scored "&avg&" percentage and you are not up to the mark"
Else
msgbox "You scored "&avg&" percentage and you have to repeat the course"
End If




Now run the script and you will get the right percentage and the required message.

Note: You can also use CINT on the expression instead on inputbox. It will look like this.
(CINT(mt)+CINT(qtp)+CINT(sql))\3

Assignments on Else If Statement

1. Write a script to print the greatest of three integers.
2. Write a script to print the least of the three integers.

Click Here for answers for Assignments


Previous11121314151617181920Next

VB Scripting - If Statement

Previous11121314151617181920Next


If Else statement in VB script is used when you want to execute a block of code based on certain condition.


Syntax for If Else Statement


If [Condition] Then
AAAACode
AAAACode
Else
AAAACode
AAAACode
End If


Example to show how If Else Statement is Used


We will write a script asking the user to enter the average scored and based conditions given appropriate message is displayed.

'Code to demonstrate If Else condition
Varint1 = Inputbox("Enter the average score you gained:")
If Varint1 > 80 Then
AAAAmsgbox "You scored " &Varint1& " percentage and are the winner"
Else
AAAAmsgbox "You scored " &Varint1& " percentage and do not qualify."
End If


If the user enters 85 then the output displayed will be

You Scored 85 percentage and are the winner


If user enters any thing less than 80 say 75 the output displayed will be

You Scored 85 percentage and do not qualify.


Previous11121314151617181920Next

VB Scripting - Branching Statements

Previous11121314151617181920Next

What is Branching and types of Branching Statements?


Branching Statements in VB Scripting provides a user with a choice to execute certain piece of code based on the condition. This would help us to validate different conditions for one expression.


In general to say that the program or code has to make a decision based on a condition when a user gives a input or based on some parameters or values the decision has to be made then branching statements plays a vital role.

For example take this scenario of giving different grades to a student based on the percentage. For one student you can assign the marks to different variables and based on the average you can branch out your code and give the grade but imagine if you have to run it for more that one student then manually you have to go into the code and change for every student and save and run.

Branching Statements helps you in making the code decide the grade based on the conditions you give in the code once and ask the user to enter the marks for each student. Using these branching statements you can branch out your code to different conditions.

Basically there are three types of Branching Statements

1. If Else
2. If Elseif
3. Switch Case


Out of these the popular branching or conditional statement used is the If Else, Endif

Else and Elseif are used to branch out different conditions.

Previous11121314151617181920Next