Tuesday, June 1, 2021

Java Tutorial: for, for-each, do-while and while loop statements

Chapters

Hello Everybody! This is brainy ghosts and today we're gonna discuss the loop statements. Java has different types of loops. In this blog we're going to discuss for,for-each,while and do-while loop statements.

for Loop Statement

Alright! Let's start with the for loop. You're going to use this loop a lot of times because of its usability when looping a collection or arrays.
General Form: for(statement1,statement2,statement3){//code to execute}
Example:
public static void main(String[]args){

  for(int i = 1; i <= 5; i++){
    System.out.print(i + " ");
  }
}

Result
1 2 3 4 5
Statement1: I would like to call this the initializer. We put a variable that we want to loop and assign a value to set the starting point of a loop.
Statement2: I would like to call this the conditional. If a condition that is related with the initializer variable becomes false, the loop ends.
Statement3: I would like to call this the value changer, if you will. This is where we increase/decrease the value of the initializer variable.

for-each Loop Statement

This loop statement simply loops a collection or array from the first element up to the last.
General Form: for(expression1 : expression2){//code to execute}
Example:
public static void main(String[]args){

int[] numbers = {1,2,3,4,5};

  for(int num : numbers){
    System.out.print(num + " ");
  }
}
Result
1 2 3 4 5
expression1: contains a variable that will hold each array/collection element per loop. The variable must have the same data type as the collection that you wanna loop.
expression2: contains an/a array/collection that is going to be iterated.

The syntax of this statement is much simplier than for loop but for loop is still more flexible. So, when to use for-each and for loop?

1.) If you want arbitrary access to array/collection elements then use for loop.
Example:
public static void main(String[]args){

int numbers[] = {2,3,5,6,8};

  for(int i = numbers.length-1; i >= 0; i--){
    System.out.print(numbers[i] + " ");
  }
}

Result
8 6 5 3 2
In the example above, I use for loop to iterate the array from the last index to first. You can't do that in for-each loop.

2.) If you need to know the index of each element in an/a array/collection, for loop is the man for the job.
Example:
public static void main(String[]args){

int numbers[] = {1,2,3,4,5};

  for(int i = 0; i < numbers.length; i++){
    if(i == 0)
      System.out.println("We're at the first element: " + numbers[i]);
    else if(i == numbers.length-1)
      System.out.println("We're at the last element: " + numbers[i]);
    else System.out.println(numbers[i]);
  }
}

Result
"We're at the first element: " 1
2
3
4
"We're at the last element: " 5
3.) If you just want to iterate array/collection in sequential manner then use for-each loop.
Example:
public static void main(String[]args){

String[] str = {"I","Love","Earth!"};

  for(String s : str){
    System.out.print(s + " ");
  }

}

Result
I Love Earth!
do-while Loop Statement

