Loop Control

In some situation you need to execute a block of code several number of times in such a case loops are used. Java supports following loops.
  1. while Loop
  2. do...while Loop
  3. for Loop
  4. for each loop
Loops in Java
1) The while Loop
  • A while loop is a control structure that allows you to repeat a task for certain number of times.
  • When executing, if the condition is true, then the actions inside the loop will be executed. This will continue as long as the condition is true.
Syntax:
The syntax of a while loop is:
1
2
3
4
while(condition)
{
     //Statements
}

Example:
The example of while loop is:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
public class DemoWhileLoop {

 public static void main(String args[]) {

  int x = 10;
  while (x < 15) {
   System.out.print("value of x : " + x);
   x++;
   System.out.print("\n");
  }
 }
}

Output:
value of x :10
value of x :11
value of x :12
value of x :13
value of x :14

2) The do...while Loop
  • A do...while loop is similar to a while loop, except that a do...while loop is guaranteed to execute at least one time.
  • The condition appears at the end of the loop, so the statements in the loop execute once before the condition is tested.
  • If the condition is true, the flow of control jumps back up to do, and the statements in the loop execute again. This will continue as long as the condition is true.
Syntax:
The syntax of a do...while loop is:
1
2
3
do{
     //Statements
}while(condition);

Example:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
public class DemoDoWhile {

 public static void main(String args[]) {
  int x = 10;
  do {
   System.out.print("value of x : " + x);
   x++;
   System.out.print("\n");
  } while (x < 13);
 }
}

Output:
value of x :10
value of x :11
value of x :12


3) The for Loop
  • for loop is a repetition control structure that execute a specific number of times.
  • for loop is useful when you know how many times a task is to be repeated.
Syntax:
The syntax of a for loop is:
1
2
3
for(initialization;condition; update){
     //Statements
}

The flow of control in a for loop:
  • The initialization step is executed first, and only once. This step allows you to declare and initialize any loop control variables.
  • Next, the condition is evaluated. If it is true, the body of the loop is executed. If it is false, the body of the loop does not execute and control jumps out of for loop
  • After the body of the for loop executes, the flow of control jumps back up to the update statement. This statement allows you to update any loop control variables. This statement can be left blank, as long as asemicolon appears after the condition.
  • The condition is now evaluated again. If it is true, the loop executes and the process repeats itself (body of loop, then update step,then condition check). After the condition is false, the for loop terminates.
Example:
1
2
3
4
5
6
7
8
9
public class DemoForLoop {

 public static void main(String args[]) {
  for (int x = 10; x < 13; x = x + 1) {
   System.out.print("value of x : " + x);
   System.out.print("\n");
  }
 }
}
Output:
value of x :10
value of x :11
value of x :12

4) The for-each Loop
  • It is mainly used to traverse array or collection elements. 
  • The advantage of for-each loop is that it eliminates the possibility of bugs and makes the code more readable.
Syntax:
1
2
3
for(data_type variable : (array or collection)){
      //...
}  

Example:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
class ForEachExample {

 public static void main(String args[]) {

  int arr[] = { 12, 13, 14, 44 };
  for (int i : arr) {
   System.out.println(i);
  }
 }
}

Output:
12
13
14
44


<-- Previous || Next -->

No comments:

Post a Comment