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 :
- BASIC OVERVIEW OF JAVA (SESSION 1)
- PRIMITIVE TYPES AND WRAPPER CLASS (SESSION 2)
- JAVA OPERATORS (SESSION 3)
- LOOPS IN JAVA (SESSION 4)
- NAMING CONVENTION WITH FACTORIAL PROGRAM (SESSION 5)
Comments
Post a Comment