JAVA programming basics...

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:

for (int x=0; x<10; x=x+1) { g.drawString("x = "+x,25,25+15*x); } How does this work? Let's look at in parts. First, the for statement.

Basic syntax for a for loop

for (//loop conditions) { //some action here; } The for statement is followed, *ALWAYS*, by a set of parentheses.

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

for(//loop) {action} Finally, notice that there is no semicolon after the last squiggly bracket. There almost never is, so again, remember that when you are debugging your code.

So, we know that x=0; hence, x<10, so let's move to the action statement, between the squiggly brackets.

{ g.drawString("x = "+x,25,25+15*x); } We know from our first Applets, that g.drawString is a way to draw a number onto the screen. So what does this do?

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:

//A program that uses a JAVA for loop //Importing neccessary packages import java.applet.Applet; import java.awt.Graphics; //The name of the public class is the name of the program public class Loop extends Applet { public void paint (Graphics g) { for (int x=0; x<10; x=x+1) { g.drawString("x = "+x,25,25+15*x); } } } and referring to it from: <HTML> <HEAD><TITLE>Let's Try the for loop</TITLE></HEAD> <BODY> <H1>Let's Try the for loop</H1> Can you see this in the appletviewer?<P> <APPLET CODE="Loop" WIDTH=200 HEIGHT=200> </APPLET> <P> Did you enjoy looping?<P> </BODY> </HTML> Let's look at a couple of variations, okay?
for (x=0; x<100; x=x+10) { //action statement } This is fairly simple. It just means that we increase by 10 each time, so we count by tens rather than ones. This will count faster; think of it as counting $100 with a hundred ones, and then counting to $100 with ten tens. The tens, obviously, is much faster.
for (x=100; x>0; x=x-1) { //action statement } This also should be fairly easy to grasp. We are starting at 100, and counting down to zero by ones.
for (x=2; x<40000; x=x*2) { //action statement } We start at 2, and multiply by 2 until we reach > 40000. Starting to get the hang of it?

for (x=0; x<10; x++) { //action statement; } What's this x++ stuff all about? Well, the ++ operator says to take the current value of the variable, and increase it by one. This means that if x=0, and you say x++, x will then equal 1. Ex:
int x=0; x++; x will now be equal to 1. int z=20; z++; z will now be equal to 21. Well what about x--? Will that work? The answer is yes, it will work.
int x=54; x--; x will now be equal to 53. int sp=12; sp--; sp will now be equal to 11. Important note here, is that x** and x// will *NOT* work. So don't try. J