MrBool
You must be logged in to give feedback. Click here to login
[Close]

You must be logged to download.

Click here to login

[Close]

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

[Close]

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

Java Loops: For, While, Do, Continue and Break

In this article I will teach you, how you can use the loops in Java Programming. There are simple ways to use loops in any programming language.

[close]

You didn't like the quality of this content?

Would you like to comment what you didn't like?

In today's article we will understand how to work with loop in Java programming. We will talk about the topics below:

  • For Loop in Java Programming
  • While Loop in Java Programming
  • Do while Loop in Java Programming
  • Use of continue in Java Programming
  • Use of break in Java Programming

These topics are very important to understand, when you are using the loops in your applications. Because sometimes we need to break or continue any iteration in loops. As you know we use the loops in any programming language just for the iterations.

Loops:

There are three types of looping concepts in Java Programming as we used in C++. These are used for different purpose, and we can use them to run same iteration and result will be same too. But their aim is change lets check the syntax in listing 1:

Listing 1: Syntax:

For(intial condition; final condition; increment/decrement)
{
//Statements here we will use the statements or conditions also
}
While(Condition)
{
//Statements here we will use the statements or conditions also
}
Do{
// Statements here we will use the statements or conditions also
}while(condition);

For Loop:

We use for loop, when we know how much it should be repeat the iteration. It is very useful at that time when we fully agree about its limit. Because sometimes we want to repeat an iteration to given conditions, as we want to write a line just 1000 times. So the for loop is very useful for it to write it from 1 till 1000. Let’s check it in listing 2 for details how we can use it, in java Programming.

Listing 2: Use of For loop in Java Programming

public class ForLoop{
  public static void main(String args[])
  {
    //printing in ascending order
    //from 1 to 10
    //using increment operator
    for(int i=1; i<=10; i++)
    {
      System.out.print(i+" ");
    }
    System.out.println();
    //printing in descending order
    //from 10 to 1
    //using decrement operator
    for(int i=10; i>=1; i--)
    {
      System.out.print(i+" ");
    }
  }
}

In this listing 2 example there is a simple program. I gave the class name as ForLoop and in the main method I used for loop to times. In the first loop, it is increasing the iteration from 1 to 10 and in the second loop it is decreasing the iteration from 10 to 1. As I told you in the syntax we can use increment and decrement operators in for loop.

While Loop:

We use while loop in Java Programming to certain number of iterations. And it will not work like for loop. Let’s check it in listing 3 for details, but in the listing 3 we use the same program which we did in listing 2. But we will change for loop in while.

Listing 3: Use of While Loop in Java Programming

public class WhileLoop{
  public static void main(String args[])
  {
    //printing in ascending order
    //from 1 to 10
    //using increment operator
    int i = 1;
    while(i <= 10)
    {
      System.out.print(i+" ");
    i++;
    }
    System.out.println();
    //printing in descending order
    //from 10 to 1
    //using decrement operator
      i = 10;
    while (i >= 1)
    {
      System.out.print(i+" ");
      i--;
    }
  }
}

Do while Loop:

The do while loop is good to use, because it will run or execute once at any cost. It is similar like while loop. For more details about the do while loop. Let’s we check the listing 4.

Listing 4: Use of Do While Loop in Java Program

public class DoWhileLoop{
  public static void main(String args[])
  {
    //printing in ascending order
    //from 1 to 10
    //using increment operator
    int i = 1;
    do
    {
      System.out.print(i+" ");
    i++;
    }while(i <= 10);
    System.out.println();
    //printing in descending order
    //from 10 to 1
    //using decrement operator
      i = 10;
    do
    {
      System.out.print(i+" ");
      i--;
    }while (i >= 1);
  }
}

In the listings 2, 3, & 4 we use the for loop, while loop and do while loop. In these examples there are fully simple and easy examples. Which are defining the correct use of all three loops. These loops sometimes do something different, because during the programming we create complex algorithms so we become confuse. Which control statements should be use now, for example we are creating an application and we are giving options to the user to choose the options and use the application. At that time we commonly we use the while or do while loop. Because using for loop becomes so difficult that type of algorithms. Let’s check the listing 5 for details.

