Question: Lab Goal : This lab was designed to demonstrate the similarities and differences in a for loop and a while loop. Lab Description : Write

Lab Goal : This lab was designed to demonstrate the similarities and differences in a for loop and a while loop.

Lab Description : Write a for loop that accomplishes the same goal as a while loop and write a while loop that accomplishes the same goal as a for loop. Finally, write a loop your way: write either a for loop or a while loop that produces the appropriate output.

Sample Output :

***** While Loop String Cleaner **** I am Sam I am with the letter a removed by a while loop is I m Sm I m ***** For Loop String Cleaner **** I am Sam I am with the letter a removed by a for loop is I m Sm I m ***** For Loop Common Divisor **** The for loop determined the common divisors of 528 and 60 are 12 6 4 3 2 ***** For Loop String Cleaner **** The while loop determined the common divisors of 528 and 60 are 12 6 4 3 2 ***** My Total Loop My Way **** The total of even numbers from 1 to 1000 using a for loop is 250500 The total of even numbers from 1 to 1000 using a while loop is 250500

Java Shell:

//(c) A+ Computer Science //www.apluscompsci.com //Name -

import static java.lang.System.*;

public class WhileOrFor { public static void main( String args[] ) { //while loop out.println(" ***** While Loop String Cleaner **** "); String original="I am Sam I am"; char lookFor = 'a'; String cleaned = original; out.print(original + " with the letter " + lookFor + " removed by a while loop is "); int loc = cleaned.indexOf(lookFor); while(loc> -1) { cleaned = cleaned.substring(0, loc) + cleaned.substring(loc + 1); loc = cleaned.indexOf(lookFor); } out.println(cleaned); out.println(); out.println(" ***** For Loop String Cleaner **** "); // rewrite the while loop above as a for loop below here //for loop out.println(" ***** For Loop Common Divisor **** "); int one = 528; int two = 60; out.println("The for loop determined the common divisors of " + one + " and " + two + " are "); for(int i = one; i > 1; i--) { if(one % i == 0 ) if (two % i == 0) out.print(i + " "); } out.println(); out.println(); out.println(" ***** For Loop String Cleaner **** "); // rewrite the for loop above as a while loop below here out.println(" ***** My Total Loop My Way **** "); // write a loop (for or while, your choice) that totals even numbers from 1 to 1000 and prints only the total } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!