Java BASIC program Structure design control Flow

Source: Internet
Author: User

Control process

Java's control process is basically the same as C and C + +, except that goto statements can be used, but break statements may be tagged to achieve the purpose of jumping out of the inner layer. Tags can be placed in front of for or while. As follows:

  1. Package COM.ZJF;
  2. Import Java.io.Console;
  3. Import Java.util.Scanner;
  4. public class Test {
  5. Public static void Main (string[] args) {
  6. Outer: for (int i=0;i<5;i++) {
  7. for (int j=0;j<5;j++) {
  8. System.out.println ("i=" +i+ "j=" +j);
  9. if (j==3) {
  10. Break outer;
  11. }
  12. }
  13. }
  14. }
  15. }
Block scope:

Java is a block scope, and this is not the same as JS.

While loop

There are two types of while loops:

while ()

{

Do some thing

}

And

Do

{

Do some thing

}

while ()

For loop

for (initialize; condition to continue looping, update)

However, it is not recommended to have complex logic in a for loop, preferably a simple logic that determines the initialization and size of variables.

If you use floating-point numbers for detection in a loop, the result may be a dead loop. The following notation is a dead loop.

    1. Package COM.ZJF;
    2. public class Test {
    3. Public static void Main (string[] args) {
    4. For (double d = 0 D; d! = 1d;d+=0.1d)
    5. {
    6. System.out.println (d);
    7. }
    8. }
    9. }

Let's look at the output in a different way:

    1. Package COM.ZJF;
    2. public class Test {
    3. Public static void Main (string[] args) {
    4. For (double d = 0 D; d < 1d;d+=0.1d)
    5. {
    6. System.out.println (d);
    7. }
    8. }
    9. }

Output:

0.0

0.1

0.2

0.30000000000000004

0.4

0.5

0.6

0.7

0.7999999999999999

0.8999999999999999

0.9999999999999999

Multiple selection: switch statement

The switch statement can only work on shaping and enumeration. You cannot test a string, and you cannot test floating-point types.

Multiple case in a switch statement cannot be duplicated, so only one can be satisfied, and more than one cannot be satisfied. But if you don't add a break after each case, you'll go on to default. As follows:

  1. Package COM.ZJF;
  2. public class Test {
  3. Public static void Main (string[] args) {
  4. int i = 3;
  5. switch (i) {
  6. Case 1:
  7. SYSTEM.OUT.PRINTLN (1);
  8. Case 2:
  9. SYSTEM.OUT.PRINTLN (2);
  10. Case 3:
  11. SYSTEM.OUT.PRINTLN (3);
  12. Default:
  13. System.out.println ("default");
  14. }
  15. }
  16. }

Results:

3

Default

The author says in the book that it is possible to trigger multiple case branches, and if the statement does not add a break, then it will proceed to the next one, so try not to use break. There is no mistake in saying this, it is possible to trigger multiple branches, but the syntax requires that the key of the case cannot be duplicated, otherwise it will compile an error. So what is the purpose of Java's design, I guess, if you don't add break, every case goes to default, so you can add some common logic to the default.

Java BASIC program Structure design control Flow

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.