Skip to main content

Introduction to Java Programming

  What is Java? Java is a programming language and a platform. Java is a high-level, robust, object-oriented and secure programming language. Java was developed by Sun Microsystems (which is now the subsidiary of Oracle) in the year 1995. James Gosling is known as the father of Java. Firstly, it was called  Greentalk  and the file extension was  .gt . After that, it was called  Oak . Initially Java was designed for small, embedded systems in electronic appliances like set-top boxes. Platform:  Any hardware or software environment in which a program runs, is known as a platform. Since Java has a runtime environment (JRE) and API(Application Programming Interface), it is called a platform. The principles for creating Java programming were "Simple, Robust, Portable, Platform-independent, Secured, High performance, Multithreaded, Architecture neutral, Object-oriented, Interpreted, and Dynamic". Currently, Java is used in internet programming, mobile devices, games, e-business so

11. Break and Continue in Java

 

Break

  • The break is a keyword which is used inside loops or switch statement.
  • The break statement is used to exit the loop irrespective of whether the condition is true or false.
  • Whenever a break is encountered inside the loop or switch-case, the control is sent outside the loop or switch body.
  • The break statement breaks the loop one by one i.e., in the case of nested loops.
Control flow for break statement


Continue

  • The continue statement is used to immediately move to the next iteration of the loop.
  • It skips everything below the continue statement inside the loop and continues with the next iteration.
  • It is used for a condition so that we can skip some code for a particular condition.
Control flow for continue statement



class BreakContinue {
    public static void main(String[] arr) {
        System.out.println("Printing numbers from 100 to 110.");
        for (int i = 100; i <= 110; i = i + 1) {
            System.out.println(i);
            if (i == 105) {
                System.out.println("Breaking the loop...");
                break;
            }
        }
        System.out.println("\nPrinting numbers from 100 to 110.");
        for (int i = 100; i <= 110; i = i + 1) {
            if (i == 105) {
                System.out.println("A number will be ignored by the loop which is 105...");
                continue;
            }
            System.out.println(i);
        }
    }
}

Output :

Printing numbers from 100 to 110.
100
101
102
103
104
105
Breaking the loop...

Printing numbers from 100 to 110.
100
101
102
103
104
A number will be ignored by the loop which is 105...
106
107
108
109
110


Previous

Next

Comments