In this quick mini-project, we will put into practice different types of loops like for loops and while loops. Prepare to go loopy. Actually, it’s really simple. 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
// 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:
After this we attempt to keep counting forever with an infinite loop but break out when
x = tooBig.
Finally, in the
for loop, we do another countdown but we also initialize
i and set the condition of the loop within the loop initialization line.
There is also a third type of loop in Java. The do-while loop.
A do-while loop is a control structure in Java that allows you to execute a block of code repeatedly until a certain condition is met. The main difference between a do-while loop and other types of loops, such as the for and while loops, is that the code inside the do-while loop will always be executed at least once, even if the condition is false.
The basic syntax of a do-while loop in Java is as follows:
1 2 3 |
do { // code to be executed } while(condition); |
In the above code, the keyword do starts the loop and the code to be executed is enclosed within the curly braces. After the code block, the keyword while is used followed by a condition in parentheses. This condition is evaluated after each execution of the loop. If the condition is true, the loop continues to execute. If the condition is false, the loop stops executing.
Here’s an example of a do-while loop in Java:
1 2 3 4 5 6 |
int i = 1; do { System.out.println("The value of i is: " + i); i++; } while (i <= 5); |
In the above example, we declare a variable i and initialize it to 1. The code inside the loop will print out the value of i and then increment it by 1. The loop will continue to execute as long as i is less than or equal to 5. Since the condition is true initially, the loop will execute at least once and will continue to execute until the value of “i” becomes 6.
It’s important to note that a do-while loop will always execute at least once, even if the condition is false. This can be useful in situations where you need to execute a block of code at least once, regardless of whether the condition is initially true or false.
In summary, a do-while loop in Java is a control structure that allows you to execute a block of code repeatedly until a certain condition is met. The code inside the loop will always be executed at least once, even if the condition is false. To use a do-while loop, you need to specify the code to be executed inside the loop, as well as the condition that determines whether the loop should continue executing or not.
Before we get dizzy from all this looping let’s look at a tutorial on Java methods.
Leave A Comment