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

How many bits are used to show unicode and ascii, How many bits are used to...

How many bits are used to show Unicode, ASCII, UTF-16, and UTF-8 characters? Unicode requires 16 bits and ASCII require 7 bits although the ASCII character set uses only 7 bits

What is the difference between SOA and a Web Service, Normal 0 ...

Normal 0 false false false EN-US X-NONE X-NONE

Explain JMS, JMS ((java messaging service))is an acronym used for Java Mess...

JMS ((java messaging service))is an acronym used for Java Messaging Service. It is Java's answer to formed software using asynchronous messaging. It is one of the official specific

La food, The fancy new French restaurant La Food is very popular for its au...

The fancy new French restaurant La Food is very popular for its authentic cuisine and high prices. This restaurant does not take reservations. To help improve the efficiency of the

Describe in brief about the polymorphism, Describe Polymorphism? Polymo...

Describe Polymorphism? Polymorphism can be referred as one name many forms. It's the ability of methods to behave differently, depending upon object who is calling it. Key feat

Write complete application to play game tic-tac-toe, (Tic-Tac-Toe)  Create...

(Tic-Tac-Toe)  Create class TicTacToe that will enable you to write a complete application to play the game of Tic-Tac-Toe. The class contains a private 3-by-3 rectangular array o

Difference between durable and non-durable subscriptions, Point-To-Point (P...

Point-To-Point (PTP). This model permits exchanging messages via queues formed for some purposes. A client can send and receive messages from one or various queues. PTP model is ea

How can we involve images within a web page, How can we involve images with...

How can we involve images within a web page using HTML and Java script? Images can be included in HTML through employing tag, below is the format of

Bluej program, program to find the frequency of a digit in a number

program to find the frequency of a digit in a number

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