# Exercise 02: Simple for loop

 Quizzes     Assignments     Puzzles     Easy exercises     Required knowledge
 Easy Exercises   For Beginners Select    01   02   03   04   05   By title

The following code demonstrates a simple Java for loop. The level of this serie exercises is very easy. They are all designed for beginners.

Read More: Java Iteration (Loop) Statements

### Java exercise 02: A simple for loop

Level: Very easy

What happens if the following code is compiled and run?

public class MyClass
{
public static void main(String[] args)
{
for(int i = 0; i < 3; i++)
{
System.out.print(i);
}
}
}

###### Author: Sar Maroof

Select the correct answer.

### Answer explanation

1. The statement for(int i = 0; i < 3; i++) means that execute the body of the loop as long as the value of the variable i is smaller than 3 and by each execution icrement the value of i by one. The initial value of i is 0, therefore the statement System.out.print(i); writes 0 to the standard ouput.
2. By the second time execution of the body of the loop, the value of i becomes 1.
3. By the third time execution of the body of the loop, the value of i becomes 2.
The statmenent System.out.print(i); writes all the three value of i to the standard ouput.

The correct answer is: a.

 Sar Maroof is a professional software development teacher, gives master classes and publishes technical articles. He is also an expert software developer and worked for several big as well as small companies and later as a freelancer.