Slide 1
Slide 2
Slide 3

Label Continue in java programming (V41)

No Comments

Continue Label

        The continue statement is used inside a loop to skip the current iteration and move to the next statement.
        A labeled continue is a more advanced feature that allows you to jump to the next iteration of the outer loop, not just the innermost loop.

Syntax:

Source Code:

Copied!
public class ContinueLabel {
    public static void main(String[] args) {
        outerLoop:
        for (int i = 1; i <= 3; i++) {
            for (int j = 1; j <= 3; j++) {
                if (j == 2) {
                    continue outerLoop;
                }
                System.out.println("i = " + i + ", j = " + j);
            }
        }
    }
}

Result:

Source Code:

Copied!
import java.util.Scanner;

public class LoginAttempt {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int totalUsers = 3;
        int maxAttempts = 3;

        userLoop:
        for (int user = 1; user <= totalUsers; user++) {
            int attempts = 0;

            while (attempts < maxAttempts) {
                System.out.print("User " + user + ", attempt " + (attempts + 1) + ": Enter password: ");
                String input = scanner.nextLine();

                if (input.equals("banned")) {
                    System.out.println("User " + user + " entered a banned word. Skipping to next user.\n");
                    continue userLoop;
                }
                if (input.equals("secret")) {
                    System.out.println("Access granted to user " + user + "!\n");
                    continue userLoop;
                }
                System.out.println("Incorrect password.\n");
                attempts++;
            }
            System.out.println("User " + user + " failed all attempts.\n");
        }
        scanner.close();
    }
}

Result:

Watch the video:


Ebook: https://softkhpc.blogspot.com/2025/05/java-programming-ebooks.html

back to top