Subscribe To MegiSoft RSS Feeds!     Your Browser OR  Google Feedburner

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

 

Article: Java iteration(loop) statements
Quizzes: Beginner  Intermediate  Advanced 

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.



Please, leave your questions, feedback and suggestions in the comments below! If you need more explanation by any of the answers, just let me know to update it for you!

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.



Please, leave your questions, feedback and suggestions in the comments below! If you need more explanation by any of the answers, just let me know to update it for you!

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.



Please, leave your questions, feedback and suggestions in the comments below! If you need more explanation by any of the answers, just let me know to update it for you!

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! If you need more explanation by any of the answers, just let me know to update it for you!

Megisoft.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. The author of Megisoft is Sar Maroof who has more than a decade of experience with web development mainly with Java, MySql, HTML and more..
Article: Java iteration(loop) statements
Quizzes: Beginner  Intermediate  Advanced 

Suggested Articles:


Click Here For Info about: Sar Maroof's New Java Book
Author: Sar Maroof
More than a decade of experience with developing web applications mainly with Java.
Sun Certified JSP as well as EJB
CIW Certified Internet Webmaster - Mind IT
Applied Science in Physics HBO Amsterdam
Bachellor Science in Physics at Basra university.
 Subscribe To Megisoft RSS Feeds! (Your Browser)
 Mail This Page To A Friend!
 Bookmark This Page!
Share on FacebookTweet about this on TwitterShare on RedditShare on LinkedIn

4 Comments

Add a Comment
  1. Hello ~ Awesome article ~ Thanks

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

  3. Nice website and easy to navigate!

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

Leave a Reply

megisoft © 2016