• $10 Game Dev' Courses - Use code "BREAKTHRU"

    Video courses

Unity Pro Development A to Z – Build 10 Android/iOS Games

The Complete Android & Java Developer Course – Build 21 Apps

The Complete Java Developer Course. Learn Step by Step

In this quick mini-project we will put in to practice different types of loop like for loops and while loops. Prepare to go loopy. Actually it’s really simple.

  • Learning java by building Android games

    Learning Java by Building Android Games

    Want to learn Java for Android? Even if you are completely new to either Java, Android, or game programming but are aiming to publish Android games, then this book is for you. This book also acts as a refresher for those who already have experience in Java on other platforms or other object-oriented languages.
    • Setup your own Android game programming environment using Android Studio
    • Control logic, branch your code, and add real decision-making depth to your games
    • Design and use 2d sprite animations, smooth pixel graphics, sound FX, simple collision detection and artificial intelligence (AI)
    • Build around a dozen sample test apps and 4 complete working games!
    Amazon USA
    Amazon UK
    Amazon CA
    Our Store (US only)
    small_book

    Android Game Programming by Example

    Learn to build exciting Android game projects. Build a simple flappy-bird like game to a multi-environment, tough, retro platformer then an OpenGL ES 2 Asteroids clone, running at hundreds of frames per second .
    • Animate your characters with sprite sheets, add multiple scrolling parallax backgrounds, and implement genuinely tough, playable levels in your games
    • Every single line of code is printed in the book! No need to refer to the code files while trying to follow along. All the code files are also supplied separately so you can refer to them in their completed form and copy/paste them into your project if you like.
    • Implement a multitude of other game features such as pickups, firing weapons, HUD’s, generating and playing sound FX, scenery, level transition, high scores, and more
    Amazon USA
    Amazon UK
    Amazon CA
    Our Store (US Only)

To get started Create a new Android project and enter Loops in the Aplication Name field and enter LoopsActivity in the Activity Name field. When Android Studio has finished generating the project read on.

Delete the generated code in LoopsActivity.java then enter the required code that we need to work within as shown below and as explained in the mini-project Game variables demo.

import android.app.Activity;
import android.os.Bundle;

public class LoopsActivity extends Activity {

    // This is the entry point to our game
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Our Java code will start here

        // !!Our code must end here - for now.
    }
}

Now we can get on with doing a bit of looping. Enter the following code in the usual place as indicated by the starting and ending comments.

        // Our Java code will start here

        // First let's do a countdown with a while loop
        int x = 10;

        while(x > 0){

            x--;
            // x decreases by one each pass through the loop

            Log.d("Inside the loop:"," x = " + x);

        }

        Log.d("Outside the loop:","I'm free.");

        // Now let's use the break keyword
        x = 0;
        int tooBig = 10;

        while(true){

            x++; //I am going to get mighty big!
            Log.d("Inside the loop:"," x = " + x);

            if(x == tooBig){
                Log.d("Too big-"," Going to break out");
                break;
            } // No you're not haha.

            //code reaches here only until x = 10

        }

        Log.d("Outside the loop:"," x = " + x);

        // Now let's try a for loop
        for(int i = 10; i > 0; i--){

            // countdown
            Log.d("Inside the loop:"," i = " + i);

        }

        Log.d("Outside the loop:","Blast off!");

        // !!Our code must end here - for now.

Run the program and then we can take a look at the output in the Logcat window.

In the code above, first we count from 10 to 0 with a while loop:
while_loop_output

After this we attempt to keep counting forever with an infinite loop but break out when x = tooBig.
while_loop_with_break_output

Finally, in the for loop, we do another countdown but we also initialize i and set the condition of the loop all within the loop initialization line.
for_loop_output

Before we get dizzy from all this looping let’s look at a tutorial on Java methods.

Please visit the Android category of our game coding bookstore for beginners