java loop labels code example
Example 1: labeled for loop in java
class WithLabelledLoop
{
public static void main(String args[])
{
int i,j;
loop1: for(i=1;i<=10;i++)
{
System.out.println();
loop2: for(j=1;j<=10;j++)
{
System.out.print(j + " ");
if(j==5)
break loop1;
}
}
}
}
Output :
1 2 3 4 5
Example 2: for label loop
void enterEmail()
{
String Email=null;
String Choose=null;
String newName=null;
String newEmail=null;
String line="";
System.out.print("Please Enter your email:");
Email=keyboard.nextLine();
label1:
for(int i=0; i<eshop.buyersList.size(); i++)
{
if(eshop.buyersList.get(i).getEmail().equals(Email))
{
System.out.println("You logged in !!!");
System.out.println("Welcome "+eshop.buyersList.get(i).getName()+".Your personal details are: ");
eshop.buyersList.get(i).getBuyerInfo();
break;
}
else
{
System.out.println("You do not own an account in our E-shop.\n Do you wish to create a new one ?\t(Type Yes or No)");
Choose=keyboard.nextLine();
while(!(Choose.equals("Yes") || Choose.equals("No")))
{
System.out.println("Please Type in Yes or No. Anything else it's incorrect!!!");
Choose=keyboard.nextLine();
}
if(Choose.equals("Yes"))
{
System.out.println("Please enter your name:");
newName=keyboard.nextLine();
System.out.println("Please enter a new email:");
newEmail=keyboard.nextLine();
while(eshop.buyersList.contains(buyer.getEmail()))
{
System.out.println("This email is already in use.\n Please enter a new one. ");
newEmail=keyboard.nextLine();
}
Buyer buyer1=new Buyer(newName,newEmail);
eshop.addBuyer(buyer1);
break;
}
else if(Choose.equals("No")){System.out.println("\nThank you for visiting our E-Shop! =D"); System.exit(0);}
}
}
}