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

Introduction, the multiple of two number in java

the multiple of two number in java

Need android geolocation service app, Need Android GeoLocation Service App ...

Need Android GeoLocation Service App Project Description: I want an android service app that will run on the background. The app has to get the users location (latitude an

Minimumself, At a shop of marbles, packs of marbles are prepared. Packets a...

At a shop of marbles, packs of marbles are prepared. Packets are named A, B, C, D, E …….. All packets are kept in a VERTICAL SHELF in random order. Any numbers of packets with thes

I need android kiosk mode, I need Android Kiosk Mode Project Description...

I need Android Kiosk Mode Project Description: I need an Android platform developer to develop an app when device start it start into kiosk mode. Only browser window open a c

Difference between static and non-static variable, What is the difference b...

What is the difference between static and non-static variables? A static variable is associated with the class as a whole rather than with specific instances of a class. Non-st

Explain the term array and scope of variable, Explain the term array and Sc...

Explain the term array and Scope of Variable? Defining the space in that a variable is effective is known as describing the scope of a variable. A variable can be either local

Program execution of boggle game application, In this assignment, you will ...

In this assignment, you will implement the game of Boggle, a word game in which players connect adjacent letters on a 4x4 board to create as many words as possible. This is based o

I need the app to be developed for android, I have a very simple app that I...

I have a very simple app that I have developed and is in the AppStore. I need the app to be developed for Android as it is presently in Objective C only. All plist html/ lists s

Difference among microprocessors and microcontrollers, What is the differen...

What is the difference among microprocessors and microcontrollers? Give one example of both? The microprocessor is electronic equipment that can perform various functions and a

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