This document was written by CS 290W TA Joshua Kay and was last modified
Alright, we need to learn some basic programming skills, now. How do programs do what they do?First, let's think about what they do. Example - a simple program that counts to 10.
How do we program a computer to count? It's a simple question, right? Yes.
We will start with the for statement. Example syntax:
Basic syntax for a for loop
These parentheses contain the Loop conditions. Where do we start counting? How far do we count? What do we count by?
After the for statement, notice the {} squiggly brackets. These brackets surround the action statement(s). What do we do each time through the loop? Note that these squiggly brackets are *ALMOST* always needed, and for the intent of this course, we ask that you *ALWAYS* use them.
Next, let's look at the action statement. This is the classic example of what a for loop is used for. Again, let's divide this into three parts.
int x=0; - This is declaring a variable x of type int (or integer) to be equal to zero. Simple enough, right? For loops almost always use integers, as they count, and for this class we won't use anything other than integer values.
x<10; - This checks to see if x<10. Well, we just declared x=0, right? So, it moves to the next statement.
x=x+1; - this takes the current value of x, and adds 1 to it. So far, so good.
Note that int x=0; and x<10; both end with semicolons (;). This is like most Java statements - you add the semicolon to know when the statement is over, and to begin the next statement.
Note also that the x=x+1 is *NOT* followed by a semicolon. This is just the way that for statements are written. If you add a semicolon after the third statement your program will *NOT* compile correctly.
This is a common mistake among beginning programmers. It would be a good idea to make a note of this, it might come in handy when you start writing Java programs on your own.
Also, notice that the for statement itself does not have a
semicolon after it. This, too, is the way that you need to write the
code. If you need a reason why, well, the for could be thought
of as a built-in method of Java code, so the syntax is like a
method
So, we know that x=0; hence, x<10, so let's move to the action
statement, between the squiggly brackets.
This will draw onto the Applet x = 0 (@ location x=25, y=25.)
Now, what does the for loop do? Well, it gets to the ending squiggly bracket, and it moves back to the for loop. x started at 0, right? Well, now, x=x+1, so x=1. x is still less than 10, so it executes the Action statement inside of the squiggly brackets again.
Now, it will draw onto the Applet x = 1 (@ location x=25, y=40.)
After it does this, it will go back to the for loop again, and add x=x+1 - so now x will be 2, etc.
This will continue until x equals 10. Once this happens, when the for loop checks if x<10, it will find out that it is not, skip to the end of the loop, and carry on with the program.
You can try this yourself by creating the applet Loop:
Did you enjoy looping?