Lesson goal: The if-statement

Previous: Compound true/false questions | Home | Next: Divisibility tester (1)

In the two lessons (here and here) you learned how to generate true or false answers based on the comparison between numbers. Displaying true or false values to the screen, as in those lessons is just the beginning.

The real power of those answers is when you have the computer make decisions based on them. This is what the if statement is all about: making decisions as to what code to execute, based on the answer to a true/false question. In other words, you can tell the computer to do something only if some condition is true.

The if statement looks like this:
if true/false-question then
BODY

end
Move the mouse over a dotted box for more information.

The default code here (reload page if needed) is a simple math-problem tester. It'll generate a couple of random numbers into variables a and b, and make a math problem out of it. The user is prompted to type in the correct answer. An if statement is used to see if the user has typed the correct answer.

Now you try. Write an if statement that will display **Good job** if the user types the correct answer to the math problem.

Type your code here:


See your results here: