ISP 121 Activity 9
Introduction to Computer Programming
Writing Programs with conditionals


The following apply to Visual Basic:

Relational Operators

= Equals (note there are two equal signs)
<> Not equals (note only one equal sign)
< Less than
<= Less than or equal than
> Greater than
<= Greater than or equal than

Logical  Operators

Not Negation (Not)
And And
Or Or

If-Then-Else syntax 

If    condition  Then

       Code that runs if the condition is true goes here

Else

       Code that runs if the condition is true goes here

End If

 

In each case, write a computer program that accomplishes the task.  Test your code! Then paste your code into your Word document.

1.  Write a computer program that checks to see if an input number is less than 0 or not.  If it is less than zero, it gives a message "The number is negative."  If it is greater than or equal to zero, it gives the message "The number is nonnegative."  Hint.  The key step of the code will be of the form  

If  TextBox1.Text >= 0 Then
         Need to write code here
Else
         Need to write code here
End If

                    

2. (Weight on other planets) Write a computer program that takes an input weight (>0) and a planet name (also the Moon and Pluto) and outputs your weight on that planet.  You need to multiply the weight by the factors below for each.

Mercury 0.4155
Venus 0.8975
Earth 1.0
Moon 0.166
Mars 0.3507
Jupiter 2.5374
Saturn 1.0677
Uranus 0.8947
Neptune 1.1794
Pluto 0.0899

Hint.  Your program needs two input boxes.  You need to write a lot of if-statements, but that is ok because you know how to copy and paste.  The condition you need to check is something like

If     TextBox2.Text = "Mercury" Then 
         calculate the correct weight for Mercury and display it
End If


3. (Dance competition) Write a program that takes three numbers in the range of 0 to 10 (the three judges' scores) outputs the average of the three scores.  If any of the three numbers is not in the range 0 to 10, the program returns a polite error message. 

Hint.   The key part of this program is getting the logic with the error messages right.  The code for calculating the average is something like:

Label1.Text = (CDbl(TextBox1.Text) + CDbl(TextBox2.Text) + CDbl(TextBox3.Text)) / 3.0

The CDbl function converts the text object into a number so that you can do calculations with it.