Sunday, July 17, 2016

JAVA Loop Statements




Loops allow for simplified coding when you want to perform the same task over and over again.


For example, you may want to create a print out of an integer value counting down from 10 to 0. Instead of writing a print command 10 times you can use a loop that will run through a single print command ten times, and update the integer value automatically.


for ( int i = 10; i >= 1; i = i -1 ) {
out.println("Countdown" + i );
}


The for loop statement contains 4 parts

int i = 10; is the initialization of the variables in the loop

i>=1;  is the test condition for the loop, as long as this condition is true the loop will continue to execute

i=i-1 is the update instruction, the variable value will be updated at the conclusion of each execution of the loop

{out.println("Countdown" + i);} is the body of the loop everything within the {} is executed as long as the loop condition allows


Common Loop Coding Errors


Don't put a semicolon ; immediately after the for statement:

for ( int i = 10; i >= 1; i = i -1 ) ; {
out.println("Countdown" + i );
}

The loop statement will end after the ; and won't run through the execution of the body of the text



Don't create an update statement that doesn't update the variable


for ( int i = 10; i >= 1; i = i-- ) {
out.println("Countdown" + i );
}

Improper use of the shortcuts i-- and i++ , these do not change the original value of the variable, use ++i and --i instead


Don't create a conditional statement that is false under the initialization value

for ( int i = 10; i <= 1; i = i -1 ) {
out.println("Countdown" + i );
}

If the initial value of the variable results in a false condition statement the loop will not execute anything




Nested For Loops


You can write loops inside of other loops when needing to run through multiple variable updates at the same time.

For example, you can use the Countdown loop and add another loop to print out a variable's value that increases by 1/10 every time the countdown progresses:


for ( int i = 10; i >= 1; i = i -1 ) {

for (double j = 0.1; j<=1; j=j+0.1) {
out.println(j + " seconds" );
}

out.println("Countdown" + i );
}

The nested loop needs to use a different variable name so that it will not interfere with the outer loop's function


Do not use the wrong variable in the test condition statement



for ( int i = 10; i >= 1; i = i -1 ) {

for (double j = 0.1; i<=1; j=j+0.1) {
out.println(j + " seconds" );
}

out.println("Countdown" + i );
}


If the wrong variable is called in the inner loop you may run an infinite loop - the variable i does not update inside the inner j-loop

Do not use the wrong update variable




for ( int i = 10; i >= 1; i = i -1 ) {

for (double j = 0.1; j<=1; i=i+0.1) {
out.println(j + " seconds" );
}

out.println("Countdown" + i );
}


If you update the wrong variable you may get results that you do not expect, or run the risk of running an infinite loop






Indefinite Loops


If you want to create a loop that has an unknown number of iterations or allows for a user to input data controlling the number of loop iterations you can use an indefinite while or do while loop.


The format for the while loop looks like this:


int n=0;

while(n<=100) {
n=n+5;
out.println(n);
}


The do while loop is best used when you know the loop will be executed at least once:



int n=0;

do{
out.println(n);
n=n+1;
}while(n<=100);



You can use while or do while loops in the place of for loops whenever you choose, you can experiment with these options to optimize your code for space and simplicity.



Loops and the Random Number Generator Class


Random numbers are an important mathematical tool for solving lots of complex world problems. You can also simulate a random input such as a role of a dice for gaming purposes. Generating random data is a common step for statistical models and can be done easily in JAVA with the Random Class.


The Random Class is most commonly used with loops to generate and store many random numbers at once with a simple set of code instructions:


import java.util.Random;  //imports the JAVA Random Class

Random rand = new Random();  //creates a random object called rand

int random_Integer = rand.nextInt();  //initializes a variable


do{
out.println(random_Integer);
random_Integer = rand.nextInt();
}while(random_Integer!=1);




Common Random Methods

random_Integer = rand.nextInt();

random_Integer_below_Max = rand.nextInt(100);

random_Double = rand.nextDouble();  (range returned between 0.0 and 1.0)




No comments:

Post a Comment