While, Do-while loop in Java
Activity Outcomes:
This lab teaches you the following topics:
while loop
do-while loop
Introduction:
Java provides different types of loop statements including while and do-while statements. Although these loop statements can be used as alternatives of for loop but are preferred to be used when the number of iterations are not known in advance.
while loop
This is also called as pre-test loop where a condition is tested first before the body of the loop is executed. The body of the loop is executed as long as the condition followed by the while statement remains true. The syntax of while loop is as below.
while (condition)
{
statement1;
statement2;
…
…
}
As you can see in the above syntax, a condition is followed by the while statement. This condition must remain true for the execution of the loop. As long as the condition will remain true, the loop will be iterated and will stop when the condition becomes false.
do-while loop
This is also called post-test loop where the condition is tested once the body of the loop is executed. If the condition is true then the body of the loop is executed again and this process continues until the condition becomes false. It means that the body of the loop will be executed at least once whether the condition is true or false as the body is
executed first and then the condition is checked. The syntax of do-while loop is as below.
do
{
statement1;
statement2;
…
…
}while (condition);
Lab Activities:
Activity 1:
Calculate the sum of all the values between 0-10 using while loop,
Solution:
Create a new Java file and type the following code.
Run the code by pressing F5.
You will get the following output.
Activity 2:
Repeat the above code by accepting 5 integer values from user and display the sum of given values.
Solution:
Create a new Java file and type the following code.
Run the code by pressing F5.
You will get the following output.
Activity 3:
Rewrite the above code to keep accepting integer values from user until 0 is entered. Display sum of the given values.
Solution:
Create a new Java file and type the following code.
Run the code by pressing F5.
You will get the following output.
Activity 4:
Rewrite the above code using do-while loop.
Solution:
Create a new Java file and type the following code.
Run the code by pressing F5.
You will get the following output.
Activity 5:
Write a Java program to accept two integer values from user and calculate their addition, subtraction, multiplication and division depending on the given choice. Keep accepting the values until a choice related to quit the process is selected.
Solution:
Create a new Java file and type the following code.
Run the code by pressing F5.
You will get the following output.
Home Activities:
Activity 1:
Write a Java program that prompts the user to enter an answer for a question on addition of two single digits and let the user repeatedly enter a new answer until it is correct as below.
Activity 2:
Guessing Numbers—The problem is to guess what number a computer has in mind. You will write a program that randomly generates an integer between 0 and 100, inclusive. The program prompts the user to enter a number continuously until the number matches the randomly generated number. For each user input, the program tells the user whether the input is too low or too high, so the user can make the next guess intelligently. Here is a sample run:
Activity 3:
Write a Java program to keep accepting integer values from user until a prime number is entered.
Activity 4:
Write a Java program to keep accepting marks and names of students until a negative number is entered against marks. Display name of the student having maximum marks.