ITERATION STATEMENTS
As we have studied in the previous programming languageslike in c or c++ that iteration statements consists of for,while,do-while statements and these are also called as looping statements.Exactly same things are applied in java programming language.
Sometimes we need to execute some code of instruction over again and again till the termination condition is met so in that case iteration statements are used.First of all let us start from for loop
For loop
For loop are used for the iteration statements and its format are as follows
For(initialisation;condition;increment/decrement)
{
Body to be executed
}
First of all it initialises the variable to some constant and then it checks for the condition and then it executes the body and finally variables are incremented or decremented according to the statements.I think one sample program will make you understand in a better way.
Int a;
For(a=0;a<4;a++)
{
System.out.println(“you are now learning core java”);
}
Here this code will be executed 4 times untill the condition is false.
While statement
this is the second type of iteration statement which java programming follows for the looping .the main thing which one has to keep in mind that the body inside the while loop will not be executed atleast once if the condition is false at the bigining itself.the format of the while statement are as follows:-
while(condition)
{
Body to be executed
}
Let’s look into the following code which use while statement
Int i=0;
While(i<=5)
{
System.out.println(“you are so smart buddy”);
I++;
}
Do-while statement
Do-while statement is the another type of looping statements which is slightly different from those two looping statements which have been discussed .Do-while statement executes its body at least once no matter its condition is false at the biggining itself.the body of this is as follows:-
Do
{
Body to be executed;
}
While(condition);
Note:-hello friends!!!!! Here I would like to give one suggetion that whatever we are learning from this blog we should practice it with own because untill and unless we don’t practice it we will not learn.