Loops in Java

Introduction

A loop is used to tell the computer to do something repeatedly. For example, you want to print out “Hello Java” 100 times.

The While loop

We use the While loop when we do not know how many times we have to repeat. The syntax for the while loop is:

while (condition){
    //code statements
}

For example with the problems mentioned in the beginning. We need to print out the string “Hello Java” 100 times. The following code will solve it with the While loop.

int count = 0; //set the starting value of counting times
//start while loop by checking condition if count is less than 100
while (count < 100){
    System.out.println("Hello Java");
// after printed out, increase count by 1 until count reached 99
    count++; 
}

The do-while loop

This is a variation of the while loop. Instead of checking the condition before executing the program, the do-while loop will check the continuation condition after completed code statement once. The syntax is:

do{
    ///code statements
}while(conditions);

The following example will help you get more detail about the differences between the While loop and do-while loop.

public class DifferencesOfLooping{
	public static void main(String[] args){
		int count = 0; 
		while(count < 10){
			System.out.println("while loop" + count);
			count++;
		}
		count = 1000; //set the new value for count
		do{
			System.out.println("Do..while loop" + count);
			count++;
		}while(count < 10);
	}
}

And the output of the above code statements is

Output

You could see that the Do-while loop printed the first output although we gave the wrong condition, and it has to stop after printing once.

The For loop

Similar to the While loop, the For loop is used to repeat the actions; However, we should use it if we know exactly how many times need to be looped. The syntax of For loop is:

for(starting index; continuation-condition; step){
    //code statements;
}

For example, we have a list of people’s names, and want to print them out.

public class testForLoop{
	public static void main(String[] args){
                //creating a list of name
		String[] name = {"john, kaka, foo, fee, fi"};
		int i;
                //getting the length of list 
		int len = name.length;
		for(i = 0; i < len; i++){
                        //starting at the index 0
			System.out.println(name[i]);
		}

	}
}

The output will be

john, kaka, foo, fee, fi 

There is a variation of the For loop, it is For each loop. For example, with the same problems, we can do like this:

public class usingForEach{
	public static void main(String[] args){
		String[] name = {"john, kaka, foo, fee, fi"};
		for(String n : name){
			System.out.println(n);
		}
	}
}

Conclusion

In this article, we covered four loops in Java including while, do-while, for and for each. Hope that you are engaging in it. Thanks for reading.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Create a website or blog at WordPress.com

Up ↑

%d bloggers like this: