Study Materials - while
loops
Introduction
Keep in mind the following components of the while
loop when considering the sample problems below:
- point of entry (e.g. user’s response to the question “do you wish to begin?”;
- loop condition (e.g. if (response = = ‘Y’ || response = = ‘y’ );
- process (what the loop does if condition evaluates to true);
- point of return/point of exit (e.g. a repeat of item 1 above, allowing user to repeat loop process, or exit loop).
Sample programs using While loop structure:
Group 1
Group 1 examples include both a while
loop (allowing user to continue to enter values), an if
condition (testing for even, odd, positive, negative), and a counter (counting the number of the requested type).
- Write a JAVA program that will prompt the user to enter an unspecified (user-determined) number of integers. The program will output the number of even integers entered.
- Write a JAVA program that will prompt the user to enter an unspecified (user-determined) number of integers. The program will output the number of odd integers entered.
- Write a JAVA program that will prompt the user to enter an unspecified (user-determined) number of integers. The program will output the number of positive integers entered.
- Write a JAVA program that will prompt the user to enter an unspecified (user-determined) number of integers. The program will output the number of negative integers entered.
Group 2
In place of the counter, now the summing technique is used.
- Write a JAVA program that will prompt the user to enter an unspecified (user-determined) number of integers. The program will output the sum of all even integers entered.
- Write a JAVA program that will prompt the user to enter an unspecified (user-determined) number of integers. The program will output the sum of all odd integers entered.
- Write a JAVA program that will prompt the user to enter an unspecified (user-determined) number of integers. The program will output the sum of all positive integers entered.
- Write a JAVA program that will prompt the user to enter an unspecified (user-determined) number of integers. The program will output the sum of all negative integers entered.
- Write a JAVA program that will prompt the user to enter an unspecified (user-determined) number of integers. The program will output the sum of all integers entered.
Group 3
Group 3 examples use mechanisms similar to Group 1. Here the termination condition is specified. if
conditions are again required.
- Write a JAVA program that will prompt the user to enter an unspecified (user-determined) number of integers. The program will output the number of all integers entered whose value is less than 10.
- Write a JAVA program that will prompt the user to enter an unspecified (user-determined) number of integers, whose value is between 1 and 999. To terminate the program, the user is to enter the negative value -999. The program will output the number of all single-digit integers entered.
- Write a JAVA program that will prompt the user to enter an unspecified (user-determined) number of integers, whose value is between 1 and 999. To terminate the program, the user is to enter the negative value -999. The program will output the number of all double-digit integers entered.
- Write a JAVA program that will prompt the user to enter an unspecified (user-determined) number of integers, whose value is between 1 and 999. To terminate the program, the user is to enter the negative value -999. The program will output the number of all triple-digit integers entered.
Repeat Group 3 examples asking the user to enter only odd numbers, then only even numbers. This will require a while
loop within a while
loop. The external loop will prompt the user as to whether or not the user wishes to enter a value. The internal loop will prompt for a valid value (an even number, an odd number, etc.).