Java USES a While statement to iterate over a typical instance of a group of Numbers

  • 2020-04-01 03:53:57
  • OfStack

public class BirdArray { 
  public static void main(String args[]){ 
    String[] str = new String[]{" The sparrow "," The eagle "," dove "," yellowbird "," The lark "," The peacock "," The parrot "," Red-crowned cranes "}; 
    int index =0;      //Create index variables
    System.out.println(" There are many kinds of birds in the park :"); 
    while(index<str.length){ //Through the array
      System.out.println(str[index++]); //Self-incrementing index value
    } 
  } 
} 


Output:


run: 
 There are many kinds of birds in the park : 
 The sparrow  
 The eagle  
 dove  
 yellowbird  
 The lark  
 The peacock  
 The parrot  
 Red-crowned cranes  
BUILD SUCCESSFUL (total time: 0 seconds) 

Conclusion:

Create an index variable, index, that specifies the index of the array, and as the index increases, the while loop iterates through each element and outputs it to the console. Notice the difference between ++index and index++.
++index: increments the value of index and then USES the incremented value.
Index++ : first use the value of index and then increment the value of the variable.


Related articles: