Dec 19, 2007

Our Methods are Getting Loopy

Hello, the post may be a little messed up, blogger really doesn't like < pre >!

Today we are going to learn "loops". Loops are a way of repeating code over and over and over and so on... There are a couple types of loops in JAVA, "while", "do-while" and "for" loops. We will do "while" and the basic "for".

The while loop:
- while some condition is true the loop will run.

while (some condition) {
//code to run
}

An example of this:

while (x > 0) {
//code to run while x > 0
}

The for loop:
- this type of loop is for some code that you know needs to run a certain amount of times. These are basically a while loop with a sort of counter built in.

for (some variable; condition; increment/decrement) {
//code to run
}

An example of this:

for (int x = 0; x < 5 ; x = x + 1) {
//we declare a variable x and set it to zero
//more on declaring variables later
//this will run when x < 5
//x will go up by one each time the loop runs through
}

A couple of things we have to go over here:
1) The "//" indicates a comment. A comment is a piece of writing that java ignores. It just skips merrily along right over it. "//" is used for 1 line comments.

2) "/* your comments */" is for larger comments. All writing in between the /* */ is ignored.

3) Operators... the <, >, != and == are your basic operators. THERE ARE 2 EQUALS SIGNS THERE! This is on purpose, when comparing two values you use TWO EQUALS SIGNS! When setting something equal to another thing you use one equals sign. The != means not equal to.

4) You can also combine these operators, <= and >=, less than or equal to and greater than or equal to. For a complete list go here.

So now let's make a small program with some of the things we know. We'll make it display a message a certain amount of times.


public class Display {
public static void main(String[] args) {
for (int x = 0; x < 5; x = x + 1){
System.out.println("Counting: " + x);
}
}
}

The output to the command line for this will be:
Counting: 0
Counting: 1
Counting: 2
Counting: 3
Counting: 4


The x is incrementing so you will see it go up by one each time starting from 0. Notice the x = x + 1 only runs once the loop has gone through once and not immediatly.

The line:
System.out.println("Counting: " + x);
Notice that this is not:
System.out.print("Counting: " + x);

.print prints continuously on one line.
.println print to a new line each time.

Also in that line we have "Five times" + x
The "+" in this acts to combine the two for output. It concatenates it, joins them together.

The while loop itself is a bit simpler:

public class Display2{
public static void main(String[] args) {
int x = 0;
while (x < 5) {
x = x + 1;
System.out.println(x);
}
}
}


This will give us an output of:
1
2
3
4
5

This is enough to soak in for one day. We'll start making some simple programs soon so we can get this deep in our heads.

2 comments:

Mal said...

Fantastic post on looping, you're right on the money. You're right about the "=+ meaning setting one thing equal to another but I would use the word "assigning" to describe the action of that operator. I realize I'm just being picky but I think that's what you wanted :). Otherwise, very impressive.

Mr. Kuropatwa said...

I'm really loving this.

You write very clearly and make the code really easy to understand. Outstanding use of colour. I'm particularly enjoying how you anticipate the questions/problems I might have and answer them.

Keep the posts coming ... I'm your biggest fan. ;-)