Difference between revisions of "JAVA: Perintah Break di Java"

From OnnoWiki
Jump to navigation Jump to search
Line 1: Line 1:
 
Break Statement is a loop control statement that is used to terminate the loop. As soon as the break statement is encountered from within a loop, the loop iterations stop there, and control returns from the loop immediately to the first statement after the loop.  
 
Break Statement is a loop control statement that is used to terminate the loop. As soon as the break statement is encountered from within a loop, the loop iterations stop there, and control returns from the loop immediately to the first statement after the loop.  
 +
 +
Break Statement adalah pernyataan kontrol loop yang digunakan untuk mengakhiri loop. Segera setelah pernyataan break ditemukan dari dalam sebuah loop, iterasi loop berhenti di situ, dan kontrol kembali dari loop segera ke pernyataan pertama setelah loop.
 +
  
 
Syntax:  
 
Syntax:  
Line 6: Line 9:
  
 
Basically, break statements are used in situations when we are not sure about the actual number of iterations for the loop or we want to terminate the loop based on some condition.   
 
Basically, break statements are used in situations when we are not sure about the actual number of iterations for the loop or we want to terminate the loop based on some condition.   
 +
 +
Pada dasarnya, pernyataan break digunakan dalam situasi ketika kita tidak yakin tentang jumlah iterasi sebenarnya untuk perulangan atau kita ingin menghentikan perulangan berdasarkan beberapa kondisi.
 +
  
  
Line 16: Line 22:
 
* To exit a loop.
 
* To exit a loop.
 
* Used as a “civilized” form of goto.
 
* Used as a “civilized” form of goto.
 +
 +
 +
Break: Di Jawa, break sebagian besar digunakan untuk:
 +
 +
* Hentikan urutan dalam pernyataan switch (dibahas di atas).
 +
* Untuk keluar dari loop.
 +
* Digunakan sebagai bentuk goto yang "beradab".
 +
 +
  
 
==Using break to exit a Loop==
 
==Using break to exit a Loop==
Line 21: Line 36:
 
Using break, we can force immediate termination of a loop, bypassing the conditional expression and any remaining code in the body of the loop.  
 
Using break, we can force immediate termination of a loop, bypassing the conditional expression and any remaining code in the body of the loop.  
 
Note: Break, when used inside a set of nested loops, will only break out of the innermost loop.   
 
Note: Break, when used inside a set of nested loops, will only break out of the innermost loop.   
 +
 +
Dengan menggunakan break, kita dapat memaksa pemutusan loop dengan segera, melewati ekspresi kondisional dan kode yang tersisa di badan loop.
 +
Catatan: Break, ketika digunakan di dalam satu set loop bersarang, hanya akan keluar dari loop terdalam.
 +
 +
 +
  
 
[[File:Exit.png|center|300px|thumb]]
 
[[File:Exit.png|center|300px|thumb]]
Line 56: Line 77:
  
 
Java does not have a goto statement because it provides a way to branch in an arbitrary and unstructured manner. Java uses the label. A Label is used to identifies a block of code.  
 
Java does not have a goto statement because it provides a way to branch in an arbitrary and unstructured manner. Java uses the label. A Label is used to identifies a block of code.  
 +
 +
Java tidak memiliki pernyataan goto karena menyediakan cara untuk bercabang secara arbitrer dan tidak terstruktur. Java menggunakan label. Label digunakan untuk mengidentifikasi blok kode.
 +
 +
  
 
Syntax:  
 
Syntax:  
Line 70: Line 95:
 
Now, break statement can be use to jump out of target block.  
 
Now, break statement can be use to jump out of target block.  
 
Note: You cannot break to any label which is not defined for an enclosing block.  
 
Note: You cannot break to any label which is not defined for an enclosing block.  
 +
 +
Sekarang, pernyataan break dapat digunakan untuk melompat keluar dari blok target.
 +
