Java flow control statements

Key to controlling the flow of execution in Java are if-else, while-do-while, for, switch, return, break, continue, and so on; we said a few channels to pick it.

1, or life, or death

On the network to see such a sentence: " Life is but a three-way, or life, or death, or living death ", the classic ah! This sentence applied in if-elsevery apt statement on, do not believe you can see:

Only an if statement

if (布尔表达式) {
// 语句
}
复制代码

Or an if statement with an else statement

if (布尔表达式) {
// 语句
} else {
// 语句
}
复制代码

Or an if statement with a else ifstatement, coupled with an else statement

if (布尔表达式) {
// 语句
} else if (布尔表达式) {
// 语句
} else {
// 语句
}
复制代码

2, think clearly walk, or go ahead and say

and while do-whilestatements used to control the number of uncertain loop, except slightly different form than, do-whilebe more than once while performing (under the same conditions oh); in the following format:

while

while(布尔表达式) {
// 语句
}
复制代码

do-while

do {
// 语句
} while(布尔表达式)
复制代码

3, please use for determining the number of

There are two forms for loop, are as follows:

①, create an int variable (can take the initiative to specify the number of cycles, for example, can i < strs.length()be changed i < 3, so that only three cycles instead of four)

String [] strs = {"沉", "默", "王", "二"};
for(int i = 0; i < strs.length(); i++) {
    String str = strs[i];
}
复制代码

Explain a little: To initialize variables before the first cycle int i = 0, followed by a conditional test i < strs.length(), and then execute the statement String str = strs[i];, a "step" after the end of the statement i++.

②, commonly known as "foreach" form (not necessarily specified index element can be removed)

String [] strs = {"沉", "默", "王", "二"};
for (String str : strs) {
}
复制代码

Also explain a little: colon :declared variable of type String str before; colon :followed by an array of strs; at the time of execution of the loop, Java inside will turn out array strs each element, and then assigned to the variable str, until the last element .

4, you can switch the string

When the if-else conditions beyond three or more (looks a little bloated), may be used to replace a switch statement. In the form of a switch statement is as follows:

switch(condition) {
    case calue1 :
        // 语句
        break;
    case calue2 :
        // 语句
        break;
    case calue3 :
        // 语句
        break;
    default :
        // 语句
}
复制代码

Of particular note is the break key, the right places must not forget to use a switch statement! Otherwise, the next will trigger a switch statement branch case, but there is no ignoring the current branch break keyword.

From the beginning of Java SE 7, switch statement supports a string of conditions; before that, switch only supports constant expression of type char, byte, short or int, and enumeration constants.

5, Fibonacci number

Fibonacci number is almost every man learn programming are not open around the questions of a pen; Fibonacci column is the "Fibonacci" (great mathematician) in raising rabbits found very interesting when the number of columns :

  • The first month bunnies no ability to reproduce, so it is still one pair;
  • Two months later, she gave birth to a total of two pairs of total bunny;
  • Three months later, she gave birth to the old rabbit, bunnies because no ability to reproduce, so a total of three pairs;

...... and so on can be listed in the following table:

It was found that the ability of a good strong laws of nature, is there? How to use Java to achieve Fibonacci row it?

Law: first number = number + second third number, the number of second, third and fourth number = number, the third number = number + V fourth number, and so on

Flow control statements have not mastered this road Fibonacci number Fibonacci sequence of interview questions will be able to do it tested out. Specific code:

public class Fibonacci {
    public static void main(String[] args) {
        int start = 1;
        int next = 1;
        System.out.print(start + "、" + next + "、");
        // 从3开始到第num个斐波那契数
        for (int i = 3; i <= 12; i++) {
            int last = start + next;
            System.out.print(last + "、");
            start = next;
            next = last;
        }
        System.out.println();
        // 通过迭代计算,效率很低
        for (int i = 1; i <= 12; i++) {
            System.out.print(getFibonacci(i) + "、");
        }
    }

    private static int getFibonacci(int index) {
        if (index == 1 || index == 2) {
            return 1;
        }
        return getFibonacci(index - 1) + getFibonacci(index - 2);
    }
}
复制代码

Thinking 1: first specify the number of the first two, and then calculates the next number in the for loop, and a front with a number of replacements after.

Thinking 2: iteration is complete, the subscript is 1 or 2 returns 1 when the remaining number is equal to the previous number and a number of front and front.


Previous: kill people auto-increment, auto-decrement attractiveness

Next: the Java through what mechanisms to ensure that the object is initialized it?

Micro-channel search " silence the king, " public number, reply 'attention after the free video "get 500G Java high quality instructional videos (already categorized).

Guess you like

Origin juejin.im/post/5dc3bb0c5188257bbb3d26d2