This statement is used when you want to loop a task and end it based on certain condition.
General Form: do{//code to execute}while(condition);
Example:
public static void main(String[]args){

  String str = "";
  System.out.println("Task: add letter \"s\" to str until it reaches"
  					+" a certain length");
  do{
    str += "s";
  }while(str.length() < 5);
  System.out.println("result: " + str);
}

Result
sssss
while Loop Statement

This is very similar to do-while but without the "do" part.
General Form: while(condition){//code to execute};
Example:
public static void main(String[]args){

  String str = "";
  System.out.println("Task: add letter \"s\" to str until it reaches"
  					+" a certain length");
                    
  while(str.length() < 5){
    str += "s";
  }
  System.out.println("result: " + str);
}

Result
sssss
We can put an initializing expression in the while parentheses while formulating a condition.
public static void main(String[]args){

  int num = 0;
  
  while((num = ++num) < 5)
    System.out.println(num);
}
Don't forget to put the initializing expression in the parentheses.

So, What's the difference between do-while and while apart from the compacity of while loop? do-while loop executes its block first before evaluating the condition whereas while loop evaluates the condition first before executing its block.
Example:
public static void main(String[]args){

   String str = null;
   String placeholder = "";
   System.out.println("Task: add letters \"s\" to str until it reaches"
                      +" a certain length");
   System.out.println();
   System.out.println("do-while loop");
   //do-while loop
   do{
      placeholder += "s";
      str = new String(placeholder);
   }
   while(str.length() < 5);
   System.out.println("result: " + str);
   System.out.println();
   System.out.println("while loop");
   //while loop
   str = null;
   placeholder = "";
   while(str.length() < 5){
      placeholder += "s";
      str = new String(placeholder);
   }
   System.out.println("result: " + str);
}
You will encounter a NullPointerException when the program starts executing the while loop 'cause str is null and the length() method of String class will throw NullPointerException if you're trying the get the length of a null string.

In do-while loop, do is executed first where a new string is created that's why the length() doesn't throw NullPointerException.

break and continue keywords

We already saw the break keyword being used in the switch statement. It can be used in loops too.
Example:
public static void main(String[]args){
  String str[] = {"doll","puppet","figurine"};
  
  System.out.println("Find puppet");
  for(int i = 0; i < str.length; i++){
    if(str[i].equals("puppet")){
      System.out.println("Found puppet in index: " + i);
      break;
    }
    System.out.println(str[i] + " is not a puppet.");
  }
}

Result:
doll is not a puppet.
Found puppet in index: 1
break keyword stops the execution of loops. In this example, "figurine" string is not displayed on the result because the loop stopped at the "puppet" string.

continue keyword interrupts the execution in the loop's body.
Example:
public static void main(String[]args){
  String str[] = {"doll","puppet","figurine"};
  
  System.out.println("Find puppet");
  for(int i = 0; i < str.length; i++){
    if(str[i].equals("puppet")){
      System.out.println("Found puppet in index: " + i);
      continue;
    }
    System.out.println(str[i] + " is not a puppet.");
  }
}

Result:
doll is not a puppet.
Found puppet in index: 1
figurine is not a puppet.
In the example above, continue keyword interrupted the loop when it found "puppet" that's why the println() below didn't execute. You can see in this example that the loop didn't completely stop, unlike in break. That's why "figurine" is displayed on the result. Try removing the continue and the result is going to be:
Result:
doll is not a puppet.
Found puppet in index: 1
puppet is not a puppet.
figurine is not a puppet.


We can't put codes next to break and continue keywords. If we do, we will encounter an "unreachable" error.
public class SampleClass{

 public static void main(String[]args){
   
   for(int i = 0; i < 4; i++){
	   break; //same result if we use continue
	   System.out.println(i);
   }
 }
}
This code will throw an "unreachable" error. Java can't execute codes next to break because if the execution encounters break or continue, it will exit the loop or move to the next iteration. Thus, the codes next to break or continue are "unreachable".

Infinite loops

Note: if you unintentionally stuck in a loop just close the terminal or command prompt(or ctrl+c in Windows). If you're using IDE find a button that will make your program to stop.
We can create a loop that never stops by using these syntaxes:
//do-while infinite loop
do{}while(true);
//for infinite loop
for(;;){//code to execute}
//while infinite loop
while(true){//code to execute}
Use the break keyword to stop these loops programatically.
In practical programming, you will see the while infinite loop most of the time 'cause it's simple and more readable than its counterparts. You will see infinite loops in game loop, timer, monitoring, etc.

Using floating-point numbers in loops

We can use floating-point numbers in loops.
Example:
public static void main(String[]args){
  
  for(double i = 0; i <= 0.6; i+=.15){
    System.out.println(i);
  }
}

Result
0.0
0.15
0.3
0.44999999999999996
0.6
The example above works fine but it's not recommended due to floating-point nature that may cause errors. I recommend you to read this topic about floating-point numbers in this link:Is floating point math broken? Alternative way is to compute the floating-point inside the loop.
Example:
public static void main(String[]args){
  
  double d = 0;
  for(int i = 0; i <= 4; i++){
    d = 0.15 * i;
    System.out.println(d);
  }
}

Result
0.0
0.15
0.3
0.44999999999999996
0.6
The example above only works if you know how many iterations you need. In the example, I use i <= 4 because I want to iterate .15 five times(Starting from 0).

Now, pay attention to this result: 0.44999999999999996
that must be .45 but the program displayed 0.44999999999999996
As you can see, floating-point numbers are sometimes innacurate and this inaccuracy may lead to nerve-racking errors. Consider this example.
Example:
public static void main(String[]args){
  double d = 0;
  
  while(true){
    System.out.println(d);
    d += 0.15;
    if(d == 0.5) break;
  }
}
The example above will cause an infinite loop due to the floating-point artifact.

Omitting some tokens and keywords in loop statement

If you read my if statement tutorial some conditions that I'm going to show here are the same as the conditions in that tutorial.

1.) Braces can be removed if there's only one statement in the body of a loop statement.
Example:
public static void main(String[]args){
  for(int i = 1; i <= 5; i++)
    System.out.print(i + " ");
  System.out.println();
    
  String str = "";
  System.out.println("Task: add letter \"s\" to str until it reaches"
                       +" a certain length");
  do
   str += "s";
  while(str.length() < 5);
	
  System.out.println("result: " + str);
}
2.) We are not required to compare a boolean variable or a method that returns boolean value to the "true" and "false" keywords. This only applies to loop statements that have condition statement like for,do-while and while loop.
Example:
public static void main(String[]args){

  String str = "";
  
  do{
    str += "s";
  }while(!str.equals("sssss"));
  System.out.println("result: " + str);
}
3.) In for loop, we can omit any statement in the parentheses.
Example:
public static void main(String[]args){

  for(int i = 0;;i++){
    System.out.print(i + " ");
    if(i >= 5) break;
  }
  System.out.println();
  
  for(int i = 0;;){
    System.out.print(i + " ");
    i++;
    if(i >= 5) break;
  }
  System.out.println();
  
  int i = 1;
  for(;i <= 5;){
    System.out.print(i + " ");
    i++;
  }
}
Nested loops

Like other statements, loop statements can be nested. Here's an example.
Example:
public static void main(String[]args){

   String[] str = {"Puppet","Trumpet","Veteran"};
	  
   for(String s : str){
       System.out.println("Word: " + s);
	   
       boolean isP = false;
       for(int i = 0; i < s.length(); i++){
         if(s.charAt(i) == 'p' || s.charAt(i) == 'P' ){
            isP = true;
            break;
         }
         System.out.println(" " + s.charAt(i) + " at index: " + i);
       }
       if(isP) System.out.println("There's a \"P\" in " + s); 
       else System.out.println("There's no \"P\" in " + s);
       System.out.println();
   }
}

Result
Word: Puppet
There's a "P" in Puppet
         
Word: Trumpet
 T at index: 0
 r at index: 1
 u at index: 2
 m at index: 3
There's a "P" in Trumpet

Word: Veteran
 V at index: 0
 e at index: 1
 t at index: 2
 e at index: 3
 r at index: 4
 a at index: 5
 n at index: 6
There's no "P" in Veteran
Notice that break didn't stop the for-each loop. It only stopped the for loop. That's because break and continue only affect loops where they reside. Now, try to nest loops yourself and try to include while or do-while loop.

Loop Using Labels

We can use labels to specifically redirect an iteration of a loop. break and continue keywords use labels to redirect iteration to a specified scope. Take a look at this example.
public class SampleClass{

 public static void main(String[]args){
   
   main_loop: 
     for(int i = 0; i < 5; i++){
       System.out.println("main: " + i);
       sub_loop:
         for(int j = i; j < 5; j++){
           System.out.println("sub: " + j);
           if(j <= 1){
             System.out.println
             ("continue sub_loop");
             continue sub_loop;
           }
           for(int k = 0; k < 2;k++){
             System.out.println
             ("inner: " + k);
             
             if(j > 3){
             System.out.println
             ("break main_loop");
             break main_loop;
             }
           }
        }
     }
 }
}

Result
main: 0
sub: 0
continue sub_loop
sub: 1
continue sub_loop
sub: 2
inner: 0
inner: 1
sub: 3
inner: 0
inner: 1
sub: 4
inner: 0
break main_loop
The name of labels must follow the naming syntax of variables.

No comments:

Post a Comment