# 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: ")

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

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

Tags

1. Computers Today (part 1 of 6)

blogs.msdn.com/…/computers-today.aspx

…..

CS SPOTLIGHT: Girls in computer programming… why it matters!!!

blogs.msdn.com/…/cs-spotlight-girls-in-computer-programming-why-it-matters.aspx

Computational Thinking – Videos & Papers by Jeannette Wing

blogs.msdn.com/…/computational-thinking-videos-amp-papers-by-jeannette-wing.aspx