Java, .NET, PHP, PYTHON, ANGULAR, .NET, ML, Data Scince, Testing, CI Tutorials in Easy Languages.

"Best Software Training, Internship, Project Development center of Indore India, Helpline 780506-3968"


Break and Continue Statement in Java

:-
It is used to manually terminate the loop based on condition. break statements always will be associated with a conditional statement.


for(int i=1;i<=10;i++)
{
    if(i==3)
     break;
    System.out.println(i);

}


1
2

for(int i=1;i<=10;i++)
{
    if(i==3 && i==5)
     break;
    System.out.println(i);

}


1
2
3
4
5
6
7
8
9
10

for(int i=1;i<=10;i++)
{
    if(i==3 || i==5)
     break;
    System.out.println(i);

}



Continue:-

It is used to skip the data based on the condition and continue the next step. continue statements always will be associated with a conditional statement.

for(int i=1;i<=10;i++)
{
    if(i==3 || i==5)
    continue;
    System.out.println(i);

}
1
2
4
6
7
8
9
10


for(int i=1;i<=10;i++)
{
    if(i==3 || i==5)
    continue;
    if(i==3)
    break;
    System.out.println(i);

}

1
2
4
6
7
8
9
10



Modified for Loop:-


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

}

}



WAP to print 1 to 5 using infinite condition?

class BreakCont
{
   public static void main(String args[])
   {
     int i=1;
     boolean flag=true;
     for(;flag;){
     if(i==5)
     flag=false;
     System.out.println(i);
     i++;
}

}

}



  

2 Comments

If you have any doubt in programming or join online classes then you can contact us by comment .

  1. import java.util.Scanner;
    class Breakcont
    {
    public static void main (String args[])
    {
    Scanner sc = new Scanner(System.in);
    int empid,i,salary,temp;
    System.out.println("enter the Emplpoyee ID (example : 1001)" );
    empid=sc.nextInt();
    System.out.println("enter the Emplpoyee Salary");
    salary=sc.nextInt();
    for (i=0;i<=20;i++)
    {
    for(;empid<=1020;empid++)
    {
    if(salary>=21000)
    if(empid==1018)
    {
    break;
    }
    if(empid==1003||empid==1005||empid==1007)
    {
    continue;
    }
    if(empid==1013||empid==1015)
    {
    temp=salary;
    temp=salary-1000;
    System.out.println("Salary of employee "+empid+" is :"+temp);
    }
    else
    {
    System.out.println("salary of employee "+empid+" is :"+salary);
    }
    }
    }
    }
    }

    ReplyDelete
  2. import java.util.Scanner;
    class Breakcont
    {
    public static void main(String args[])
    {
    int sal,empid,i,j;
    Scanner sc=new Scanner(System.in);
    System.out.println("Enter Salary");
    sal=sc.nextInt();
    i=sal/30;
    j=sal-(i*2);
    System.out.println("Emp_id\tSalary");


    for(empid=1001;empid<=1020;empid++)
    {
    if(empid==1018 && sal>20000)
    break;
    if(empid ==1003 || empid==1005 || empid==1007)
    {
    System.out.println(empid);
    continue;
    }
    if(empid==1013 || empid==1015)
    {
    System.out.println(empid+"\t"+j);
    continue;
    }
    System.out.println(empid+"\t"+sal);
    }

    }
    }

    ReplyDelete

Post a Comment

If you have any doubt in programming or join online classes then you can contact us by comment .

Previous Post Next Post