Skip to main content

Loop controls in Java

Loop control is one of java's language feature like any other programming language feature, which allows to execute written statements in loop body to execute sequentially those many time until loop condition not get satisfied.

Java programming language provides four types of loop controls or constructs.

  1. while loop
  2. do...while loop
  3. for loop
  4. for-each loop
we will see all those constructs with example one by one.

while loop

  • while loop execute the statements or group of statements multiple times unless given loop condition not fail
  • while loop checks for condition first then execute statements in loop body 
  • repeat above step till condition satiesfied
see, below code snippet explains while loop:
Output:

do...while loop

  • do...while loop checks for condition later executing body of loop
  • unlike while loop it does not check condition first, this is one of major difference between do--while and while loop
  • like while loop, it executes loop body until condition not failed
  • in failure condition scenario, do...while loop executes at-least once.
see, below code snippet:
Output:

for loop

  • for loop is useful when you know how many times the loop body to be executed.
  • this loop construct allows you to compactly define or initialise loop condition then define how long condition will stay true and condition update statement
  • unlike do...while loop for loop checks for condition after condition initialisation, then execute body and then execute condition update statement
  • repeat above step until condition stays true.
see, below code snippet:
Output:

for each loop

  • for each loop is advance loop in java.
  • it introduced to improve code readability and beautify code.
  • this loop iterates over array and Iterable type objects
  • one can nest for each loop inside for each loop
  • for each is not useful for filtering list or array.
  • it travers list sequentially without index to access elements
see, below code snippet:
Output:





Comments

Popular posts from this blog

Lambda expression in Java 8

Behaviour Parameterisation …pattern which allows you to write such a flexible code which can cope up with any requirement change. Now you may wondering why I am talking about this weird sounding pattern instead lambda expression, right? Well, let me tell you that we will soon get to know about lambda expression in detail but understanding Behaviour Parameterization  pattern will definitely help you thoroughly how lambda expression is useful to write concise and succinct code and you will start feeling power of lambda expression which is one of prime feature of java 8 release. Lets say, we have list of Books and you need to find Books with greater than 200 pages. You could write a method getBooksGreaterThanPages(): Using this method is simple enough, what if you need to find books smaller than 200 pages? Or Books above particular prices? Or Books from particular author or publications? Or even worst book with particular publication with certain pages and below partic...

Default Methods in Java 8

Adding new methods in interface s always break the implementation in class. So Java 8 come up with new feature which propose a mechanism to extend existing interfaces by adding new methods without breaking existing implementation thus, achieved the backward compatibility. Default methods is the feature which helps you to extend existing interfaces . If added method in interface provides the default implementation then no implementing Class get affects.   Default methods enable you to add new functionality to the interfaces of your libraries and ensure  binary compatibility with code written for older version of those interfaces. An implementing class can override the default implementation defined in implemented interface  and add its own implementation. To understand it with example, lets say there is ABC company which wants to launch their smart Radios in market, so they have developed  SmartRadio interface(contract) and ABCRadio final product im...

Reference data type in java

Unlike Primitive data types in java, Reference data type store the address of object in memory which they refer to, than actual value. lets say, Car class is already defined, and its object is created in memory then variable declared with type Car becomes the reference type which will be holding address of car object to which it refers. Car car = new Car(); int noOfDoors = 4; Car myCar = car; // copy the car reference into myCar reference . Here, variable car actually contains the address and variable noOfDoors will contain actual 4 value. See below diagram for more explanation: Reference data type holding address of object in heap memory and primitive data type holds actual primitive values Available reference types in java: Class type Interface type array type e.g. String name = new String("Java"); List listOfNames = new ArrayList(); Students topRankers[] = new Student[10]; Remember though you know now that Reference data type holds ...