4 Types loops in java ? Syntax| Working of loops

Introduction : loops in java

Hello Everyone, So today we are going to study loops in java. In this article I am going to explain what are loops in java ? Types of loops in java and many more. I am also going to show you syntax and a basic programme related to every loop syntax. So this article is going to be very much excited and full of knowledge. So without wasting any time let’s get started.

Definition

Loops are an important part of any programming language. We use loops when we want to perform any task n number of times. For example if we want to print any statement n number of times then we can use loops without writing the print statement for n times. In java we have while loop, Do while, for loop and for each loop.

Why we need loops ?

Loops are used to perform tasks repeatedly. For example, if we have a function that prints Good Morning and we want to call it multiple times, we can use loops. Instead of manually calling the function each time, we can use a loop to print it n number of times.

Types of loops

  1. For loop
  2. While loop
  3. Do While loop
  4. For each loop

Every loop have their own use cases and importance we will understand their importance as we further proceed in this article.

Code an syntax of For loop

Syntax


    for(initial value ; Condition ; increment the initial value){
     
      // Code

    } 

Code and Explanation

public class Main {
    public static void main(String[] args) {


// using for loop to print Good Morning message 10 times
    
/*

    Explanation


    1. int i = 0; This is the initialization of int type variable from zero 0.

    2. i != 0 This line checks if the current value of i is equals to 
    10 or not if it's 10 then the loop will terminate else it will print Good Morning .

    3. i++ this will increment the value of i to +1 when it print Good morning

    keyword for is used to use for loop

    */ 

    for(int i = 0; i != 10 ;i++){
      // This below line will print Good Morning
      System.out.println("Good Morning");

    }
    
  }
}

In above code we are using for loop to print Good Morning message 10 times. To define for loop you have to follow below mentioned steps

  1. First use the keyword “for” and then write open and close brackets ().
  2. Inside these brackets We need to give three parameters such as initial value, condition, increment the value by +1 every time when the condition is true.
  3. Now initialise the value in above code we set the initial value to zero.
  4. After initialising the value now define a condition. Which will be checked every time when the loop trying to run the code. If the condition is false then it will terminate the loop and move to the next instruction. We can define multiple conditions also in loops also.
  5. As of now we have defined our initial value and condition. Now it’s time to increment the value of initial value by one every time when the condition turns out to be true.
  6. Now define open and close curly brackets and write the operation you want to perform inside those brackets.

Code an syntax of while loop

Syntax

 
     while (condition) {
        
     // Code to execute and increment the value of defined by +1 when condition is true

     }

Code and Explanation

public class Main {
    public static void main(String[] args) {

      // Define an ariable of int tye with intial valeu of zero.

      int i = 0;

      // Now use while keyword to define while loop

      while (i != 10) {
        // Code to execute and increment the value of i by +1 when condition is true
        System.out.println("Good Morning");
        i++;
      }
    
  }
}

Definition : In while loop first we check for condition and if result of condition is true then we execute the code inside while loop. while loop is one of the simple loop in term of syntax.

Code Explanation

  1. First define a variable with an initial value of zero inside main function.
  2. Now use while keyword to define while loop.
  3. Inside the brackets of while write a condition.
  4. Now define open and close curly brackets and inside those brackets write code which you want to execute and increment the value of initial defined value by +1.

Code and syntax of do while loop

Syntax

/* 
       
    do{
          // Code

          // Increment the variable value

    }while(condition)
*/

Code and Explanation

public class Main {
    public static void main(String[] args) {


      /* 
      
      In do while loop we use two reserved keyword first is do and second is while

      */
       
      // Initialize the variable to zero 
      int i = 0;

      // do is the main keyword first define the code to execute inside the open and close bracket       // of do
      do{
        System.out.println("Good Morning");
        // Increment the value of initial value by +1 when condition is true
        i++;
      }
      /* while is used to define the condition. If the condition is true then perform 
      the operation else terminate the loop */ 
      while(i < 10);
  
    
    
  }
}

Definition : Do while loop is extended version of while loop. In do while loop first we perform the operation and then we check the condition is the condition is true then we continue to perform operation which are defined inside the loop else we will terminate the loop.

Code Explanation

  1. First declare the initial value to zero. This value will be used later to check the condition statement.
  2. Inside the main function define do keyword then define an open and close curly brackets. Inside those brackets write your code.
  3. In the same block of do increment the value of initial defined variable by +1.
  4. Now define while keyword and write open and close bracket.
  5. Inside the bracket define condition statement. This condition will be checked every time when loop will try to execute the code.

Code an syntax of For Each Loop

Syntax

for (type variableName : arrayName) {
  // code
}

Code and Explanation

public class Main {
    public static void main(String[] args) {


      // Define an array with int type elements
      int NumberArray[] = {1,2,3,4,5,6,7,8,9,10,11,43,456,23,12,344,35,4565,64,32,3245,5,45};
      
      /* Now by using for each loop we are assignning the values defined inside
         NumberArray to ArrayValue
      */
      for (int ArrayValue : NumberArray) {

        // This statement is going to print the value which are assigned to ArrayValue
        System.out.println(ArrayValue);

      }


      // for each loop is going to run until it will reach to last element
    
  }
}

Definition : You are not going to use for each loop very often while programming. But it also has very important use cases. As the name suggests for each so this type of loop is going to read each and every value in the list, array etc. For example if we want to read and print all the elements from the array or array-list then we can use for each. But this does not mean that for, do-while and while loop cannot perform this operation.

Code Explanation

  1. First define an array of int type then enter any random values in that array.
  2. Now use “for” keyword do define for each loop and also then define open and close brackets.
  3. Inside open and close bracket define an variable which is going to store value of list.
  4. After : this sign provides the source which means give the list name from which we are going to get all elements to print.
  5. Now last step is to define open and close curly brackets inside those brackets which an code which you want to execute.

Conclusion

So I hope this article helped you guys to learn about loops in detail. I tried my best to teach you guys about loops and its use cases and types. Still if you have any question or issue then comment section is all yours. Please share your thoughts and suggestion. We would love to know and help you guys. If you have any suggestion you can connect with us on suggestions@thecseguide-com or can contact us. I will see you guys in next exciting article till then bye!!!

Leave a Comment