Catatan: Anda tidak dapat memutuskan label apa pun yang tidak ditentukan untuk blok penutup.
 +
 
Syntax:   
 
Syntax:   
  

Revision as of 06:04, 5 May 2022

Break Statement is a loop control statement that is used to terminate the loop. As soon as the break statement is encountered from within a loop, the loop iterations stop there, and control returns from the loop immediately to the first statement after the loop.

Break Statement adalah pernyataan kontrol loop yang digunakan untuk mengakhiri loop. Segera setelah pernyataan break ditemukan dari dalam sebuah loop, iterasi loop berhenti di situ, dan kontrol kembali dari loop segera ke pernyataan pertama setelah loop.


Syntax:

break;

Basically, break statements are used in situations when we are not sure about the actual number of iterations for the loop or we want to terminate the loop based on some condition.

Pada dasarnya, pernyataan break digunakan dalam situasi ketika kita tidak yakin tentang jumlah iterasi sebenarnya untuk perulangan atau kita ingin menghentikan perulangan berdasarkan beberapa kondisi.


Break.png


Break: In Java, the break is majorly used for:

  • Terminate a sequence in a switch statement (discussed above).
  • To exit a loop.
  • Used as a “civilized” form of goto.


Break: Di Jawa, break sebagian besar digunakan untuk:

  • Hentikan urutan dalam pernyataan switch (dibahas di atas).
  • Untuk keluar dari loop.
  • Digunakan sebagai bentuk goto yang "beradab".


Using break to exit a Loop

Using break, we can force immediate termination of a loop, bypassing the conditional expression and any remaining code in the body of the loop. Note: Break, when used inside a set of nested loops, will only break out of the innermost loop.

Dengan menggunakan break, kita dapat memaksa pemutusan loop dengan segera, melewati ekspresi kondisional dan kode yang tersisa di badan loop. Catatan: Break, ketika digunakan di dalam satu set loop bersarang, hanya akan keluar dari loop terdalam.



Exit.png

Contoh:

// Java program to illustrate using
// break to exit a loop
class BreakLoopDemo {
    public static void main(String args[])
    {
        // Initially loop is set to run from 0-9
        for (int i = 0; i < 10; i++) {
            // terminate loop when i is 5.
            if (i == 5)
                break;
 
            System.out.println("i: " + i);
        }
        System.out.println("Loop complete.");
    }
}

Output:

i: 0
i: 1
i: 2
i: 3
i: 4
Loop complete.


Using break as a Form of Goto

Java does not have a goto statement because it provides a way to branch in an arbitrary and unstructured manner. Java uses the label. A Label is used to identifies a block of code.

Java tidak memiliki pernyataan goto karena menyediakan cara untuk bercabang secara arbitrer dan tidak terstruktur. Java menggunakan label. Label digunakan untuk mengidentifikasi blok kode.


Syntax:

label:
{
  statement1;
  statement2;
  statement3;
  .
  .
}

Now, break statement can be use to jump out of target block. Note: You cannot break to any label which is not defined for an enclosing block.

Sekarang, pernyataan break dapat digunakan untuk melompat keluar dari blok target. Catatan: Anda tidak dapat memutuskan label apa pun yang tidak ditentukan untuk blok penutup.

Syntax:

break label;

Contoh:

// Java program to illustrate
// using break with goto
class BreakLabelDemo {
    public static void main(String args[])
    {
        boolean t = true;
 
    // label first
    first : {
       
    // Illegal statement here
    // as label second is not
    // introduced yet break second;
    second : {
    third : {
        // Before break
        System.out.println("Before the break statement");
 
        // break will take the control out of
        // second label
        if (t)
            break second;
        System.out.println("This won't execute.");
    }
        System.out.println("This won't execute.");
    }
 
        // First block
        System.out.println("This is after second block.");
    }
    }
}

Output:

Before the break statement.
This is after the second block.


Referensi