A Programming Language Manual

6.1 If instruction

All the programs we wrote so far have a liniar flow of execution. Meaning that the processor was executing all instruction, one by one, in the order of their appearance. But there are places when we have to make decisions and choose to execute only some instructions, by the case. Here is an example:

Run it, and as you probably guess, it will display that a is less than b. But what happened?
We compared the two variables and we used the if else instruction to make a decision.

If the result of the comparation is true (non zero), then the first println instruction is executed and the second one, skipped.

If the result is false (zero), then the second println instruction is executed and the first one skipped.

Since we set a to 1 and b to 2 at the beginning of the program and because 1 is less then 2, the processor will see the condition as true and will choose to execute the first println.

After the choice was made and the selected instruction was executed, the program will continue with the rest of the instructions, in this case a final println, "done."

Here is a picture of the execution flow:

...and now you see where the branching name comes from.

6.2 One or two brances

There are two forms of the branching instructions. Single branching, with a single instruction block, where if the expression is true, the block gets executed and if not, it gets skipped.

if( expression )

And double branching, where the first block gets executed if the expression is true and the second block, if not.

if( expression )

  If we need to execute more than one instruction on a branch, we must use braces { }, to mark the block of instructions.

The indentation of the instuctions on the branches is only recomended for easy reading of the program. So, we can also write:

6.3 Deeper and deeper

Since the branching instuction is an instruction itself, it can be used inside of the block of instruction of another branch. And so our tree of decisions grows...

Take very good care on how you group your multiple brachnes to avoid unexpected selections. Using correctly the indentations, it will help you with that. Here is a bad example:

6.4 Switch instruction

The switch instruction is a substitute for a list of more conditions. The case keyword is used to enumerate each value for comparation. The optional default keyword placed last is used for other values then those enumerated. Each comparing can be stopped in any case by an optional break instruction.

6.5 Goto instruction

You can also control the flow of your programs, by forceing the processor to jump to a specified instruction. This is done with the goto instruction. It can jump to a specified label name forward or backward in the program instructions list.

The goto instructions might become very dangerous if not used correctly. As you can imagine, incorrect jumping might result in infinite cycles causing the program to never end. They can also make a program hard to read. Because of that, programmers prefer to avoid extensive use of goto instructions. So they are not recomended for beginners.

6.6 Latent stop

The stop instruction stops the execution of the program from anywhere it is used. But it does much more. It asks the processor to remember exactly where it was stopped so it can continue later, from the next instruction. This procedure is called latent stop.

Unfortunately, our console application has no use for this technique, but in a game engine, like DizzyAGE, it is very usefull for implementing non-blocking functions. Imagine that a game must display a message and wait for the user to read it, so it can display another. If a "wait for key" function would be used, the whole game will freeze until the user, presses a key. For a game that means no animations, no sound effects and for a Windows application that means "hanging". To avoid all these, the game engine stops the program, does all those updates and then, returns to test for the key. If no key is pressed yet, it stops again and do another update.

It's like stopping to drink a little water, while you're speaking to somebody.

Now, because you can finaly control the flow of your programs, you might think you can make any program you want. And that is not far from the truth, because all the other instructions to come are based on those basic ones.

Before we go any further, let's have a look at a short program.

Next, we'll see a few nice instructions that might save us from repeating ourselves :)