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

Source Code For Lesson 10:

import java.util.Scanner;
class Jtutorial1 {
    public static void main(String args[]){
        Scanner input = new Scanner(System.in);
        int i=0;//initialize a new variable for use as a counter
        while(i < 10){//While the variable is less than 10
            System.out.println(i);//Print the current value for i
            i+=2;//increase the value of i by 2
        }//end while
        i=1;//set i equal to 1
 
 
        do{//does the loop condition at LEAST once
            System.out.println(i);// output i
            i*=2;   // multiply the current value of i by 2
        }while(i<4096);//until i is 4096 or greater
 
 
 
 
    }//end main
}//end class

 


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 }

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

//Simple "sentinel loop"
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 loopi=0;//reset 0 before the loop

//Simple "output" 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:

//Simple do_while loop (Executes at least once)
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.

Increment / decrement 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.

Last modified: April 9, 2019

Author