Freigeben über


Small Basic Example - Evaluating Math Functions

Evaluating Math Functions

In this example, you’ll use nested if conditions to solve a math problem. You’ll write a program that evaluates y=f(x), for a given input, x:

  
The complete program’s in Listing 7-11, along with the output from four sample runs.

Code

' Calculated y as a function of x

TextWindow.Write("Enter x: ")

x = TextWindow.ReadNumber()

y = 0

If (x > 0) Then

  If (x <= 10) Then     ' 0 < x <= 10

    y = x

  Else                  ' x > 10

    If (x <= 20) Then   ' 10 < x <= 20

      y = 20 - x

    EndIf

  EndIf

EndIf

TextWindow.WriteLine("y(" + x + ") = " + y)

 

Output:

Enter x: -5

y(-5) = 0

 

Enter x: 5

y(5) = 5

 

Enter x: 15

y(15) = 5

 

Enter x: 25

y(25) = 0

 

The comments in the program will help you understand each step.

Trace through the program using a few different values for x. (Plug a number in for x and then work through each step on pencil and paper. Then enter the number as a user of your program to check your answer.)

 

Have a Small and Basic week!

   - Ninja Ed & Majed Marji

Comments