Skip to main content

Break and Continue Statement in Java



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.


  

Comments

  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

POST Answer of Questions and ASK to Doubt

Popular posts from this blog

Uncontrolled form input in React-JS

  Uncontrolled form input in React-JS? If we want to take input from users without any separate event handling then we can uncontrolled the data binding technique. The uncontrolled input is similar to the traditional HTML form inputs. The DOM itself handles the form data. Here, the HTML elements maintain their own state that will be updated when the input value changes. To write an uncontrolled component, you need to use a ref to get form values from the DOM. In other words, there is no need to write an event handler for every state update. You can use a ref to access the input field value of the form from the DOM. Example of Uncontrolled Form Input:- import React from "react" ; export class Info extends React . Component {     constructor ( props )     {         super ( props );         this . fun = this . fun . bind ( this ); //event method binding         this . input = React . createRef ();...

JDBC using JSP and Servlet

JDBC means Java Database Connectivity ,It is intermediates from Application to database. JDBC has different type of divers and provides to communicate from database server. JDBC contain four different type of approach to communicate with Database Type 1:- JDBC-ODBC Driver Type2:- JDBC Vendor specific Type3 :- JDBC Network Specific Type4:- JDBC Client-Server based Driver  or JAVA thin driver:- Mostly we prefer Type 4 type of Driver to communicate with database server. Step for JDBC:- 1  Create Database using MYSQL ,ORACLE ,MS-SQL or any other database 2   Create Table using database server 3   Create Form according to database table 4  Submit Form and get form data into servlet 5  write JDBC Code:-     5.1)   import package    import java.sql.*     5.2)  Add JDBC Driver according to database ide tools     5.3)  call driver in program         ...

JSP Page design using Internal CSS

  JSP is used to design the user interface of an application, CSS is used to provide set of properties. Jsp provide proper page template to create user interface of dynamic web application. We can write CSS using three different ways 1)  inline CSS:-   we will write CSS tag under HTML elements <div style="width:200px; height:100px; background-color:green;"></div> 2)  Internal CSS:-  we will write CSS under <style> block. <style type="text/css"> #abc { width:200px;  height:100px;  background-color:green; } </style> <div id="abc"></div> 3) External CSS:-  we will write CSS to create a separate file and link it into HTML Web pages. create a separate file and named it style.css #abc { width:200px;  height:100px;  background-color:green; } go into Jsp page and link style.css <link href="style.css"  type="text/css" rel="stylesheet"   /> <div id="abc"> </div> Exam...