Tags

, , , ,

https://leanpub.com/u/sanjibsinha

In some cases like the previous examples, the ‘while’ loop is indispensable. The user input validation is also done by using ‘while’ loop. We can do it by applying the ‘if-else’ logic. But, that should not be as robust as the code below:

package fun.sanjibsinha.understandingloops;
/**
* how to force the user to give valid input
*/

import java.util.Scanner;

public class WhileThree {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);
System.out.println("Enter your age : between 0 and 150 ");
int age = sc.nextInt();
while (age < 0 || age > 150){
System.out.println("That is impossible; try again! ");
age = sc.nextInt();
}
System.out.println("Your are " + age + " years old.");
}
}

 

We assume that humans can live up to 150 years. Therefore, the range is set between 0 and 150. Now, we ask user to tell her age. If you see the output, you will understand how ‘while’ construct forces the user to give the true value.

// output of the above code
Enter your age : between 0 and 150 
-1
That is impossible; try again! 
1456
That is impossible; try again! 
258
That is impossible; try again! 
-45
That is impossible; try again! 
120
Your are 120 years old.

By this time, we have seen how judiciously we could use ‘while’ loop to solve different type of problems. But, in some cases, ‘do-while’ construct works better than ‘while’ construct. Let us see the next code snippet, where user is prompted to calculate the total price of number of items. The user gives the price of one item, then is asked to give the quantity, so the total price is calculated. With the help of ‘do-while’ construct, we give the user enough freedom to calculate as long as she wishes. To terminate the program, she just types ‘no’. Since Java is case sensitive, we have used the proper method to ignore the upper case or lower case.

package fun.sanjibsinha.understandingloops;

/**
* user can calculate total price more than once
*/

import java.util.Scanner;

public class DoWhileOne {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String answer = "";
do {
System.out.println("Enter the price :");
double price = sc.nextDouble();
System.out.println("Enter the quantity : ");
int quantity = sc.nextInt();
double totalPrice = price * quantity;
System.out.printf("The price of " + quantity + " pieces is $%.2f: ", totalPrice);
System.out.println();
System.out.println("Do you want to calculate more price? Yes or No.");
answer = sc.next();
} while (answer.equalsIgnoreCase("Yes"));
}
}

Here is the output:

// output of the above code
Enter the price :
1456.45
Enter the quantity : 
23
The price of 23 pieces is $33498.35: 
Do you want to calculate more price? Yes or No.
yes
Enter the price :
456.23
Enter the quantity : 
45789
The price of 45789 pieces is $20890315.47: 
Do you want to calculate more price? Yes or No.
no


Process finished with exit code 0

We have seen a few problems to have an idea how ‘while’ and ‘do-while’ constructs work. We will see more problems so that we could understand it properly.