Câu lệnh điều khiển vòng lặp trong Java (break, continue)

Table of Contents

Trong bài trước, chúng ta đã tìm hiểu Vòng lặp trong Java. Giả sử trong một vòng lặp tại một điều kiện cụ thể nào đó, bạn muốn dừng thực thi hoặc tiếp tục thực thi vòng lặp đó, thì bạn sử dụng cách nào. Java hỗ trợ 2 lệnh break và continue giúp bạn điều khiển và kiểm soát vòng lặp.

1. break

Từ khóa break trong java được sử dụng để dừng thực thi lệnh trong vòng lặp hoặc trong mệnh đề switch tại điều kiện đã được chỉ định. Đối với vòng lặp bên trong vòng lặp khác, thì nó chỉ dừng vòng lặp bên trong đó.

Ví dụ sử dụng break với vòng lặp for:

package com.maixuanviet;

public class ForSample3 {
public static void main(String[] args) {
   for (int i = 1; i <= 5; i++) {
        if (i == 4) {
            break;
        }
        System.out.print(i + " ");
    }

}
}

Kết quả:

1 2 3

Ví dụ sử dụng break với vòng lặp bên trong vòng lặp for khác:

package com.maixuanviet;

public class ForSample4 {
public static void main(String[] args) {
 for (int i = 1; i <= 5; i++) {
        System.out.print(i + ": ");
        for (int j = 1; j <= 5; j++) {
            if (j == 4) {
                break;
            }
            System.out.print(j + " ");
        }
        System.out.println(); 
    }

}
}

Kết quả:

1: 1 2 3
2: 1 2 3
3: 1 2 3
4: 1 2 3
5: 1 2 3

2. continue

Từ khóa continue trong java được sử dụng để tiếp tục vòng lặp tại điều kiện đã được xác định, với điều kiện đó khối lệnh phía sau từ khóa continue sẽ không được thực thi. Đối với vòng lặp bên trong một vòng lặp khác, continue chỉ có tác dụng với vọng lặp bên trong đó.

Ví dụ sử dụng Continue trong java với vòng lặp for:

package com.maixuanviet;

public class ForSample5 {
public static void main(String[] args) {
 for (int i = 1; i <= 5; i++) {
        if (i == 4) {
            continue;
        }
        System.out.print(i + " ");
    }

}
}

Kết quả:

1 2 3 5

Ví dụ sử dụng Continue với vòng lặp bên trong vòng lặp for khác:

package com.maixuanviet;

public class ForSample6 {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
        System.out.print(i + ": ");
        for (int j = 1; j <= 5; j++) {
            if (j == 4) {
                continue;
            }
            System.out.print(j + " ");
        }
        System.out.println(); 
    }

}
}

Kết quả:

1: 1 2 3 5
2: 1 2 3 5
3: 1 2 3 5
4: 1 2 3 5
5: 1 2 3 5

Related posts:

Java Program to Find Number of Articulation points in a Graph
Java Program to Compute Discrete Fourier Transform Using the Fast Fourier Transform Approach
Guide to Java OutputStream
Adding Parameters to HttpClient Requests
Object cloning trong java
Overview of the java.util.concurrent
Java Byte Array to InputStream
Java Program to Find Transitive Closure of a Graph
Java Concurrency Interview Questions and Answers
Java Program to Implement Branch and Bound Method to Perform a Combinatorial Search
Spring Security OAuth2 – Simple Token Revocation
Java Program to Implement Skew Heap
Java Program to Construct an Expression Tree for an Postfix Expression
Java Program to Implement Sorted Circularly Singly Linked List
Spring Security 5 for Reactive Applications
An Intro to Spring Cloud Task
Getting Started with Custom Deserialization in Jackson
Comparing Dates in Java
Java Program to Implement the MD5 Algorithm
Hướng dẫn Java Design Pattern – Service Locator
Spring Boot - Exception Handling
Các chương trình minh họa sử dụng Cấu trúc điều khiển trong Java
Java Program to Implement LinkedBlockingQueue API
Spring 5 Testing with @EnabledIf Annotation
Map Serialization and Deserialization with Jackson
Java Program to Implement Self organizing List
How to Get a Name of a Method Being Executed?
Java Program for Topological Sorting in Graphs
Java Program to Perform LU Decomposition of any Matrix
New in Spring Security OAuth2 – Verify Claims
How to Get All Dates Between Two Dates?
Java Program to Find Shortest Path Between All Vertices Using Floyd-Warshall’s Algorithm