Java iteration (loop) statements

Iteration or loop statements are used for repeated processes. There are three types of loops in Java:

1- while
2- do while
3- for

 

iteration-statements-color

1- while loop

The loop condition is evaluated before executing the loop body. The while statement executes the loop body as long as the loop condition is true. When the loop condition becomes false the loop is terminated and execution continues with the statement immediately following the loop.

If the loop condition is false to begin with, the loop body is not executed at all. A while loop can executed zero or more times.

Exercise 1

Note that the following code counts down from 3 to 1. Each time i++ adds one to the value of i. At the first execution of the while loop  i = 1,  the second time i ++ adds one to the number i. That changes the number i to 2. The loop still go on because 2 < 3 and so on til i =3.

What is written to the standard output as the result of executing the following statements?

public class MyClass {

	public static void main(String[] args){
		int i = 1;
		while(i <= 3) {
			System.out.print(i);
			i ++ ;
		}
	}
}

Select the correct answer.


In the real world we save data in databases, but that is not practical for beginners to see how while loops work. That is why we use a Java array in the following example. If you don’t know yet what an array is, then you can read the array lesson first.

An array is a collection of the same type elements. 

Exercise 2

A movie is only allowed for adults. 5 individuals want to buy tickets online. Their ages are 17,18, 22, 16, 45. Write a program using while loop that prints only the ages of the adults to the standard out put. To use a while loop in the real world we need a data storage similar to a database. For simplicity we use in this example an array of integers. We add all the ages to the array. A while loop allows to go through all the ages, but to select the adults we need  to use the if statement.

What is written to the standard output as the result of executing the following statements?

public class MyClass {

	public static void main(String[] args){
		// declares an array of integers to add all the ages
		int[] myArray;
		// allocates memory for 5 integers
		myArray = new int[5];
		// initialize the elements which are ages in this example
		myArray[0] = 17;
		myArray[1] = 18;
		myArray[2] = 22;
		myArray[3] = 16;
		myArray[4] = 45;
		int i = 0;
		// myArray.length=5 the number of the elements
		while(i < myArray.length) {
			if(myArray[i] >= 18) {
				System.out.println(myArray[i]);
			}
			i ++ ;
		}
	}
}

Select all the correct answers.


2- do-while loop

The loop condition is evaluated after executing the loop. The do-while statement executes the loop until the loop condition becomes false. When the loop condition becomes false, the loop is terminated. The loop body is executed at least once.

Exercise 3

What is written to the standard output as the result of executing the following statements?

public class MyClass {

	public static void main(String[] args){
		int i = 7;
		do {
			System.out.println(i);
			i ++ ;
		}while(i < 4);
	}
}

Select all the correct answers.


3- for statement

The for loop is the most general of all the loops. If you know how many times you need to iterate, the for loop is probably the best choice.

Exercise 4

What is written to the standard output as the result of executing the following statements?

public class MyClass {

	public static void main(String[] args){
		for(int i = 1; i < 5; i ++ ) {
			System.out.print(i);
		}
	}
}

Select the correct answer.


Please, leave your questions, feedback and suggestions in the comments below!
SarMaroof.com offers a practical method to learn and improve your Java skills. It avoids unnecessary long boring theoretical explanations, but it uses many exercises and quizzes.


Sar Maroof is graduated from HBO Amsterdam “higher professional education” when he already had a bachelor of science degree in Physics.
He is a SUN certified JSP as well as EJB. He has experience with Java since 2001 and worked for several big as well as small companies and later as a freelancer.
He is the author of Java quizmaster and Build a Java application in 7 days.

Books by Sar Maroof

           
 Subscribe to Sar Maroof's RSS feeds!
 Mail this page to a friend!
 Bookmark this page!
Share on FacebookTweet about this on TwitterShare on RedditShare on LinkedIn

5 Comments

Add a Comment
  1. straight from the source

    I just want to say I am beginner to weblog and definitely savored this web-site. More than likely I’m going to bookmark your site . You definitely have good writings. Kudos for sharing your website page.

  2. Hello ~ Awesome article ~ Thanks

  3. Hey, that post leaves me feeling foohlsi. Kudos to you!

  4. Nice website and easy to navigate!

    1. The accident of finding this post has — my day

Leave a Reply

Your email address will not be published. Required fields are marked *

Sarmaroof © 2017