import java.util.Scanner;
class WhileLoop
{
public static void main(String[] args)
{
int n;
Scanner input = new Scanner(System.in);
System.out.println("Input an integer");
while ((n = input.nextInt()) != 0)
{
System.out.println("You entered " + n);
System.out.println("Input an integer");
}
System.out.println("Out of loop");
}
}
Step-by-Step Explanation
1. Import the Scanner Class:
Import java.util.Scanner;
In the code, the Scanner class in java.util is imported to enable the user’s input into the program.
2. Class Declaration:
class WhileLoop {
The program consists of a declared class named WhileLoop containing the logic.
3. Declaration of main Method
public static void main(String[] args) {
It is the main entry of your program where execution starts.
4. Declare integer variable
int n;
Declare integer variable n to store integer input given by the user through the keyboard.
5. Scanner Object:
Scanner input = new Scanner(System.in);
A Scanner object called input is created to read the user’s input from the console.
6. Input from User:
System.out.println(“Input an integer”);
The program asks the user to input an integer.
7. Input Reader: While Loop to repeat reading the input until the user enters 0
while ((n = input.nextInt())!= 0)
{
System.out.println(“You entered ” + n);
System.out.println(“Input an integer”);
}
The while loop begins and runs as long as the user does not get 0 as the response.
At every iteration of the while loop, the program:
- Reads an integer using input.nextInt() and assigns it to n.
- If n is not 0, it prints the number entered and asks the user to enter another integer.
- The loop continues like that.
8. Exit the Loop:
When the user enters 0 the expression (n!= 0) will be false. Here the loop ends.
9. Print Exit Message:
System.out.println(“Out of loop”);
Once the program breaks out of the loop (if the user has entered 0), the application prints “Out of loop”. This is to indicate that the loop has been finished and the application is over.
Example Runs:
#Example 1: User Inputs a Series of Integers
Input:
Enter an integer
5
You entered 5
Enter an integer
7
You entered 7
Enter an integer
0
Execution:
- The program will read 5 and print “You entered 5”, then prompt for another input.
- The program reads 7 and prints “You entered 7”, then takes another input.
- Program reads 0 which triggers to end the loop and now it prints “Out of loop”.
Output:
Input an integer
You entered 5
Input an integer
You entered 7
Input an integer
Out of loop
#Example 2: User Entered Only 0
Input:
Input an integer
0
Execution:
When the user inputs 0, the program directly comes out of the loop and prints “Out of loop”.
Output:
Input the integer
Out of loop
Explanation of Execution Flow:
1. The program prompts for an integer.
2. It continues taking and displaying integer values given until it receives input 0.
3. At this point, the value 0 is inserted the while the loop is terminated from work, and the program displays “Out of loop” as a sign of its termination.
Conclusion:
This is a repeat input that accepts the user’s data and processes them. The while loop continues when other than 0 has been typed into the user interface. If one has typed in 0, the program goes out of the while loop, and the system signals the end of a session. This is good in cases where data collection must be an ongoing process but is stopped based on a given condition (for this example, inputting 0).