While loop using break and continue Program in java

import java.util.Scanner;
class BreakContinueWhileLoop 
{
public static void main(String[] args) 
{
int n;
Scanner input = new Scanner(System.in);

while (true)
{
System.out.println("Input an integer"); 
n = input.nextInt();
if (n != 0) 
{
System.out.println("You entered " + n);
continue;
}
else 
{
break;
}
}
}
}

Code Breakdown

1. Scanner Class Importation:

This line imports the Scanner class, which is needed to read input from the user.

2. Class Definition:

class BreakContinueWhileLoop

This is the definition of the class called BreakContinueWhileLoop.

3. Main Method:

public static void main(String[] args)

This is the starting point of the Java program. This is where the execution of the program starts.

4. Variable Declaration:

int n;

Here, an integer variable n is declared. The value that is to be accepted from the user will be held in this variable.

5. Scanner Object Creation:

Scanner input = new Scanner(System.in);

This creates a Scanner object called input to read the user’s input from the console.

6. Infinite While Loop:

while (true)

This is an infinite while loop that will be running indefinitely because its condition true will always be met. The only way to leave the loop would be to programmatically break it.

7. Take User Input 

System.out.println(“Input an integer”);

This will print the message “Input an integer” to ask the user to enter a value.

8. Reading User Input:

n = input.nextInt();

This will read the integer value from the keyboard that the user entered and assign it to the variable n.

9. If Statement (Non-Zero Check):

 if (n!= 0)

 {

System.out.println(“You entered ” + n);

      continue;

 }

  • It checks if the input integer n is not equal to 0 that is, n != 0.
  • If n is not zero, it prints the message “You entered [n]” where [n] is the number entered by the user.
  • The continue statement is then executed. This causes the loop to jump back to the top immediately where it will prompt the user to input another, skipping the rest of the body of the loop.

10. Else Block (Zero Input):

else

{

       break;

 }

  • If the user inputs 0, the else block is run.
  • The break statement will be executed and the loop breaks and the program ends.

11. Program Termination

The program ends when the loop is broken by the break statement after the input of 0 from the user.

Example Walk Through

  • It will ask for an integer from the user.
  • If any non-zero integer is typed in by the user, then the program will print that integer and ask for another input to continue.
  • If the user types in 0, then the loop will terminate (because of break), and the program will terminate.

Output

Case 1:

Input an integer

5

You entered 5

Input an integer

10

You entered 10

Input an integer

0

Explanation of Output:

1. The user inputs 5. The program prints “You entered 5” and asks for a new input.

2. The user inputs 10. The program prints “You entered 10” and again asks for a new input.

3. Finally, the user inputs 0. The program breaks out of the loop and the program ends.

Case 2:

Input an integer

8

You entered 8

Input an integer

3

You entered 3

Input an integer

0

Explanation:

1. The user enters 8 and the program prints “You entered 8”.

2. The user enters 3 and the program prints “You entered 3”.

3. The user enters 0, and the loop breaks out since the break statement has been encountered.

Summary:

  • The program repeatedly prompts for an integer and prints it if it is non-zero.
  • The continue statement makes the program loop back for another input if a non-zero number is input.
  • The loop terminates if the user inputs 0 because of the break statement.

Leave a Comment

Your email address will not be published. Required fields are marked *