Saturday, December 18, 2010

IF/ELSE statements: 1

Here's an exercise on IF/ELSE, modified to pseudocode from one of the exercises in the textbook (http://visual-basic-dox.net/Prentice.Hall.PTR-An.Introduct/tindex.htm Chapter 5.2, exercise 1). It might not make sense to do If statements like this, but ignore than and just try to work out what would be displayed with different values.

What would be display with each of these numbers entered?

0
4
5
9
10

As usual, don't answer here, go to the "Pseudocode Help" forum (http://groups.google.com/group/pseudocodehelp) to answer.

Here's the pseudocode:


prompt user for a number
get number
if (number less than or equal to 9)
display ("Less than 10")
else
if (number equals 4) then
display ("Equal To Four")
end if
end if


Here is what it should code up as (NOTE: I'm guessing that is the correct code -I'm using an Apple laptop at the moment and it can't run Visual Studio)


Module Module1
Sub Main()
Console.WriteLine("Please Enter a number")
Dim num As Double = CDbl(Console.ReadLine)
If num <= 9 Then
               Console.WriteLine("Less than ten.")
       Else
              If num = 4 Then
                  Console.WriteLine("Equal to four.")
               End If
          End If
      End Sub
 End Module