# 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

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.

This code will not run as-is. Figure out what "true/false-question" you would put between the if and then words to check if ans (the number typed at the keyboard) is equal to the answer of a+b.

Note: the tonumber( ) function wrapped around the input() function is needed to convert what is typed at the keyboard into a numeric form, so it'll work with the comparison operator ==.

If you figure this, can you then figure out how to add another if statement using the ~= ($\ne$) operator to display **wrong** if the user types the wrong answer? Dismiss.

Show a friend, family member, or teacher what you've done!