Tag Archive for while

Java Tutorial #10 — Loop logic / intro


Topics Discussed: While loops, do_while loops, counter controlled loops, loop syntax, loop scoping

Source Code Available Here


Loops
In this video I discuss two or the most important constructs in the entire language of java. In the next lesson I talk a bit more about for loops, which seem to be used a little more than while and do_while which we discussed here.


While loops
While loops are statements that will be repeated until either a break statement is reached, or until the condition is no longer true. Here’s a few examples of loops using while:


//syntax while(condition){ //do stuff }
//Examples below

int i=0;
while(i < 9){
     //do stuff
     i++; //increment i-- if you remove this statement, the loop will run infinitely.
}

boolean isTrue=true;
i=0; //reset 0 before the loop

while(isTrue){//while the boolean is true
     if(i > 10){
          isTrue = false;
}//end if
     i++; // increment i
}//End loop

i=0;//reset 0 before the loop
while (i < 10){
     System.out.println(i); //output the value of i each time through the loop
     i++; // increment i each time through the loop
}

In the examples above you’ll notice that we always have a ‘way out’ of the loop, that’s because there is a common error known as an ‘infinite loop’ in which your code executes endlessly because your loop is unable to exit.

Do_while loops
Do_while loops are the same basic idea as while loops, except that they execute the contents of the loop once before checking the conditional. These are very useful in making users enter certain values for their input, as seen below:


int aValue=0;
do{ // do these instructions
System.out.println("Enter a value 1 or greater for the variable: ");
aValue= input.nextInt();
}while(aValue < 1);//until this condition is no longer true.


Incrementing numbers
Any number that is of a type that can hold a whole number value can be incremented or decremented. In MOST cases you will be using “post-incrementing” which is seen as varName++; however, there are other ways of incrementing numbers:


++i; // pre-increment
i++; // post-increment (most-common)
--i; // pre-decrement
i--; // post-decrement (common while decrementing)

Each time you hit the statement i++;, you increase the value of i by the value of 1.

Java Tutorial #13 — More loops! Sentinel and break;


Topics Discussed: More loop practice, sentinel / dummy record loops, breaking out of loops

Source Code Available Here


Sentinel control loops
A sentinel loop is a loop that is controlled by a ‘false’ value. These will be much more useful when we get into more advanced types of data storage (especially arrays, lists, arraylists, etc.). The program will run until this value comes up. Let let’s assume for a moment that we have some values that we want to iterate through:


Values we want to iterate through: 1, 7, 17, 43, 123,12,19, 4, 190, 23, 26, 44, 92, 81, 77, 41,4, 0

Now, with all those values a counter controlled loop just wouldn’t cut it since our values will vary so widely. Assuming that we didn’t know the number of entries the user was entering we would have one last option on how to end the loop, by adding a value onto the end of the data

New dataset: 1, 7, 17, 43, 123, 12,19, 4, 190, 23, 26, 44, 92, 81, 77, 41, 4, 0, -999

Now we can fashion a loop (in pseudocode) to take advantage of this oddball value:

while(x != -999){
     //process next record
}


The break statement
The break statement is used as a way to break out of a loop, it’s syntax is really quite simple, but it’s implementation can be tricky at times. To call the break statement in a program simply add break; while you’re in a loop. This will break out of the inner-most loop, example:

while(condition){
     while(condition2){
          if (condition==true){
               break;// this break will break out of the inner while but will stay inside the outer while.
          }//endif
     }//end inner while
//end outer while

In the above example it’s obvious that the break statement can give us additional control in our loops, but it comes at the cost of complexity. There will be some times in the future where we will have much more complicated loops where breaking out of one will return us to another at a strange point. Be certain that you plan your loops / breaks with care as they are tempestuous at best.