Explain break statement in a loop, JAVA Programming

Assignment Help:

Explain break statement in a loop ?

A break statement exits a loop before an entry condition fails. For instance, in this variation on the CountWheat program an error message is printed, and you break out of the for loop if j becomes negative.

class CountWheat  {
   public static void main (String args[]) {
      int total = 0;
     for (int square=1, grains = 1; square <= 64; square++) {
      grains *= 2;
      if (grains <= 0) {
        System.out.println("Error: Overflow");
        break;
      }
      total += grains;
      System.out.print(total + "\t  ");
      if (square % 4 == 0) System.out.println();
    } 
    System.out.println("All done!");
   }
 }

Here's the output:

% javac CountWheat.java
% java CountWheat
2            6           14              30
62           126         254             510
1022         2046        4094            8190
16382        32766       65534           131070
262142       524286      1048574         2097150
4194302      8388606     16777214        33554430
67108862     134217726   268435454       536870910
1073741822   2147483646      Error: Overflow
All done!

The most general use of break is in switch statements.


Related Discussions:- Explain break statement in a loop

Java, Write a program called Power that displays the positive powers of 2. ...

Write a program called Power that displays the positive powers of 2. When the user enters the exponent at a prompt, the program displays 2 to that power. The program halts when the

IRWS AND CPP, There is this solution for cpp assignment code EM132495039, i...

There is this solution for cpp assignment code EM132495039, i need solution of this and another assignment as well kindly guide me

Arrays, Prompt the user for an int between an upper and lower boundary. Reu...

Prompt the user for an int between an upper and lower boundary. Reuse the validateInput() method from project 2 to validate if the input is in bounds. If it is not, print an error

What is jsf life cycle and its phases, The series of steps followed by an a...

The series of steps followed by an application is known its life cycle. A JSF application typically follows six steps in its life. 1. Restore view phase 2. Apply request valu

Dijkstra''s algorithm, How can I convert Dijkstra''s algorithm to do max fl...

How can I convert Dijkstra''s algorithm to do max flow instead of min flow, and how can I implement it into Ford Fulkerson algorithm?

Hotel online booking system, who can helps me with pseudocode with that top...

who can helps me with pseudocode with that topic above mencioned

Project, Hey, i have a project on java can you help me

Hey, i have a project on java can you help me

Java input and output, what are all possible ways in java for user input an...

what are all possible ways in java for user input and output

Program, Develop a code for fibonacci series

Develop a code for fibonacci series

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd