Alan here. When we talked last, I showed you how to make functions.

So far we’ve learned about variables, commands, and loops, as well as how make your own commands through functions.

This fills out most of the essentials for making a program, but we still haven’t discussed one of the basics. This is a “conditional statement.”

This is how you make a command to say, “do xx after ○○ happens.” It’s a command that you use quite frequently, but if you don’t have programming experience it’s a bit hard to think of ways to use it.

For instance, you often use it to make game characters move.

It’s something like “when the right key is pressed, the character moves right.”

The computer makes the character move to the right after “judging” the condition of the right key being pressed.

In other words, “if” is the very brains of the computer. One could very well say that how well you use “if” in a program determines your skill as a programmer.

(continued after the link)

Last time we discussed in our practice problems how to make a program that draws circles. It should look something like this:

function circle(){
for(i=0;i<360;i++){
move(1);
turn(1);
}
}

function onLoad(){
logo.init();
penDown();
for(j=0;j<4;j++){
circle();
turn(90);
}
penUp();
}

The end result should turn out like below. Here, we rotate the circle by 90 degrees, drawing it four times, so we end up with the following result.

Let’s use “if” to change this shape up a bit.

Let’s rewrite the circle function as the following.

function circle(){
for(i=0;i<360;i++){

if( i<30 ){
move(5);
}
move(1);
turn(1);
}
}

“if” appears in the middle. It’s resembles a loop a bit in that regard.

In the case of “if,” the contents of the parentheses following “if” are referred to as the “conditional expression.” If this is conditional expression is fulfilled, the contents of the parentheses are executed. Here, each time “i” is less than 30, move(5) is executed.

Let’s give it a go! Quite the odd shape, huh? Here we have 4 connected, so let’s rewrite the program so that only one is displayed. It’s as though one edge of the circle spins off into oblivion. Here you have proof that “move” is being frequently executed only when “i” is less than 30.

Let’s mix things up even more.

function circle(){
for(i=0;i<360;i++){

if( i == 180 ){
turn(90);
}else{
move(1);
turn(1);
}
}
}

[/pre]

Here we’re making a 90 degree turn only when “i” equals 180. Also note the command “else” that shows up after “if”’s {} brackets.

This “else” forms a set with “if,” meaning that if the conditions of “if” are not fulfilled, “else” will be fulfilled.

Here, when “i” is exactly equal to 180 it will turn 90 degrees. Otherwise, it will move as though drawing a regular circle.

Can you imagine what this will look like?

Here it is: If we repeat the loop four times, we end up with the flowerlike pattern below. As you can see, “if” can be used in many fun and unusual ways.

Congratulations! We’ve now gone over all the basics of programming.

Next time, let’s jump right into game programming! Stay tuned!