[Android Software Development] tjdwowh:
Sorry about the long wait! My computer broke down, and I just managed to get the power supply replaced. :)

But on a more positive note:

I think after 3 more days, we can begin actual game development.

Of course there's a lot more to learn, but I think it would be more fun and more efficient to learn some of the tougher concepts by application.

Without further ado, let's jump into our next lesson!

Lesson #1-16: Analyzing the Looping Class

In the last lesson, I created a class called Looping as follows:

[COLOR="Blue"]public class Looping {
[/COLOR]	[COLOR="RoyalBlue"]public static void main(String[] args){[/COLOR]

int initialValue = 0;
int finalValue = 10;

int counter = 0;

[COLOR="DarkRed"]	if (initialValue < finalValue) {[/COLOR]
System.out.println("Input accepted!");
System.out.println("Initial Value: " + initialValue);
System.out.println("Final Value: " + finalValue);
System.out.println();
System.out.println("Initiating count.");
System.out.println();

System.out.println(initialValue);
counter++;

[COLOR="DarkOrange"]while (initialValue < finalValue) {[/COLOR]
initialValue++;
System.out.println(initialValue);
counter++;

[COLOR="DarkOrange"]}[/COLOR]

[COLOR="Olive"]if (initialValue == finalValue){[/COLOR]
System.out.println();
System.out.println("Counting complete.");
System.out.println("There are " + counter + " numbers (inclusive) between " + (initialValue - counter + 1) + " and " + finalValue + ".");
[COLOR="Olive"]	}[/COLOR]

[COLOR="DarkRed"]	} else {[/COLOR]
[COLOR="SeaGreen"] //Executed if: if (initialValue < finalValue) above is not true.[/COLOR]

System.out.println("Final value is less than initial value!");
System.out.println("Please choose new values.");
[COLOR="Red"]}
[/COLOR]
[COLOR="RoyalBlue"]}[/COLOR]
[COLOR="Blue"]}
[/COLOR]

The Output:

Input accepted!
Initial Value: 0
Final Value: 10

Initiating count.

0
1
2
3
4
5
6
7
8
9
10

Counting complete.
There are 11 numbers (inclusive) between 0 and 10.

It might have been a while since we discussed some of these concepts, so please refer to previous lessons if you are confused!

And of course, you can pm me or email me questions as always.

Now to analyze:

1. The blue text begins the Looping class. (I hope you know this by now :D ).

2. The light blue text then begins the main method (you should know what the main method is also).

3. The next three statements beginning "int..." initialize three integers: initialValue, finalValue, and counter.

4. The point of this program is to count from a lower number to a higher number, so the first condition it tests for, using an if statement is whether initialValue < finalValue.

5. If this condition is satisfied, we see a bunch of System.out.println("...")

6. The counter variable is increased by 1.

7. The while loop then initiates, and this increases initialValue by 1, and displays the new initialValue.

8. Counter variable is again increased by 1.

9. With each iteration (repetition) of the while loop, we check if the initialValue (which increases by 1 with each iteration) is equal to finalValue, because that is when we can stop counting.

10. When this if condition (colored gold) is satisfied, we stop counting, and display the counter variable, which has been increasing by 1 with each increase in value of the initialValue variable.

Make sure you run this code a few times with your own numbers to understand what exactly is happening at each step.

If you want to make your own programs, you should be able to analyze and at least have a general sense of what other programmers are trying to accomplish with their code.

Lesson #1-17: The For Loop

The while loop utilized a counter that we coded ourselves. Now we will implement the for loop which comes with a built-in counter, so to speak. :)

The form of a for loop is as follows (as stated in Java docs):

for (initialization; termination;
increment) {
statement(s)
}

"When using this version of the for statement, keep in mind that:

1. The initialization expression initializes the loop; it's executed once, as the loop begins.

2. When the termination expression evaluates to false, the loop terminates.

3. The increment expression is invoked after each iteration through the loop; it is perfectly acceptable for this expression to increment or decrement a value."

Confused?

Let's look at an example:

[COLOR="Blue"]class ForLoop {
[/COLOR] [COLOR="RoyalBlue"]public static void main(String[] args){[/COLOR]
[COLOR="Red"]for(int variable = 1; variable <11; variable++){[/COLOR]
System.out.println("Count is: "
+ variable;
[COLOR="Red"]}[/COLOR]
[COLOR="RoyalBlue"]}[/COLOR]
[COLOR="Blue"]}[/COLOR]

The output would be:

Count is: 1
Count is: 2
Count is: 3
Count is: 4
Count is: 5
Count is: 6
Count is: 7
Count is: 8
Count is: 9
Count is: 10

The for loop is quite simple:

The first part of the for (...) is the initialization of a variable.

We initiate an integer called variable, with the value of 1.

The second part "variable <11" is the condition. Think of this as: while (variable < 11)...

Everything inside the for loop will run as long as this second part is satisfied.

The third part "variable++" is what happens to the initialized variable after each iteration. We increase it by one.

Lesson #1-18: Applying the For Loop

In this lesson, we will recreate the Looping program using not the while loop, but the for loop!

Have a look at the while loop example again:

[COLOR="Blue"]public class Looping {
[/COLOR]	[COLOR="RoyalBlue"]public static void main(String[] args){[/COLOR]

int initialValue = 0;
int finalValue = 10;

int counter = 0;

[COLOR="DarkRed"]	if (initialValue < finalValue) {[/COLOR]
System.out.println("Input accepted!");
System.out.println("Initial Value: " + initialValue);
System.out.println("Final Value: " + finalValue);
System.out.println();
System.out.println("Initiating count.");
System.out.println();

System.out.println(initialValue);
counter++;

[COLOR="DarkOrange"]while (initialValue < finalValue) {[/COLOR]
initialValue++;
System.out.println(initialValue);
counter++;

[COLOR="DarkOrange"]}[/COLOR]

[COLOR="Olive"]if (initialValue == finalValue){[/COLOR]
System.out.println();
System.out.println("Counting complete.");
System.out.println("There are " + counter + " numbers (inclusive) between " + (initialValue - counter + 1) + " and " + finalValue + ".");
[COLOR="Olive"]	}[/COLOR]

[COLOR="DarkRed"]	} else {[/COLOR]
[COLOR="SeaGreen"] //Executed if: if (initialValue < finalValue) above is not true.[/COLOR]

System.out.println("Final value is less than initial value!");
System.out.println("Please choose new values.");
[COLOR="Red"]}
[/COLOR]
[COLOR="RoyalBlue"]}[/COLOR]
[COLOR="Blue"]}
[/COLOR]

We can safely remove all the counter variable related statements, as the for loop will handle that for us.

The only other segment of code we need to modify is the while loop section.

We will replace this with the for loop :

[COLOR="Blue"]public class ForLooping {
[/COLOR]	[COLOR="RoyalBlue"]public static void main(String[] args) {[/COLOR]

int initialValue = 0;
int finalValue = 10;

[COLOR="DarkRed"]if (initialValue < finalValue) {[/COLOR]
System.out.println("Input accepted!");
System.out.println("Initial Value: " + initialValue);
System.out.println("Final Value: " + finalValue);
System.out.println();
System.out.println("Initiating count.");
System.out.println();

System.out.println(initialValue);

[COLOR="Green"]	for (initialValue = initialValue + 1; initialValue <= finalValue; initialValue++){
[/COLOR]	System.out.println(initialValue);

[COLOR="Green"]}[/COLOR]

[COLOR="DarkRed"]} [/COLOR][COLOR="Red"]else {[/COLOR]
System.out.println("Final value is less than initial value!");
System.out.println("Please choose new values.");
[COLOR="Red"]}[/COLOR]

[COLOR="RoyalBlue"]}[/COLOR]
[COLOR="Blue"]}[/COLOR]

Remember to change the name of the class (Select it on the left side of the screen in the Package Explorer, use F2) before running the code!

The output:

Input accepted!
Initial Value: 0
Final Value: 10

Initiating count.

0
1
2
3
4
5
6
7
8
9
10

Using the for loop significantly reduced the size of the code, and it is much more efficient!

=) I don't know what we will be learning tomorrow, but I'll try to make the most of the 2 more days!

[Day 10] Final Touches Part I

[Day 11] Final Touches Part II

[Day 12] Beginning Java Game Development