Loops in Java

In this article, we will study about loops in Java. Loops are the repetition of the code. For example, if I want to print 'harish' ten times then I will have to write Print command or 'harish' 10 times, but using loop I will write it only once and it will repeat to print 10 times.

Java has four types of loops:

  1. while loop
  2. do while loop
  3. for loop
  4. foreach loop

In this section we will read about only first three loop because fourth loop is related to iteration we will read about this loop in next tutorial.

while loop:

In this loop, we define a initial condition and a terminating condition. The loop starts with initial condition and ends at terminating condition. The syntax of while loop is :

while(condition){.........code.........}

This condition is same as condition of if else statement. While condition is true or non-zero this code will run again and again.

Look at this example:

  1. package findalltogether;
  2. public class Tutorials
  3. {
  4. public static void main(String args[])
  5. {
  6. int number = 0;
  7. // while loop start
  8. while(number != 10)
  9. {
  10. // code
  11. System.out.println("harish");
  12. // changing condition for terminate loop
  13. number++;
  14. }
  15. }
  16. }

In this code, number starts from 0 and the loop will run until number is less than 10. number is increasing 1 unit per repetition. This code will print 'harish' 10 times.

do while loop:

This loop is also same as while loop but only difference is that it checks condition later and runs code first. In other words, it checks condition only for repetition. Its syntax is:

do{.....code.......} while (condition);

For example:

  1. package findalltogether;
  2. public class Tutorials
  3. {
  4. public static void main(String args[])
  5. {
  6. int number = 0;
  7. do
  8. {
  9. System.out.println("harish");
  10. number++;
  11. }while(number != 10);
  12. }
  13. }

This code is also printing 'harish ' ten times. The different occurs only when I initialized number 10 or more value. In this case, while loop will not run but do while will. So, do while loop runs at least one time. Note the semicolon after while command in do while loop.

for loop:

The syntax of for loop is:

for( initialize, condition, operation){ ....... code ....... }

Before discussing anything first take an example:

  1. package findalltogether;
  2. public class Tutorials
  3. {
  4. public static void main(String args[])
  5. {
  6. // example 1
  7. //for loop with initialisation inside loop
  8. for(int i = 0;i< 10;i++)
  9. {
  10. System.out.println("harish");
  11. }
  12. System.out.println("Initialization outside the loop");
  13. // example 2
  14. // Initialisation outside the loop
  15. int j = 0;
  16. for(;j<10;j++)
  17. {
  18. System.out.println("second type");
  19. }
  20. // example 3
  21. // more than one condition
  22. System.out.println("more than one condition");
  23. for(int p = 1, x =5; p<10 && x < 30; p++, x*=2)
  24. {
  25. System.out.printf("p is %d and x is %d \n",p,x);
  26. }
  27. }
  28. }

In first two types of loops, we see that first we have to initialize a variable, then we have to check a condition for loop and at last we have to change the value of variable (increment or decrement or something other). In for loop, we do all three things at same place. The for loop takes three arguments. Note these points:

  1. initial value: This value is same as it is for first two types. In for loop, we can initialize a variable in two ways, one inside the loop (in brackets) and another before the loop. Note the syntax in above example. Each argument is separated by semicolon. If you are initializing value outside the for loop then also we have to put semicolon for initial value at initializing place (second example).
  2. condition: condition is same as first two cases. It compares the initialized variable value and the given value. The loop will run until the condition is true.
  3. operation: operation is also same as before. It changes the value of variable(s) after finishing code of loop each time. Operation may be any operation like addition, multiplication or even any complex operation.
  4. In third example, note that we can define more than one variables, conditions and operations inside the loop by separating then with a comma.

NOTE: We are not taking any tough example here because we will take them in advance series. It is basic series for beginners. If you like it then please share it with your friend otherwise give your valuable feedback to improve this as well as future tutorial to help other people.

Don't forget to Subscribe for latest tutorials directly in your inbox.

Comments and feedback are welcome.

[000webhost_bottom_banner]



About Harish Kumar

Harish is an interested person in the field of web development and blogging. He works for the need of young web developers in learning various languages, latest technologies and other essential tips and tricks. If you need some help or you have some suggestion then you email him at harish@findalltogether.com without any hesitation. You can also suggest/demand for articles of your own choice.

Related Articles

Java is object oriented and platform independent programming language which runs on JVM. Lets see advantages of java ...
Introduction to Java
In this tutorial, we will learn how to give/ take input/ output to console. What are classes, main() and comments? Wh...
First program in Java
Variable is a memory location which stores data. In Java, we have to define what type of variable (integer, text, str...
Variables and data types in Java

Login or Sign up to leave comment.