Thursday, 28 December 2017

2.4 For, While, Do While loop and Break statement

Loops normally use to do a repeated task by avoiding the duplicate work.

All the looping control statements are easy and you can use any of them as per your understanding.

class Loop
{
public static void main(String args[])
{
//for loop
for(int i=1;i<=10;i++)
{
System.out.println(i);
}

//While loop
int j=1;
while(j<=10)
{
System.out.println(j);
j++;
}

//Do while
//This loop will run at least once and 
//then it will check for the condition.
int k=15;
do{
System.out.println(k);
}while(k<=10);

//labeled break statement
Bikesh:    // label
for(int p=1;p<=5;p++)
{
for(int q=1;q<=5;q++)
{
if(p==3)
break; // normal break
if(p==4)
break Bikesh; // labelled break
System.out.print("* ");
}
System.out.println();
}
}
}

No comments:

Post a Comment