X

Play AlgoQuiz

Rate Us :



Share with Friends :

Menu

Looping concept in Java:

There may be a situation when we need to execute a block of code for several number of times and is often referred to as a loop. Java has very flexible three looping mechanisms. As of version 1.5 of Java, the enhanced for loop was introduced which is mainly used for Arrays.

We can use one of the following three loops :

X

flexible :

It refers to be able to be easily modified to respond to altered circumstances.

X

mechanisms :

It refers to a natural or established process by which something takes place or is brought about.

X

iterations :

It refers to the repetition of a process.

  • while loop




  • do...while loop




  • for loop




while loop :

A while loop is a control structure that allows us to repeat a task for a certain number of times.

If we don't know the number of iterations in advance then while loop is best suited.

Syntax: while(boolean_expression) { //Statements }

The argument to the while loop should be of boolean type. If we are using any other type then we will get compile time error.

while(1)
{
 System.out.println("Algo");
} /* Compile time error : incompatible types
                          required : boolean
                          found : int */

Curly braces are optional and without curly braces we can take only one statement which should not be declarative statement.

while(true)
System.out.println("Algo"); // valid
while(true); // valid
while(true)
int a = 10; // invalid (because we have declarative statement)
while(true)
{
 int a = 10;
} // valid
while(true)
{
 System.out.println("Algo");
}
System.out.println("Valley"); // Compile time error : unreachable statement
while(false)
{ // Compile time error : unreachable statement
 System.out.println("Algo");
}
System.out.println("Valley");
int a = 10 , b = 20;
while(a < b)
{
 System.out.println("Algo");
}
System.out.println("Valley"); // valid
final int a = 10 , b = 20;
while(a < b)
{
 System.out.println("Algo");
}
System.out.println("Valley"); // Compile time error : unreachable statement

Example:

class Example { public static void main(String [ ] args) { int a = 1; while(a <= 5) { System.out.println("USA"); a++; } } }

When executing, if the boolean_expression result is true, then the actions inside the loop will be executed. This will continue as long as the expression result is true. Here, key point of the while loop is that the loop might not ever run. When the expression is tested and the result is false, the loop body will be skipped and the first statement after the while loop will be executed.

Output:

Here, the loop will be carried on until the condition becomes false. Therefore, output will be - USA USA USA USA USA


X

MiniDoll :

MiniDoll is an application to learn with fun. You can play it to solve some tricky questions related to Java Programming. Either enjoy playing it alone or discuss with your friends to solve it. It is a simple approach to learn by solving questions. This application has different topics of Java and each topic is divided into three phases. Start solving from initial topic to final topic and by the end of every topic you can boost your concept of that topic.