Learning Petit Lesson 2: Program Flow

In the previous lesson we covered how to display messages, get input from the user, and store values in variables. While this seems like all you would need to make a text-based game, you may have realized very quickly that there's not very much you can do with a program that just carries out some steps in order and then ends. In this lesson, we will learn how to have a program respond to conditions and make decisions, and methods to repeat or skip code.

IF statements and decision-making
Almost every program will need to make decisions or respond in a certain way when a certain condition is true. The core decision making tool in BASIC is the IF statement, which is written like a mixture of English and algebra:

This code should be easy to understand - the program checks if the player has less than one health, and prints that the player died if so. IF allows our program to respond according to certain conditions, so an IF statement is known as a conditional statement.

IF syntax
Basic IF statements come in the form

The different symbols we can use to compare values are called Relational Operators. Notice that equality is tested with a double equals == to distinguish it from assigning a value like X = 5 + 5. Also note that you can add a comment to a line by adding an apostrophe; anything after it is just a note and Petit ignores it. In these lessons I will often use comments to clarify examples. Also note ! is shorthand for "not".

When a condition is met, the condition is said to be true, and the code after THEN will be executed. Otherwise the code after THEN is ignored. This is a good time to introduce two new types of values: TRUE and FALSE, which are called booleans (see notes). We can assign TRUE or FALSE to a variable like a number or string value:

That example is unnecessarily complicated but it demonstrates how TRUE can be used. There is so much to IF that it's worthy of it's own article, which you should probably give a read after you're finished with this one.

Let's look at example of IF with user input:

We're beginning to see how IF is the keystone of interactivity. We'll get back to IF in a bit, but let's examine some new commands first.

Branching with GOTO and GOSUB
So far we've only seen code that is followed line by line until the end of the program. In many cases we need more flexibility than that.

GOTO
GOTO is best explained by example:

GOTO is called a jump command because it allows us to jump directly to another place in code. In Petit, GOTO jumps to a label, which starts with @ and can have any name you want. In the above example, if the player has no health left, "You survived!" is never printed because the code jumps to the death scene. Note that I have also introduced the END command, which simply ends the program immediately.

In most cases, GOTO is used with an IF statement, but GOTO can have other uses:

This is a way to implement a loop - a section of code that is repeated multiple times. This particular example would run forever (until you pressed SELECT to quit the program), but there are many other ways to use loops.

GOTO lets you jump somewhere else in the code, but doesn't provide any way to get back to where you came from. You could go back with another GOTO, but that becomes messy very quickly. Let's examine a related command, GOSUB.

GOSUB and RETURN
To be added

FOR loops
To be added

Back to IF
Sometimes you want an IF statement to do several things. Unfortunately, Petit's worst feature is that it doesn't let you write a multiline IF statement (see notes). This approach is messy: This is technically referred to as very bad code. To prevent you from doing that, Petit lets you combine multiple instructions per line, like this:



This is technically referred to as very bad code, primarily because it's 3 times the length of the DS screen. Do any one of these:



or

The GOSUB method should probably be considered better than the GOTO method.

While I could easily go on for many more pages about loops and IF statements, I think this is a good place to wrap up and let you digest what you've learned. In the next lesson, we will cover some more advanced features of the console and strings.

Recommended exercises

 * 99 Bottles of Beer - make a program that prints out all the lyrics to 99 Bottles of Beer. Try it with a FOR loop and then with GOTOs and IFs!