Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java Loop Question You're going to sum up 1 + 2 + 3 + 4 + ... and stop when the sum is over 100.

Java Loop Question

image text in transcribedimage text in transcribed

You're going to sum up 1 + 2 + 3 + 4 + ... and stop when the sum is over 100. You will do this in one of two ways. The first you will use check to see if the sum is over 100, and if so, the loop will break. Here is the loop in SumToOneHundred.java: while(true) { sum += counter; counter++; } Obviously as you can see, it will never terminate, unless you place a condition that the look will break, and you will have to use the break keyword for this. Next, you will implement a while loop that does the same thing but WITHOUT the break statement. You will do this by having a condition in the while() statement that will cause it to terminate when the sum is over 100. 2 public class SumToOne Hundred { public static void main(String[] args) { int sum = 0; int counter = 0; /* first while loop */ while(true) { sum += counter; counter++; } // there should be an error here that says "unreachable code" // you need a condition that causes the while loop the exit so you // can reach this. System.out.println("Sum istim sum). 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 BO 31 82 33 34 35 36 } sum = 0; counter = 0; /* second while loop */ /* your while loops should look like this: * * * while (condition) { sum += counter; counter++; } * * * */ System.out.println("Sum is + sum); }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Relational Database Design With Microcomputer Applications

Authors: Glenn A. Jackson

1st Edition

0137718411, 978-0137718412

More Books

Students also viewed these Databases questions

Question

Which team solution is more likely to be pursued and why?

Answered: 1 week ago

Question

4. I can tell when team members dont mean what they say.

Answered: 1 week ago