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

Bubble sort Implementation

Bubble sort  : In bubble sort ,we will select the 1st element and compare with all the remaining element, same process we will continue for all the elements as we are traveling the whole Array 2 times except the element which we have selected to compare with other elements but still it will be consider as n time.    So time complexity for bubble sort will be O(n^2).         space complexity for bubble sort will be O(1). // Bubble Sort class BubbleSort { public static void sort ( int [] array) { int n = array. length ; while ( true ) { boolean swapped = false; for ( int i = 0 ; i < n - 1 ; i++) { if (array[i + 1 ] < array[i]) { swap (array , i , i + 1 ) ; swapped = true; } } if (!swapped) break; } } private static void swap ( int [] array , int i , int j) { int temp = array[i] ;...

Object-Oriented Programming Concept in Java

OOPS( Object-Oriented Programming ) Concept in Java :   As we all know Java is Object Oriented programming language and what exactly it means in simple words to understand can be described as whatever is going to happen by Java , it will be based on some Object.  So next question can be what is Object ? , "Object is the representation or reference of Class to access its properties and use its behaviour ", now next is What is Class in java and answer to this question is "A class in java is the blueprint of Properties and Behaviours of it's own Object" as explained in my previous post  BASIC OVERVIEW OF JAVA  (SESSION 1)   Let's understand through an example : public class FirstJavaProgram { int firstNumber=10; int secondNumber=20;      public int sum(int fNum, int sNum){         return fNum+sNum;     }     public static void main(String[] args) {     //our logics ...

Overview of time and space complexity for sorting algorithm

Bubble sort : In bubble sort ,we will select the 1st element and compare with all the remaining element, same process we will continue for all the elements as we are traveling the whole Array 2 times except the element which we have selected to compare with other elements but still it will be consider as n time.    So time complexity for bubble sort will be O(n^2).         space complexity for bubble sort will be O(1). Selection sort : In selection sort we will divide the Array in 2 parts , sorted and unsorted and select the min element from unsorted and will copy to sorted array.      Time Complexity for selection sort also is O(n^2).      space complexity for bubble sort will be O(1). Insertion sort :  In insertion sort, Same process like selection sort , we will divide the array in sorted and unsorted array and we will select element from unsorted array and will insert it into sorted array at its proper...