Java Loop Control


SUBMITTED BY: rahulranjanmca

DATE: Jan. 5, 2016, 5:59 p.m.

FORMAT: Text only

SIZE: 2.9 kB

HITS: 30296

  1. There may be a situation when you need to execute a block of code several number of times. In general, statements are executed sequentially: The first statement in a function is executed first, followed by the second, and so on.
  2. Programming languages provide various control structures that allow for more complicated execution paths.
  3. A loop statement allows us to execute a statement or group of statements multiple times and following is the general form of a loop statement in most of the programming languages:
  4. Java Tutorial
  5. Java programming language provides the following types of loop to handle looping requirements. Click the following links to check their detail.
  6. Loop Type Description
  7. while loop
  8. Repeats a statement or group of statements while a given condition is true. It tests the condition before executing the loop body.
  9. for loop
  10. Execute a sequence of statements multiple times and abbreviates the code that manages the loop variable.
  11. do...while loop
  12. Like a while statement, except that it tests the condition at the end of the loop body
  13. Loop Control Statements:
  14. Loop control statements change execution from its normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed.
  15. Java supports the following control statements. Click the following links to check their detail.
  16. Control Statement Description
  17. break statement
  18. Terminates the loop or switch statement and transfers execution to the statement immediately following the loop or switch.
  19. continue statement
  20. Causes the loop to skip the remainder of its body and immediately retest its condition prior to reiterating.
  21. Enhanced for loop in Java:
  22. As of Java 5, the enhanced for loop was introduced. This is mainly used to traverse collection of elements including arrays.
  23. Syntax:
  24. The syntax of enhanced for loop is:
  25. for(declaration : expression)
  26. {
  27. //Statements
  28. }
  29. Declaration: The newly declared block variable, which is of a type compatible with the elements of the array you are accessing. The variable will be available within the for block and its value would be the same as the current array element.
  30. Expression: This evaluates to the array you need to loop through. The expression can be an array variable or method call that returns an array.
  31. Example:
  32. public class Test {
  33. public static void main(String args[]){
  34. int [] numbers = {10, 20, 30, 40, 50};
  35. for(int x : numbers ){
  36. System.out.print( x );
  37. System.out.print(",");
  38. }
  39. System.out.print("\n");
  40. String [] names ={"James", "Larry", "Tom", "Lacy"};
  41. for( String name : names ) {
  42. System.out.print( name );
  43. System.out.print(",");
  44. }
  45. }
  46. }
  47. This would produce the following result:
  48. 10,20,30,40,50,
  49. James,Larry,Tom,Lacy,

comments powered by Disqus