IT Band of Brothers Java Grammar loop flow control statements sentence structure 1

Loops may be a case where the loop condition is repeatedly executed at a point code, which is repeated is referred to as loop, when the loop is repeatedly performed, it is necessary at the right time to the loop condition is false, thus ending the cycle, otherwise the loop will always execute it, forming an endless loop. Loop might contain the following four parts:

1. Initialization statements: a statement or statements, these statements are used to perform some initialization, initialization statements executed before the cycle begins.

2. Cycling conditions: This is a boolean expression that can decide whether to execute the loop body.

3. Loop: This is the main part of the cycle, the cycle if conditions permit, this block will be repeatedly performed. If the block is only one line statement, then the curly braces code block can be omitted.

4. Iteration statement: This section performed in a loop after performing evaluate a condition before the loop, the control loop is typically used to condition variables, such that the end of the cycle at the right time.

The above four parts just general classification, not every cycle is very clear separation of the four sections.

 

1 for loop

The for loop is relatively simple loop, in most cases, may be substituted for loop while loop, do-while loop. The complete syntax for loop as follows:

for (initialization statement; loop condition; iteration statement) {

    Loop body;

}

When the program is executed for loop, the first execution of the loop initialization statement initialization statement is executed only once before the loop begins. Before each iteration of the loop, first calculate the value of the loop condition, if the loop condition returns true, the loop body, after the end of the loop execution loop iteration statement is executed. Thus, for a for loop, the loop condition is better than the loop body to be executed more than once, because the last time through the loop returns false conditions, will not perform the loop.

It is noteworthy that, for the loop iteration loop statement does not put together with the body of the loop, so local experience continue even while the body of the loop end of this loop, loop iterations statement the same will be implemented.

If the loop is only one line statement, the loop body braces may be omitted:

for (initialization statement; loop condition; iteration statement) loop body;

Example: The following program demonstrates the flow of execution for loop:

public class ForDemo{

    public static void main(String[] args){

         for(int i = 0;i < 10; i++){

              System.out.println ( "i values:" + i);

         }

    }

}

Compile and run this program, the console 10 displays information as shown in FIG.

f9ba5d28fd0a45d8a0afa2eb2e6f5fbc.png

FIG 10 ForDemo operating results

 

In the above loop, loop initialization statement for only one cycle conditions just a simple boolean expression, in fact, at the same time allowing for the development of a plurality of loop initialization statement, a loop condition may comprise logical operators expression, as follows:

public class ForDemo{

    public static void main(String[] args){

         for(int b = 0,s = 0,p = 0;b < 10 && s< 4 && p < 10;p++){

              System.out.println(b++);

              System.out.println(++s + p);

         }

    }

}

The above code to initialize variables there are three, but only one declaration, so if you need to declare more than one variable in the initialization expression, then these variables should have the same data type.

Further, there are some interesting for loop variants, it is defined by some part of the cycle not achieved. In Java, all or part of the initialization, the conditions for partially or iterative loop can be empty, as follows:

public class ForDemo{

    public static void main(String[] args){

         int i;

         for(i = 0; i < 10;){

              System.out.println ( "i values:" + i);

              i++;

         }

    }

}

Compile and run this program, the console displays information such as shown in FIG. 11.

f1b909919991458aa488d270e78ea49b.png

ForDemo operation result of FIG. 11 modified

 

Here, there is no loop iterations for expression, and increasing the loop control variable i is placed inside the loop body. This means that every time the cycle should test whether i is equal to 10, except that no further action. Of course, because of the increased i inside the loop, so the loop can be normal.

下面在看for循环的另一个变体,在下面的例子中,for循环的初始化部分也被移出了for循环:

public class ForDemo{

    public static void main(String[] args){

         int i = 0;

         for( ; i < 10; ){

              System.out.println("i的值为:" + i);

              i++;

         }

    }

}

重新编译并运行这个程序,控制台将显示如图12所示的信息。

在此版本中,在循环开始前就把i初始化了,没有把它作为for循环的一部分。一般情况下都是把循环控制变量的初始化放在for循环内部,但是当初始值是通过某个复杂过程得到的,而又不方便把这一过程放在for语句中时,可以把初始化放在循环之外。

1f5575d273a349c99f6443e3303309ef.png

图12  再次修改后的ForDemo运行结果

 

for循环还有另外一种变体就是将循环的条件表达式设为空,可以生成无限循环,即永远不会停止的循环。例如下面的代码就是Java程序创建无限循环的常用方法:

for( ; ; ){

    //...

}

该循环将永远运行下去,尽管有许多程序设计任务,如操作系统命令处理器,需要无限循环,但是多数“无限循环”都是具有特殊终止条件的循环。

在Java中,for循环(或其他循环)可以没有循环体,因为空语句在语法上是有效的。没有循环体的循环十分有用。例如,下面的程序就是用了没有循环体的循环,计算从1至5的数之和:

public class ForDemo2{

    public static void main(String[] args){

         int sum = 0;

         for(int i = 1; i <= 5; sum += i++);

         System.out.println("1至5的和为:" + n);

    }

}

编译并运行这个程序,控制台将显示如图13所示的信息。

Note that, the whole process is done in the summation for statement, no loop. Here we must pay special attention to the iterative expression:

sum += i++;

99d847df91c5417691cf75d4a9d39e9e.png

FIG 13 ForDemo2 operating results

Guess you like

Origin www.cnblogs.com/itxdl/p/11261990.html