If This, Then That🔗

if-statement, branch, branching, decision, choice, else, otherwise, condition, conditional

Sometimes you may want to display a part of the program to certain users. You can set what some of your users will see using the *if keyword.

The *if keyword can work in conjunction with many other GuidedTrack elements and keywords, such as variables, points, and the *set keyword, as illustrated below.

*question: Do you prefer the mountains or the ocean?
		*set: mountainlover
		*set: beachbum

Want to know what I think?

*if: mountainlover
	Mountains are stupid.

*if: beachbum
	I love long walks on the beach!

In the program above, users will be asked their preference for the mountains or the ocean. By using the *set keyword, you can remember this information about the user, and use it to personalize the program for them later on. In this example, users will see one of two phrases, depending on whether their preference was set as "mountainlover" or "beachbum."

It's important to note where the tabs are used in the above example. If you want to remember which multiple choice answer a user selected, you must indent the *set keyword below their selection. Whenever you use the *if keyword, you always indent what the user will see next on the line below the *if keyword.

You can also use the *if keyword with math operations:

  • + addition
  • - subtraction
  • * multiplication
  • / division
  • () parentheses to set operator preference
  • > more than
  • < less than
  • >= more than or equal to
  • <= less than or equal to
  • = equals
  • ^ to the power of
  • and
  • or
  • not

Here's an example using the *save keyword, the *if keyword, and some math operations.

*question: What number am I thinking of, between 1-10?
	*save: number

*if: number = 5
	Great job!

*if: (number < 5) or (number > 5)
	You're wrong!

Here's another way you could write that last line, using "not":

*if: not number = 5
	You're wrong!

The above is essentially saying, "If the variable number is something other than 5, display 'You're wrong!'."