Codekids Lesson 5More While, If, and Rectangles |
In the last lesson, you learned about variables and the while(){} statement. But, maybe you didn't really get it. So here's another way to think about these. We're going to act out this 'computer program' in class:
This program uses three variables to control your jumping and twirling:
You keep doing the jumping and twirling again and again until the number of jumps is subtracted down to zero. the If statementThe if statement is really useful. You will use if many times if you do a lot of programming. It has these parts: if (variableName == someValue){
} When processing hits the if, it looks to see if the variable you put there is equal to the value you put there. If it is, processing goes inside the if statement's curly braces and does whatever you put there. If not, processing skips past the if statement. Here's an example: if (you == happy){
} You can also tell process what to do if the variable is NOT EQUAL to the value. You do this using the else statement: if (variableName == someValue){
}else {
} Here's another example: if (you == happy){
}else {
} = vs ==Remember what we learned last week? How do you say this:
GETS!!!!!!!!!! Why? Because there's similar thing in processing:
This is pronounced 'IS EQUAL TO.' Remember, = means GETS and "==" means IS EQUAL TO.
RectanglesUse the rect command to make squares and rectangles. It works just like the ellipse command. The following example will make a 300 pixel wide and 100 pixel tall rectangle with its upper-left corner starting 10 pixels over and 10 pixels down from the top of the window.
You can use the rectMode command to make the rectangles start from their centre instead of their upper-left corner:
Try these two different programs in processing to see the difference: Starting from the rectantle's upper-left corner:
Starting from the rectangle's centre:
Try it!Copy this code into Processing then run it:
Once you've got it running, hack it! If you create something cool, copy it out of Processing and email it to me! |
|
|
|
|