Skip to main content

Java Loop Tutorial

 JAVA LOOPS  : 

 Loops means multiple time execution of same code written inside loop blocks.

Now let's understand first FOR LOOP

1. FOR LOOP : FOR LOOP will have 3 things.

  • Start index
  • A terminator condition 
  • Increment flag.

if loop start index is 1 and terminator condition is index should be less than 10 and start index incremented by 1 , then code inside loop brackets will be executed 9 times.

if loop start index is 1 and terminator condition is index should be less than 10 and start index incremented by 2 , then code inside loop brackets will be executed 4 times.

As mentioned FOR LOOP has three parts , start index, terminator condition and incremental value.

Example : 

for(int i=0 ; i < 10 ; i++){

  // Code to be executed 

}

All this three operator = , < and ++ already explained in my previous post JAVA OPERATORS Please go through it if you have confusion to understand.

And how this for loop is getting started and executing only till terminator condition. Java is using decision making statement called IF ELSE to start and end the loop.

Now let's understand IF ELSE statement. It's pretty simple to understand just remember that only one condition will be executed in case of IF ELSE, either IF or ELSE, below is the syntax.

IF(CONDITION)

  //Code to be executed LINE 1

ELSE

   //Code to be executed LINE 2

  if condition is true then only line 1 will be executed , if not then line 2 will be executed. Condition should only return true or false, we can have multiple condition using AND or OR operator as explained in my previous post JAVA OPERATORS .

 Let's go back to FOR LOOP 

for(int i=0 i < 10 ; i++){

  // Code to be executed 

}

So here loop is starting with i values as 0 and it will go to terminator condition   i.e  i <10  , this is true as 0 is less than 10 , so it will go inside brackets and code will be executed , once its done i value will be incremented by 1 as i++ is post increment operator so after execution only i value will be incremental . 

  Now i value will be 1,  again loop will go to terminator condition i.e   i<10 , this is again true as 1 is still less than 10 , so it will go inside brackets and code will be executed , once its done again i value will be incremented by 1 as i++ is post increment operator so after execution only i value will be incremental 

 This process will be repeat till terminator condition i.e i <10 is getting false. it will be false once i value will be reached to 10, as 10 is not less than 10.That's why code will be executed 9 times only, if we will change terminator  condition to i <= 10 , then execution will be for 10 times.

I hope FOR LOOP  is cleared to you guys if not , please let me know in comment section.

Once you will understand FOR LOOP, then rest loops will be very easy for you.


2. WHILE LOOP : this loop will continue  till it's not satisfying the condition mentioned inside while.


int i=0;
while( i < 10 )
{
    //Code to be executed
    i++;

}

this loop will run for 9 times;


3. DO WHILE LOOP : Same as WHILE LOOP, only difference is the condition check will be after code execution.

int i=0;
do
{
    //Code to be executed
    i++;

}while( i < 10 )


4. FOR EACH LOOP : This is enhanced for loop where we don't need to think about start index terminator and incremental value, this will be take care by loop only, only thing we need to provide is the Array of data, this loop will start from the first element to last element of the Array by itself.

for(int a : array)
{

// Code to be executed
Print a;

}

Next topic is how can exit the loop in between or skip a particular condition while running any loop.

Answer to this question can be defined in 2 keywords BREAK and CONTINUE.

BREAK keyword is used to interrupt the loop and stop further looping. and java will go to next line of execution without running remaining loop. In simple words it break the loop and exit the moment it encounter a statement BREAK.

for(int i=0 i < 10 ; i++){

  if( i== 5 )

   break;

  // Code to be executed 

}

In this case only till i value is 4 loop will be running i.e 4 times, then loop will get terminated and remaining loop will not be consider.

Now same condition as BREAK if we want but we don't want to terminate the loop , we just want that for a particular condition only loop should be skip and rest should be run as usual , in this case we should use a keyword called CONTINUE.

  CONTINUE will not terminate the loop it will only skip a particular loop when it meets condition.

for(int i=0 i < 10 ; i++){

  if( i== 5 )

   continue;

  // Code to be executed 

}

In this case code will not be executed when i value will be 5, it means code will be executed 9 times instead of 10 times


PLEASE READ EARLY SESSION THROUGH BELOW LINK :


  1. BASIC OVERVIEW OF JAVA (SESSION 1)
  2. PRIMITIVE TYPES AND WRAPPER CLASS (SESSION 2)
  3. JAVA OPERATORS (SESSION 3)
  4. LOOPS IN JAVA (SESSION 4)
  5. NAMING CONVENTION WITH FACTORIAL PROGRAM (SESSION 5)

Comments

Popular posts from this blog

Java Program : Writing First Java Factorial Program with explanation

 NAMING CONVENTION IN JAVA : Java is an object oriented programming language , we can relate it to real life object like i mapped Java with human in my previous post JAVA OVERVIEW (SESSION 1)  and represent human properties like body parts as properties in Java and Human can dance , drive , walk , run these can be mapped as Behaviour in java.    Now To represent properties and behaviour in java , there are some standard naming conventions we should follow. Class name should always starts with Uppercase letter like class Student { //Code to be executed } Properties or any kind of variables should starts from lower case and afterwards every first letter of each next word should be in Upper case . like class Student { int studentId ; String studentName ; //Code to be executed } Methods name should also starts from lower case and afterwards every first letter of each next word should be in Upper case . like class Student { int studentId ; String studentName ;

OOPS Concept in Java : ENCAPSULATION

OOPS Concept in Java : ENCAPSULATION   This OOPS concept can be used to make things simpler in case of software development . Main purpose of this concept is to hide the properties of any class and give access to fetch and modified based on business criteria.  A simple example can be a POJO ( Plain Old Java Object) in which all the properties of a class can be private and through getter and setter method of properties we can fetch and update the properties of Object. So instead of having direct access to properties we have created 2 methods to make the CLASS things encapsulated in single unit while access to it is via 2 public methods.   Just consider we have requirement that once the object is created its value should not be changed then simplest way to achieve this  can be done by just removing setter method and we will keep only getter methods to access Object properties . In this case after Object creation whatever the value of Object properties has been initialised it will b

OOPS Concept in Java : POLYMORPHISM

 POLYMORPHISM IN JAVA :  Polymorphism means mutiple forms of Single reference. And to understand in simple way just take an example from my previous post of OOPS CONCEPT IN JAVA : INHERITANCE  , I request you guys to go through this link before proceding here. So in this post I have created a method called sum() in PARENT class which has been used by CHILD class without writing same sum() method in CHILD class. This was possible becuase of INHERITANCE concept in java.  But what if CHILD class is not satisfied with PARENT sum() and CHILD class wants to improve it like by adding some message before the calculation. To do this we have another OOPS CONCEPT IN JAVA i.e POLYMORPHISM and by applying this logic we can make same sum() method behvae differently based on Object. As I mentioned earlier POLYMORPHISM means different forms and same has been achieved here by calling same sum() method but the output is different based on Object on which it has been called. If Object is of PARENT