Try the fastest way to create flashcards
Question

What is the keyword break for? What is the keyword continue for? Will the following programs terminate? If so, give the output.

int balance = 10;
while (true) {
    if (balance < 9)
       break;
    balance = balance - 9;
}
System.out.println("Balance is " + balance);
int balance = 10;
while (true) {
    if (balance < 9)
        continue;
    balance = balance - 9;
}
System.out.println("Balance is " + balance);

Solution

Verified
Answered 2 years ago
Answered 2 years ago
Step 1
1 of 2

Keyword break\textbf {break} is meant to be used when the programmer wants to terminate a loop when a certain condition is met.

Keyword continue\textbf {continue} is used to go to the next iteration without executing the rest of the loop's body.

The first program will be terminated with the break keyword and the output will be "Balance is 1"\textbf{"Balance is 1"}, while the second program will never be terminated and its println statement is unreachable.

Create a free account to view solutions

Create a free account to view solutions

Recommended textbook solutions

Introduction to Java Programming, Brief 8th Edition by Y. Daniel Liang

Introduction to Java Programming, Brief

8th EditionISBN: 9780132130790 (1 more)Y. Daniel Liang
500 solutions
Fundamentals of Database Systems 7th Edition by Ramez Elmasri, Shamkant B. Navathe

Fundamentals of Database Systems

7th EditionISBN: 9780133970777Ramez Elmasri, Shamkant B. Navathe
961 solutions
Introduction to Algorithms 3rd Edition by Charles E. Leiserson, Clifford Stein, Ronald L. Rivest, Thomas H. Cormen

Introduction to Algorithms

3rd EditionISBN: 9780262033848Charles E. Leiserson, Clifford Stein, Ronald L. Rivest, Thomas H. Cormen
872 solutions
Introduction to Algorithms 4th Edition by Charles E. Leiserson, Clifford Stein, Ronald L. Rivest, Thomas H. Cormen

Introduction to Algorithms

4th EditionISBN: 9780262046305Charles E. Leiserson, Clifford Stein, Ronald L. Rivest, Thomas H. Cormen
945 solutions

More related questions

1/4

1/7