Tutorial 3: If Statements

Onward and upwards! This tutorial will cover another of the most important concepts in programming: If statements.

This one actually doesn't need much explaining... We use If Then Statements all the time in normal life!

Maybe this sounds familiar to you...

"If you do all of your homework, then you can have some extra time playing on your Nintendo Switch!"

Or perhaps...

"If you eat all of your vegetables, then you can have extra chocolate cake, or else you can go to bed with no dinner!"

Hopefully those sentences make some sense! An If statement checks if something is happening, and then does something. If it's anything else, we do something else!

Okay, enough rambling! Let's write a small quiz program using what we've learned so far.

  1. print( "Welcome to the FUZE ultimate quiz of supreme difficulty \n" )
  2. update()
  3. sleep( 2 )
  4. print( "Question 1. What is the capital of Japan? \n" )
  5. update()
  6. sleep( 2 )
  7. answer = input( "What is the capital of Japan? \n" )
  8. if answer == "Tokyo" then
  9.     print( "CORRECT! Well done! \n" )
 10. else
 11.     print( "INCORRECT! I can't believe you didn't know that... \n" )
 12. endif
 13. update()
 14. sleep( 3 )

We've got a new function here, take a look at line 7.

input()

On line 7 we use the input() function to allow the player to type an answer to our question. The input() function will bring up the FUZE4 Nintendo Switch keyboard on screen to type in whatever you like. Whatever the player types is stored in a variable. We have called our variable answer.

Something we need when using the input() function is to give the player a prompt message. We have put a reminder of the question: {code:"What is the capital of Japan?"}, but you can use whatever you like! If you would like to display no prompt simply put empty speech marks in the brackets.

If Then Statement

Lines 8 to 12 are our If Then Statement. It begins with if to state the condition. In the example, we check if the contents of the variable called answer is exactly equal to the text string "Tokyo". If it is, then we print "Correct! Well done!".

We use the keyword else as part of the if statement to give a different result if the condition is not met. In this case, we know that if our condition is not met this means the answer is incorrect, so we can tell the player.

Lastly, we must use endif to finish the if statement, otherwise we will run into some problems later! Without endif the computer will treat any lines that may follow as part of the if statement.

Single Equals (=) and Double Equals (==)

Notice the double equals sign on line 8? When we are comparing two things, we must use double equals (==). When we are assigning a value to a variable we use single equals (=)

Keeping Score

Okay... Let's step this up. We can use a variable to keep track of the player score too and really turn this into a playable game.

  1. score = 0
  2. print( "Welcome to the FUZE ultimate quiz of supreme difficulty \n" )
  3. update()
  4. sleep( 1 )
  5. print( "Question 1. What is the capital of Japan? \n" )
  6. update()
  7. sleep( 2 )
  8. answer = input( "What is the capital of Japan?" )
  9. if answer == "Tokyo" then
 10.     print( "CORRECT! Well done! \n" )
 11.     score += 1
 12. else
 13.     print( "INCORRECT! I can't believe you didn't know that... \n" )
 14. endif
 15. update()
 16. sleep( 2 )
 17. print( "You scored... ",score, "\n" )
 18. update()
 19. sleep( 1 )
 20. if score == 1 then
 21.     print( "Congratulations! Full Marks" )
 22. else
 23.     print( "Better luck next time." )
 24. endif
 25. update()
 26. sleep( 3 )

In the example above we have added a score variable to line 1. It begins at 0 because our player hasn't answered any questions yet!

Line 11 is a new line which increases the contents of the score variable by 1 if the player answers correctly. Remember, we use += to do this.

Next, lines 20 to 24 are a new if statement to give the player a message depending on their score.

Using what we we have learned, can you write 2 more questions for the quiz? You should place your new questions between line 16 and line 17, because lines 17 to 26 finish the quiz and tell the player their score.

You will need to change line 20 if score == 1 then to be equal to your maximum total score. For example, if your quiz contains 3 questions and you score 1 point for each correct answer, the line should read if score == 3 then

Perhaps use the ink() function to bring your quiz into full colour!

Functions and Keywords used in this tutorial

else, endIf, if, input(), print(), sleep(), then, update()