Listing 5: Using Control statement in Java Programming

import java.util.Scanner;

public class ControlStatment{
  public void Display(){
    System.out.println("Welcome to Mr.bool");
  }
  public void ForLoopSytax(){
  String syntx = "for(initial;final;incre/decre){statment}";
  System.out.println(syntx);
  }
  public void WhileLoopSytax(){
  String syntx = "while(condition){statment}";
  System.out.println(syntx);
  }
  public void DoWhileLoopSytax(){
  String syntx = "do{statment}while(condition);";
  System.out.println(syntx);
  }
  public void Application(){
     Scanner input = new Scanner(System.in);
     char letter;
     letter = (char)input.nextInt();
   do
    {
      Display();
      System.out.println("Please Choose Any Option");
      System.out.println("Press [F] For For Loop Syntax..");
      if(letter == 'F' || letter == 'f')
      {
        ForLoopSytax();
      }
      System.out.println("Press [D] For Do While Loop Syntax..");
      if(letter == 'D' || letter == 'd')
      {
        DoWhileLoopSytax();
      }
      System.out.println("Press [W] For While Loop Syntax..");
      if(letter == 'W' || letter == 'w')
      {
        WhileLoopSytax();
      }
      System.out.println("Press [N/n] For Exit From The Application");
    } while(letter != 'N' || letter != 'n');
  }
  public static void main(String args[]){
    ControlStatment CS = new ControlStatment();
    CS.Application();
  }
}

In the listing 5, the program will be stop when the user will input N or n letter. If the user input any other as mentioned in the program so it will call the methods according to the letter. We can use the loops for any purpose in our programs. We use the loops to control the situations or control the iterations in the programming.

Now we will use some keywords with loops iterations. These keywords are being used to make more powerful execution of the iterations in loops. These keywords are continue and break.

Keyword Continue:

The keyword continue is used in the control statements with condition statements. Control statements mean that we use continue commonly in loops when we are using the conditional statements in loops. The continue statement will continue the iteration when the conditional statement is true.

Keyword Break:

The keyword break is used to stop the iterations in control statements. Sometimes we need to stop the loops at a specific point. We also use the conditional statements in this case, when the conditional statement is true then iteration will be stop.

NOTE: We can use continue and break keywords in a single loop iterations when we are using the conditional statements. It depends on us when conditional statement is true or false what it should to do? Let’s check the listing 6 for details about the break and continue keywords.

Listing 6: Break Keywords

public class Keyword{
  public static void main(String args[]){
    for(int i=1; i<=10; i++)
    {
      if(i == 5)
      {
        System.out.println("Loop stopped at 5th iteration");
        break;
      }
    }
  }
}

As you can see in the listing 6, when the iteration reached at the 5 from 1. The iteration stopped due to break keyword. Now lets we use the continue keyword in listing 7

Listing 7: Continue Keyword

public class Keyword{
  public static void main(String args[]){
    for(int i=1; i<=10; i++)
    {
      if(i % 2 == 0)
      {
        System.out.printf("\nLoop is at %dth iteration..",i);
        continue;
        
      }
      else if(i == 5)
      {
        System.out.printf("\nLoop stoped at %dth iteration",i);
        break;
        
      }
    }
  }
} 

In this listing 7, I used both keywords continue and break. When if condition is true so the iteration will be stop at 5th iteration, otherwise the else will be true and the iteration will be continue.

Conclusion

In this tutorial we discussed the control statements in Java programming. We also explained the examples which we used them in the listings. We also discussed two keywords as they are continue and break. These keywords commonly used in the conditional statements as you can see the listing 6 and 7.

These topics which we covered today, these are most important to understand the way to use. Because as I mentioned it before there is difference between the three loops. As they are for, while and do while loop. These all are used for different purposes.



Java and Android application developer with more than 3 years experience.

What did you think of this post?

Did you like the post?

Help us to keep publishing good contents like this.

SUPPORT US

funded

remaining

[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually � in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits � in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login