java-based - control flow statements

I. Introduction

Weekend sleep so comfortable, I do not want to move, and wanted Shuishui, the sun, or the life of toil came to write articles. The basis of a series of articles have been out to control the process, I feel also very fast, I did not have much confidence in the whole network series of articles I have written this basic series so well, easy for beginners to understand and complete knowledge can feel good article look at authors. The concept of control flow statements actually feeling inside is converted from an engineering flowchart over, such as non-selection, ah, ah like loop execution flow chart can be used to tell the story slightly! ! ! ! ! !

Two if - else statements

2.1 if -else

if (if) - else (otherwise) on the basis of the previous series ternary operator has mentioned a moment, if else how to understand it? Actually very simple, if the weather is good today, I wanted to go out in the sun, otherwise, I wanted to sleep in the bed. This sentence translated if - else follows:

 if (今天天气很好)
    出门晒晒太阳;
else
    睡在床上;   

From this example we can extract the if - else is structured as follows:

if (boolean-expression) {
    statement;
}else{
    statement
}

We then written in java code examples below

/**
 * @Author lsc
 * <p>知识追寻者,if else入门基础系列 </p>
 */
public class IfElse {

    public static void main(String[] args) {
        // 创建对象
        IfElse ifElse = new IfElse();
        // 示例true表示今天天气很好
        String weather = ifElse.weather(true);
        System.out.println(weather);//出门晒晒太阳
        // 示例 false表示今天天气不好
        String weather1 = ifElse.weather(false);
        System.out.println(weather1);//睡在床上

    }

    /* *
     * @Author lsc
     * <p> if else 使用入门</p>
     * @Param [weather 表示天气的好坏]
     * @Return java.lang.String 返回根据天气的好坏,想要做的事
     */
    public String weather(boolean weather){

        if (weather==true){
            return "出门晒晒太阳";
        }else {
            return "睡在床上";
        }
    }
}

2.2 if -else if-else

In addition if -else statement, it also supports if - else if - else, where else can not write (but not recommended logic is not very strict), before the middle of the express statement if else if not established, it is judged else if the condition, if it is to perform in an expression; else if in theory is unlimited; a simple example is as follows: If the hand is ice, put on gloves; if the feet are ice, put on cotton socks, if neck very cold, she wrapped a scarf, or go casual;

This example will be rewritten if -else if-else logic is as follows:

if (手很冰) {
    戴上手套;
}else if (脚很冰) {
    穿上棉袜;
} else if (脖子很冰) {
    裹上围巾;
}else {
    便装出门;
}

The above example is extracted as if -else if-else structure is as follows:

if (boolean- expression) {
    statement;
} else if  (boolean- express){
    statement;
} else if  (boolean- express){
    statement;
} 
..........
    else {
    statement;
}

The java code sample written as follows:

 public static void main(String[] args) {
        // 创建对象
        IfElse ifElse = new IfElse();
        // 1 表示手冷
        String keepWarm1 = ifElse.keepWarm(1);
        // 2 表示脚冷
        String keepWarm2 = ifElse.keepWarm(2);
        // 3 表示 脖子冷
        String keepWarm3 = ifElse.keepWarm(3);
        // 知识追寻者:[戴上手套][穿上袜子][裹上围巾]
        System.out.println("知识追寻者:"+keepWarm1+keepWarm2+keepWarm3);
    }

 /* *
     * @Author lsc
     * <p> 根据身体部分的是否冷,进行穿戴动作, </p>
     * @Param [whichCold 表示哪里冷]
     * @Return java.lang.String
     */
    public String keepWarm(int whichCold){
        // if 表达式中只有为真才会进入语句体
        if (whichCold==1){
            return "[戴上手套]";
        }else if(whichCold==2){
            return "[穿上袜子]";
        }else if (whichCold==3){
            return "[裹上围巾]";
        }else {
            return "便装出行";
        }
    }

Three switch statement

In fact, the switch statement with the if - else statement is somewhat similar, which is a multi-branch structure of statements, some books will be classified as a selection statement is also true, we will rewrite the above example as follows to switch logic

