As you all have used the looping control statements in c/c++.There may be situation when we have to execute same operation for so many times.At that time we can use looping statement in our program easily and get required output by writing small block of code.The code written inside the loop will continue to execute  until the condition become false.So it is necessary to write proper condition in loop statement to avoid the occurrence of infinite loop.

There are three type of loop mainly

1. While loop
2.For loop
3.Do while loop







let’s take a look about their syntax and example


1.While loop-While loop is generally know as entry control loop it checks the condition first and if it is true then and then only it goes inside the curly braces.
Syntax


initialization;

while(condition){statement1;statement2;statement3;increment/decrement operation;}

example


int i=0;
while(i<4)
{
System.out.println(i);
i=i+2;
}

First we have to initialize the value and then write a proper condition and operation to get required output
2.For loop-It have three part inside the for statement first one is initialization,second one is condition and third one is increment/decrement.
syntax


for(initialization;condition;increment/decrement)
  {
statement;
}

example


for(int i=0;i<4;i+2)
{
System.out.println(i);
}



3.Do..While loop-This is known as exit control loop.It checks the condition after executing the statement.


syntax


initialization;
do{
statement;
increment/decrement;
}while(condition);

example


int i;
do{
System.out.println(i);
i+2;
}while(i<4);

Let’s execute one example
problem:Execute a java program having three loop for,while and do..while.

code–>>


class loop1{

public static void main(String args[]){
int i;System.out.println(“For loop output”);
for(i=0;i<4;i++)
{
System.out.println(i);
}
System.out.println(“While loop “);
while( i!=8)
{
System.out.println(i);i++;
}
System.out.println(“Do while loop”);
do
{
System.out.println(i);
i++;
}while(i<12);
}
}

output–>>




For loop output
0
1
2
3
while loop
4
5
6
7
Do while loop
8
9
10
11

Logic–>>
what we have done?First we have take a variable named i and then write a for loop statement.In for loop we have initialized value of i to zero and take a condition i<4 and increment it with i++ operation.So the statement inside curly bracket execute four time .First it prints the value zero then increment it and then checks the condition.after condition becomes false it will goes to while loop statement .There is condition i<8which become true for i because it’s value is 4.Now it perform the same operation and continue to execute untill condition become false.At last it points to do while loop.In that it first execute the statement and then it checks the condition and perform same operation as above.


If you face any difficulty regarding this then comment here or you can also contact us ,Dont forget to like our facebook fan page.Happy coding!

By jigar

Leave a Reply

Your email address will not be published. Required fields are marked *