Break and Continue Statement in Java

3


Break and Continue Statement in Java
:-
It is used to manually terminate the loop based on conditions. 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 conditions?
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++;
}
}
}
Program to create salary calculator:-
WAP to calculate the salary of an employee where empid will be started from 1001 to 1020, empid 1003,1005 and 1007 2 days salary will be deduced, empid 1013 and 1015 salary will not be displayed, if entered salary will be >20000 then the calculation will be implemented till 1017.


  
Tags

Post a Comment

3Comments

POST Answer of Questions and ASK to Doubt

  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
  3. Name - Suresh Suryavanshi
    Batch - 10-11
    Teacher - Vandana mam

    class BrkCnt
    {
    public static void main(String[] args)
    {
    for(int i=1;i<=20;i++)
    {
    if(i==5&&i==8)
    continue;
    if(i==11)
    break;
    System.out.println(i);
    }
    }
    }

    class BrkCnt1
    {
    public static void main(String[] args)
    {
    for(int i=1;i<=20;i++)
    {
    if(i==5||i==8)
    break;
    if(i==11)
    continue;
    System.out.println(i);
    }
    }
    }

    class Brk
    {
    public static void main(String[] args)
    {
    for(int i=1;i<20;i++)
    {
    if(i==5)
    break;
    System.out.println(i);
    }
    }
    }

    class Brk1
    {
    public static void main(String[] args)
    {
    for(int i=1;i<20;i++)
    {
    if(i==5&&i==8)
    break;
    System.out.println(i);
    }
    }
    }

    class Cntu
    {
    public static void main(String[] args)
    {
    for(int i=1;i<=20;i++)
    {
    if(i==-5||i==8)
    continue;
    System.out.println(i);
    }
    }
    }

    class Cntu1
    {
    public static void main(String[] args)
    {
    for(int i=1;i<=20;i++)
    {
    if(i==5&&i==8)
    continue;
    System.out.println(i);
    }
    }
    }

    ReplyDelete
Post a Comment