switch (哪里冷) {
        case 手冷 :
            戴上手套;
             break;
        case 脚冷:
            穿上袜子;
             break;
        case 脖子冷:
            裹上围巾;
             break;
        default :
            便装出行;           
}

The above extract the switch statement logic structure is as follows: case shows a case, expression and if the value for the statement executes statement, break out of the loop represents, expressed in a switch statement to the end switch will jump out of the switch; if each statement ignore the break (not an experienced programmer cry), the statement will execute until the end of the switch statement encounters a break statement will be the first, so keep in mind that each case statement beginners in learning when bring back the break statement

switch (expression) {
    case  value1 :
        statement;
        break;
    case value2;
        statement;
        break;
    .......................
    default :
        statement;      
}

The above example java code into the following:

/**
 * @Author lsc
 * <p> switch 语句 </p>
 */
public class SwitchZ {


    public static void main(String[] args) {
        // 创建对象
        SwitchZ switchZ = new SwitchZ();
        // 1 表示手冷
        String keepWarm1 = switchZ.keepWarm(1);
        // 2 表示脚冷
        String keepWarm2 = switchZ.keepWarm(2);
        // 3 表示 脖子冷
        String keepWarm3 = switchZ.keepWarm(3);
        // 知识追寻者:[戴上手套][穿上袜子][裹上围巾]
        System.out.println("知识追寻者:"+keepWarm1+keepWarm2+keepWarm3);
    }


    public String keepWarm(int whichCold){

        String keepWarm = "";
        switch (whichCold){

            case 1:
                keepWarm += "[戴上手套]";
                break;
            case 2:
                keepWarm +=  "[戴上手套]";
                break;
            case 3:
                keepWarm +=  "[裹上围巾]";
                break;
            default:
                keepWarm +=  "[便装出行]";
        }
        return keepWarm;
    }
}

Four loop

Loops name suggests Italian, is the expression for the loop will have to execute a statement if No body.

4.1 while statement

With a very interesting example to describe students: students did not write the job, the teacher to punish pupils copy "White Rabbit White Rabbit," a paper 20 times;

while (小学生抄了《小白兔小白兔》次数小于20次){
        小学生抄《小白兔小白兔》;
}

Which we will extract the while statement is structured as follows:

while (boolean-expression) {
        statement;
}

The example code written in java while statement is as follows:

    public static void main(String[] args) {
        // 表示罚抄的次数
        int number = 0;
        while (number<20){
            number++;
            System.out.println("小学生被老师罚抄《小白兔小白兔》次数:"+number);
        }
    }

Output:

小学生被老师罚抄《小白兔小白兔》次数:1
小学生被老师罚抄《小白兔小白兔》次数:2
小学生被老师罚抄《小白兔小白兔》次数:3
小学生被老师罚抄《小白兔小白兔》次数:4
小学生被老师罚抄《小白兔小白兔》次数:5
小学生被老师罚抄《小白兔小白兔》次数:6
小学生被老师罚抄《小白兔小白兔》次数:7
小学生被老师罚抄《小白兔小白兔》次数:8
小学生被老师罚抄《小白兔小白兔》次数:9
小学生被老师罚抄《小白兔小白兔》次数:10
小学生被老师罚抄《小白兔小白兔》次数:11
小学生被老师罚抄《小白兔小白兔》次数:12
小学生被老师罚抄《小白兔小白兔》次数:13
小学生被老师罚抄《小白兔小白兔》次数:14
小学生被老师罚抄《小白兔小白兔》次数:15
小学生被老师罚抄《小白兔小白兔》次数:16
小学生被老师罚抄《小白兔小白兔》次数:17
小学生被老师罚抄《小白兔小白兔》次数:18
小学生被老师罚抄《小白兔小白兔》次数:19
小学生被老师罚抄《小白兔小白兔》次数:20

4.2 do while statement

The biggest difference between the do- while statement and the while statement is that no matter whether the expression is true, do statement will be executed once the body;

do - while the following structure:

do {
    statement;
} while (boolean-expression)

The sample code is written as java do-while statement is as follows:

 public static void main(String[] args) {
        // 表示罚抄的次数
        int number = 0;
        do {
            number++;
            System.out.println("小学生被老师罚抄《小白兔小白兔》次数:"+number);
        } while (number<20);
    }

