Java While Loop
Java While Loop. In this article, i will go through how to incorporate a While loop into our sample program using the while loop in Java.
There are 2 kinds of While Loop
- While() – This will check the initial condition inside the parentheses before initiating the conditional statement.
- do{}While()- This will initiating the loop , and check the condition later after that.
To explain how to use DO While loop in Java , i will create a sample program , which enable the user to input a number from 0 to 9 to guess the Secret number.
Let Step into the Code below to incorporate Java While Loop into our sample program
In the sample code below, i will create 2 function . The first one is to check whether the User have input the value more than “i>10”. The second function is to print out a value to warn the User that they did not follow instruction and have input the value out of the threshold 0<i <10. I will skip exception handling in this sample,where if the user enter a text instead of an integer the system will return an error.
- First let create a function call overwrittten() to check whether the user input more than the given threshold.
- The function will overwrite the User input value , and return the value
- Second create a function which warn the User , that they have input a value more than the given threshold,This function will print out a message
- Now lets go back to our main and import the Scanner Library then create a new scanner object called “inputScanner”
- Create 2 variable , “i & checkpoint”. we will need the i to get the while loop moving
- Start our while loop by setting the condition while (i>=0 && i< 10 )
- Print out the message to ask the user to guess the number between 0 to 9
- Assign the User input from the object scanner to the variable checkInput
- Use the overwrittten(checkInput) function to check the User Input
- Use the warnUser(checkInput) to output a message warning the user whether they have key in the value within the required threshold.
- Now lets assign our secret number. Our secret number is 8.When the User input 8 ,it will break out of the while loop and display the message “You have enter the Correct number the secret number is 8”
- Voila , we have made it again . Just created our very first While loop in Java
package oop2; import java.util.Scanner; public class secretnumber { public static void main(String[] args) { Scanner inputScan1 =new Scanner(System.in); int i =0; int checkInput; while (i>=0 && i< 10 ){ System.out.println("Guess the Secret number from 0-9, "); checkInput=inputScan1.nextInt(); i=overwrittten(checkInput); System.out.println("you have enter "+checkInput); warnUser(checkInput); } else if (i==8){ break; } } System.out.println("You have enter the Correct number the secret number is 8"); inputScan1.close(); } public static int overwrittten(int userInput){ int overwritevalue=2; if( userInput>=10){ //Overwrite the Value overwritevalue =0; } else{ overwritevalue =userInput; } return overwritevalue; } public static void warnUser(int input){ if(input >=10){ System.out.println("Hey Follow Instruction 0 to 9 only"); } } }
Check out how to create constructor in Java here
Check out Java Library at Oracle here