
First and foremost, and I can’t stress this enough, I am not a developer. I want to be though and that’s what counts. If any of the information provided in this 12 week series is incorrect or can be made simpler, please let me know.
This section deals with part (4) Introduction to Java. To keep up with the posts follow @AndroidSPIN and @openandroidmove on twitter for when a new series is up for you to view and learn from. You can also find the whole series HERE in our forums that will be updated with each step written.
Introduction to Android and Java.
(1) Installing Android – Completed
(2) Creating Hello World – Completed
(3) Running on Emulator - Completed
(4) Introduction to Java, Variables, Data types, Loops, Conditionals and Operators
In the final segment this week, we are going to be looking at how to create simple loops in your application. Loops simply repeat an action until the required operation meets what you have required.
Here’s our code:
package learning;
public class learning {
/**
* @param args
*/
public static void main(String[] args) {
int a =0;
while(a<5){
System.out.println(“In Loop”);
a++;
}
}
}

When you execute this program the OUTPUT window will display out “In Loop” text five times. Why? Because we used while. What this code while(a<5){ string is telling Java is while ‘a’ is less than five to run the program again. Now if we simply left it at that, the program would be in a loop forever because ‘a’ would forever be zero until you changed it manually. This is where a++; line comes in. With this command we are telling Java to add one to a every time it cycles through the loop. When it loops through the first time 0 is less than five so it cycles again adding one. One is less than 5 so it cycles again and adds one… and so on and so forth until it have finally reach five which then satisfies the statement and the program stops. Which is why inside your OUTPUT window you will see a total of five in loop statements. 0,1,2,3,4.
The second way you can create a simple loop is through a for statement.
package learning;
public class learning {
/**
* @param args
*/
public static void main(String[] args) {
int i =0;
for(int a=0;a<5;a++){
System.out.println(“In Loop”);
}
}
}

The loop is created in both instances and ends with the same results: “In Loop” being printed five times.
From what I understand thus far, you can write the loop however you wish and feel comfortable. Something else I have been discovering along this journey is that there are many, many, many different ways to write out a code or command that will end up doing the same thing.
Now let’s take this principal and apply it to something useful. We are going to build a simple quick program to display the even numbers from 0 through 100. Here’s our code:
package learning;
public class learning {
/**
* @param args
*/
public static void main(String[] args) {
int i =0;
for(int a=0;a<102;a=a+2){
System.out.println(a);
}
}
}

In this line for(int a=0;a<102;a=a+2){we kept a =0 then we told Java that if a>102 to keep cycling and we told the program that a=a+2. So every time the cycle is performed, two will be added to ‘a’ until it reaches 102. That will simply keep printing “In Loop” on your OUTPUT window, so we changed our statement to System.out.println(a);since there are no “” around ‘a,’ the system will print whatever ‘a’ equals each time.
Go ahead and get familiar with this. Make your own adjustments. The more you test and change, the easier it is to remember. Here are a couple example lines I changed.
for(int a=0;a<105;a=a+5)
for(int a=0;a<2000;a=a+15)
for(int a=20;a<2000;a=a+11)
for(int a=1;a<100;a=a+2)
Experiment with it and get the hang of using loops.
Next week we will be moving forward to:
Week 2 – Android Architecture and OOPS
(1) Building Blocks of Android
(2) Java Classes and Objects
(3) Class Methods and Instances
(4) Inheritance and Polymorphism in Java
(5) Interface and Abstract class
Be prepared to really get into actually working with Android and making programs. I am also going to include a list of links in the forums for you to look through at your leisure that cover a little bit of information that we didn’t get to here. Although some of it is important information, I am a visual learner and until we or I actually use it and can see what it does I skip though it.
Happy coding everyone. I’ll see you in the middle of next week again with some more lessons.

![[Review] Wireless Game Controller by Gamestop](http://androidspin.com/wp-content/uploads/2013/04/883386b1-150x150.jpg)

![[Game Review] Daddy Was A Thief](http://androidspin.com/wp-content/uploads/2013/05/dad-was-a-thief-150x150.jpg)

