Java Learning notes---using flag bits to control loops

Source: Internet
Author: User

Using flag bits to control loops

The usual techniques for controlling loops are mentioned earlier: counter-controlled loops. Another common technique for controlling loops is to assign a special value when reading and processing the value of a collection. This particular input value also becomes the flag value (Sentinel values), Used to indicate the end of the loop. If a loop uses a flag value to control its execution, he is called the loop of the flag-bit control (sentinel-controlled loop).


In loop control, do not use floating-point values to compare values for equality. Because floating-point values are approximate values of certain values, using them can result in inaccurate loops and inaccurate results.

Input and output redirection
Input redirection refers to reading input from a file, not from the keyboard.
Output redirection refers to sending a transmission to a file, not to a screen.

Sentinevalue class code at the end of the text, the F:\workspace\Welcome\bin directory under the new Input.txt file, the contents are as follows:

 PackageWelcome;ImportJava.util.Scanner;/** used to read and calculate the sum of an indeterminate number of integers, input 0 indicates the end of the input*/ Public classSentinelvalue { Public Static voidMain (string[] args) {Scanner in=NewScanner (system.in); System.out.print ("Enter an integer that will exit if you enter 0:"); intdata =In.nextint (); intsum = 0;  while(Data! = 0) {sum= Sum +data; System.out.print ("Enter an integer that will exit if you enter 0:"); Data=In.nextint (); } System.out.println ("The sum is" +sum); }}

Java Learning notes---using flag bits to control loops

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.