4.3 for loop

for drawing out the recycling process can be very appropriate! Interested readers can use a for loop structure drawing out the process;

for sentence structure is as follows:

for ( Initial value; boelean-exepression; step ) {
    statement;
}

The example code for java written statement is as follows:

public static void main(String[] args) {

        for (int number =0; number<20; number++){
            System.out.println("小学生被老师罚抄《小白兔小白兔》次数:"+number);
        }
    }

Note that the output will be 0 ....... 19; which performs the steps of determining, 0 = number can be seen only when the variable initialization is performed once; will be later determined number is smaller than 20, the loop is executed, then adding a number value, is repeatedly executed until the number is 20 when the exit;

  1. numer = 0;
  2. number <20 established
  3. Console Print
  4. number +1
  5. number <20 established
  6. Console Print
  7. ..............
  8. Until the end of number = 20;

4.4 Advanced for for-each loop

java5 advanced feature is provided for a new cycle, set in this way for iterative array may not be performed according to standard procedures for the loop; statement can be seen from the following number is int each array, the number is of type int ; when the statement is executed will get value from an int array numberArray assigning the number, if the value of numberArray taken away once, then the index array index is increased by one, until the array is traversed completed; give you a word: continue the request will only exploit, when sustainable development, continue to pull out of wool;

public static void main(String[] args) {
        // int数组,如果是初学者请留个印象,不必深究
        int[] numberArray = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};

        for (int number : numberArray) {
            System.out.println("小学生被老师罚抄《小白兔小白兔》次数:"+number);
        }
    }

4.5 infinite loop

Means infinite loop will always execute the loop body, this is a very memory-intensive operation; the code below schoolchildren would have been fined copy, copying until exhausted, and even death, ah very poor. Think about this program execution is not also, like people, will continue to consume memory, the last memory Ben collapse, downtime. With this cycle of death to give you a summary of words: the world is useless eternal promise, but patience is not completely spend nothing; so life in multi-tolerant, live better points, Mo insist!

for example infinite loop:

 public static void main(String[] args) {
        for (;;){
            System.out.println("小学生被老师罚抄《小白兔小白兔》");
        }
    }

while infinite loop example:

public static void main(String[] args) {
        while (true){
            System.out.println("小学生被老师罚抄《小白兔小白兔》");
        }
    }

Five continue statement

continue statement commonly used in a loop, circulation means skip time;

Example:

public static void main(String[] args) {
        // int数组,如果是初学者请留个印象,不必深究
        int[] numberArray = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};

        for (int number : numberArray) {
            // 如果 number 小于 15 将每次都会跳过循环
            if (number<15){
                continue;
            }
            System.out.println("小学生被老师罚抄《小白兔小白兔》次数:"+number);
        }
    }

Execution results are as follows:

小学生被老师罚抄《小白兔小白兔》次数:15
小学生被老师罚抄《小白兔小白兔》次数:16
小学生被老师罚抄《小白兔小白兔》次数:17
小学生被老师罚抄《小白兔小白兔》次数:18
小学生被老师罚抄《小白兔小白兔》次数:19
小学生被老师罚抄《小白兔小白兔》次数:20

Six breek statement

break statement indicates that out of the loop, with a lot of books written articles is to terminate the loop is actually not accurate enough, I prefer to call it breaks out of the loop away from it;

 public static void main(String[] args) {
         // 表示罚抄次数
         int number = 0;
        while (true){
            number++;
            if (number>5){
                // 跳出循环
                break;
            }
            System.out.println("小学生被老师罚抄《小白兔小白兔》次数:"+number);
        }
    }

Seven summary

Benpian control flow statements can be said to be very basic, very suitable for beginners to learn, usage of which continue, break statement as well as the label, it will jump to the circular position of the label rather than out of the recent cycle, using a multi-scene Skip outer layers of nested loop case, I want to study in depth the reader can study on their own. Beginners can write a Fibonacci columns based on knowledge of learning;

Here Insert Picture Description

Guess you like

Origin www.cnblogs.com/zszxz/p/12058041.html