You may have already guessed that the keyword used to start a while statement is while. Similar to an if statement, the body of the loop, designated by the curly brackets, runs repeatedly unless the condition becomes false.
while (condition) {
  // code block to be executed
}
 A loop that does not end is referred to as an infinite loop. In a while loop, especially at the introductory level, you will find that the body of the loop also includes a section where you might update tracking variables. You will see examples of this below. As usual, variables used in the body of the loop need to have been declared or are declared. Most tracking variables will be declared immediately above the while loop.

while loop

int x =0; 
while(x < 5){
    System.out.println("Hello World");     x++; }
If the condition evaluates to true then the body of the loop will run. In the example above, the body of the loop runs for when x equals 0 to 4 (0, 1, 2, 3, 4). Notice how x was declared and initialized before the loop, but is updated in the body of the loop. Forgetting to update the value of x means the condition will always be true!

Infinite Loop

while(true){
    System.out.println("Hello World");
} 

The condition for this loop never changes therefore the body will always run hence infinite.

Examples

In the example above, the code will find the sum of 7+8+9+10+11, ..., 999. Accumulating the numbers in the variable sum and incrementing the tracking variable num is done in the body of the loop. It is not until after the loop has terminated that the solution is known so the print line is moved outside of the while loop.

Find the sum of all even numbers from 7 to 999 inclusive
int num = 8, sum = 0;
while(num <= 998){
    sum += num;
    num+=2; 
}
System.out.println(sum);
//2nd solution
int num = 7, sum = 0;
while(num <= 999){
    if(num%2==0){
        sum += num;     }
     num++;
} System.out.println(sum);

Finding the sum of all numbers from 7 to 999 inclusive can have different solutions as shown above. One can either start at the first even number in the range and end at the last even number or you can use the modulo operator to check for evenness. Recall that if a number%2 returns 0 then it is even since dividing by 2 resulting in a 0 remainder. 

for Loop

The second type of loop common in programming is the for-loop. There is also a second version of this you will see later known as an enhanced for loop. This loop structure can be easier to follow and debug as all of its components are typically on the same line as shown below.
for (initialization condition; condition; var update(s)){
    //body runs if condition is true
}
Below are equivalent blocks of code using the while and for loops.
for(int i = 0; i < 5; i++){
    System.out.println(i);
}
//using while loop
int i = 0;
while( i < 5){
    System.out.println(i);
    i++; }
Often time, many students will switch to strictly using the for loop the majority of the time they need a loop because of the organization factor that makes it easier and shorter to debug.

Infinite for loop

for( ; ; ){
     System.out.println("I will not stick gum under the desk");
}  
Believe it or not, the block of code shown above compiles! It's an infinite loop!

Examples:for Loops

int sum =0;
for(int num = 0; num <= 1001; num++){
    if(num%2==1) sum+=num;
}
System.out.println(sum);
Notice that you might still need to declare and initialize variables before the loop. You need to do this if the variables need to exist after the execution of the loop's body as shown above.
 
Print numbers 3-7 inclusive using a for loop:
for(int i = 3; i <= 7; i++){
   System.out.println(i);
}

Print "hello world" 30 times;

for(int i = 0; i < 30; i++){
   System.out.println("Hello World");
}


Looking for enhanced for-loops?

Enhanced for-loops are covered in Unit 6. They're common when traversing data structures so the first introduction to them is when learning about arrays.