ch06-Loop WarmUps - Package Title Lab Manual Course Title...

Info icon This preview shows pages 1–4. Sign up to view the full content.

Package Title: Lab Manual Course Title: Big Java Chapter Number: 6 Loops Question type: Essay 1.1 1 ) Loops provide a mechanism for repeating a block of code called the loop body. We begin this lab by experimenting with while loops, the simplest form of loop code. Many loops are controlled with a single variable, which we will refer to as the loop control variable or the loop index. Consider the code below. What is the output the program produces? /** A simple program that prints a loop control variable. */ public class SimpleLoop { public static void main(String[] args) { int i = 0; int limit = 6; while (i < limit) { System.out.println("i = " + i); i++; } } } 1.2 2 ) Consider the code below again. What happens if you comment out the line that increments i ? Will the program ever stop looping? /** A simple program that prints a loop control variable. */ public class SimpleLoop { Lab Manual Chapter 4 11/30/16 1
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

public static void main(String[] args) { int i = 0; int limit = 6; while (i < limit) { System.out.println("i = " + i); i++; } } } Question type: Project 1.3) Manipulating the loop control variable is a critical skill in learning to write code with loops. Modify the program in Lab 4.1.1 so that it produces the following output: i = 6 i = 8 i = 10 i = 12 i = 14 i = 16 i = 18 ... i = 98 2.1 3. ) There is a famous story about a primary school teacher who wanted to occupy his students’ time by making the children compute the sum of 1 + 2 + 3 + … + 100 by hand. As the story goes, the teacher was astounded when one of the children immediately produced the correct answer: 5050 . The student, a child prodigy, was Carl Gauss, who grew up to be one of the most famous mathematicians of the eighteenth century. Repeat Gauss’s remarkable calculation by writing a loop that will compute and print the above sum. After you have the program working, rewrite it so you can compute 1 + 2 + … + n where n is any positive integer. Lab Manual Chapter 6 11/30/16 2
Image of page 2
2.2) Java provides three types of loops: while, for, and do (also called do-while). Theoretically, they are interchangeable – any program you write with one kind of loop could be rewritten using any of the other types of loops. As a practical matter, though, it is often the case that choosing the right kind of loop will make your code easier to produce, debug, and read. It takes time and experience to learn to make the best loop choice, so this is an exercise to give you some of that experience. Rewrite Lab 6.2.1 using a for loop. Repeat the exercise again but this time use a do while loop. Which form of loop seems to work best? Why? 3) Write a program that uses a while loop. In each iteration of the loop, prompt the user to enter a number – positive, negative, or zero. Keep a running total of the numbers the user enters and also keep a count of the number of entries the user makes. The program should stop whenever the user enters “q” to quit.
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

Image of page 4
This is the end of the preview. Sign up to access the rest of the document.
  • Fall '08
  • STAFF
  • Control flow, Euclidean algorithm, Lab manual chapter

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern