Thinking in Java chapter knowledge control the flow of execution order.

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/tuzi007a/article/details/96474440

Keywords involved, if-else, while, do -while, for, return, break , and select statement switch, there is a similar goto jump.
A, if-else
where else is optional, and sometimes you do not need to write else.
Both forms of description:
. 1, IF (Boolean expressions)
{
execute statement;
}
2, IF (Boolean expressions)
{
execute statement;
}
the else
{
executing statements;
}
Here there is also a situation that else if statements. For example,
if (Boolean expression)
{
execute statement;
}
else if (Boolean expression)
{
execute statement;
}
/ * the else
{
executing statements;
} * /
Note, if a plurality of statements and multiple else if there is a difference .
Write multiple else if, else if only the above is false when the sentence will be carried out to determine the next else if's. If a else if once executed, else if others will not be executed.
When multiple if statements, no matter if one is true or false, if statement will be executed once each.

Second, the iteration statement, also called loop, also called traversal. (How comfortable on how to call it, anyway, are translated word.)
Keyword, while, do-while, for .
while format,
while (Boolean expression)
{
execute statement;
}
In particular,
while (to true)
{
}
is an infinite loop, since the determination result of the statement has been true.
Here the code book to an example, which has a
boolean result = Math.random () <0.99 ; mean value randomly generated double compared with 0.99, and then the Boolean result of the comparison is assigned to the result. This is the first random appearance. The method used here static random Math library, the role of which is to (but not including 1 including 0) to generate a double value between 0 and 1.

do-while format
do
execute the statement;
the while (boolean expression)

while the only difference is the do-while do-while statements will be executed at least once, even if the expression for the first time was calculated to be false. In a while loop structure, if the conditions for the first time on is false, which statement did not execute. In fact, do-while less common.
Three, for sentence
structure
for (initialization expression; Boolean expression; expression cycle)
{
execute statement;
}
above three expressions can be empty, the simplest method for the expression of an endless loop: for (;;) {}.
There is also a small statement for knowledge, is the comma operator, but only in the for statement. Format
for (1 initialization, initialize 2; 1 Boolean, Boolean 2; cycle 1 expression, the expression cycle 2)
{
the statement is executed;
}
is a plurality of variables may be defined in the for statement, these variables must be the same type. If int is int, long, if you are long and the like.
Four, foreach syntax. After the chapter speaks more, I do not write here.
Five, return
purposes: First, to specify a method of the return value (does not need to return the return value void inside), and second, leading to the current method exits and returns that value.
The
static int Test (testVal int, int target)
{
IF (testVal> target)
{
return. 1;
}
}
...
if there is no return statement in the method returns void, then there is an implicit return at the end of the process, so the method is not necessarily to have a return. However, if a method declaration will return anything other than the void, we must ensure that every code path will return a value.
Six, break and continue.
In any loop, the loop can break and continue with the process control. Wherein the break quits the loop that the remaining statements are not executed. Continue stops the execution of the current cycle, and then returned to the beginning of the cycle, the next cycle begins.
Such as,
for (int I = 0; I <10; I ++)
{
IF (I ==. 4)
BREAK;
IF (I == 0. 3%)
Continue;
System.out.println ( "I =" + I);
}
in this cycle, the value of i never reach 10, because when i 4, break statement breaks out of the loop. And continue to return to the place is the for loop statement at the beginning of i ++, so that i increment occurs.

Seven label
format tag
label 1: identifier followed by a colon.
This section focuses on: break, continue, break label, continue label.
Use tag object, we want another loop or nested loop switch statement.
This is because the break and continue keywords will normally interrupt only the current loop, but if used together with labels, they'll interrupt the cycle, where until the label is located.
①break break inner loop, outer loop back;
②continue moved back to the point of execution at the start of the inner loop (i ++ here);
③continue label and interrupt the inner loop and outer loop, go directly to the label at; then, it was a continuation of cycle, but from the beginning of the outer loop;
④break label also breaks all, and at the back label, but it does not re-enter the cycle. That is, it is actually completely terminated two cycles.
Here there is a good model,
class the labelFor
{
public static void main (String [] args)
{
int I = 0;
Outer:
for (; to true;)
{
Inner: // tag.
for (; I <10; I ++)
{
System.out.println ( "I =" + I);
IF (I == 2)
{
System.out.println(“continue”);
continue;
}
if(i==3)
{
System.out.println(“break”);
i++;

				break;
			}
			if(i==7)
			{
				System.out.println("continue outer");
				i++;
				
				continue outer;
			}
			if(i==8)
			{
				System.out.println("break outer");
				break outer;
			}
			for(int k=0;k<5;k++)
			{
				if(k == 3)
				{
					System.out.println("continue inner");
					continue inner;
				}
			}
		}
			
	}
}

}
Parsing step:
. 1, I = 0, enter for (; true;) {} The infinite loop. Here's inner: label.
2, then continue inside proceeds for (; i <10; i ++) in the cycle.
3, encountered output statement, i value of output. the println ( "I =" + I);
. 4, I = 0, continue to go down, found the following few if () statements are fault results, it enters in the third loop. for (int = 0 K; K <. 5; K ++) {}
. 5, the value of K to start the cycle, k = 0,1,2 are no corresponding output statement, when 3 k =, the output statement, the output continue inner .
6, continue to go down. Hit continue inner; based on its usage, immediately terminate the current cycle, back to the inner: label.
7, inner: // label.
for (; i <10; i ++) {}. Back here, i ++ expression began to enter the for loop, for i increment. At this time, I =. 1.
. 8, continues down, with steps 3 and 4.
9, i = 2, so that if (i == 2) the result is true, execute output statement. Output continue.
10, then down, continue to perform statement. Terminates the current cycle, this cycle retreated to the initial position, i.e., i ++ here. I for self-growth. Then. 3 = I.
. 11, I =. 3, then if (i == 3) determines that the statement is true, execute the output statement, the output break. And then do i ++; increment statement.
Further down, face break, and terminates the current loop, the outer loop back for (; true;) {} .
12, then the operation cycle i = 4,5,6, nothing special. With the Steps 5 and 6.
13, this time to i = 7, then if (i == 7) This statement evaluates to true. Perform output statements output continue outer. Then increment i ++. the value of i becomes 8.
14. Further down, the statement encountered continue outer; jumps to label outer: at. It is here. Outer:
for (; to true;) in the outermost loop.
15, i = 8, then if (i == 8) determines that the statement is true, execute the output statement, the output break outer.
16, then go down, encountered break outer; this statement. Mean the termination outer: tag all of the following cycle, a cycle back to the outside of the outer. But the outer: outside tag does not loop, so, here it interrupted all the loops. Program no longer continue. End.
These are the tags usage for the cycle. Similarly, while the label is also applicable. The same rules.
Eight, switch statements. Select statement.
Format:
Switch (expression that yields an integer value int or char type)
{
Case. . Integer value 1: execute statement; BREAK;
Case. . Integer value 2: execute statement; BREAK;
Case. . Integer value 3: execute statement; BREAK;
...
default: executing statements;
}
simple written code to do the demonstration.
Switch4_8 class
{
public static void main (String [] args)
{
0 = I int;
for (; I <. 5; I ++)
{
Switch (I)
{
Case. 3: System.out.println ( "I =" + I); BREAK;
Case 2: System.out.println ( "I = "+ I); BREAK;
Case 0: System.out.println (" I = "+ I); BREAK;
default: System.out.println (" I = "+ I);
}
}
}
}
minutia:
1, an integer value that follows the case not be the same. If the same, it will complain.
2, execution and break statements can not write.
3, switch expressions generated integer value is compared with the case in each of the following values, with or without break.
4, if the value found in case the generated value is a match, the corresponding statement is executed; if there is no match, the default statement is executed.
5, break off the equivalent of upper and lower case, do not write and write break break there is a difference.
Write break, if this case the statement is executed, it is cut off and the next case, it will not execute the next case in which the statement is executed. If you do not write the break, that is, upper and lower case of the implementation of the statement has not been cut off. If the case of the execution statement is executed, then regardless of the value of the next case are consistent with the values of this case will go to the next case statement execution of.

The book to a particular example, randomly generated letters, objects are created,
the Random the Random R & lt new new = (47);
for (int I = 0; I <100; I ++)
{
int = r.nextInt C (26 is) + ' a ';
...
}
Random.nextInt (26 is) is generated to give a value between 0-26. Including 0, excluding 26.
Note that the above had a 47 on the 47, 47 have not write this, the difference is still getting bigger.
Specific can see https://blog.csdn.net/zhang41228/article/details/77069734 this blog,
as well as on the difference between Math.random () and Random (), as described in this blog post. https://blog.csdn.net/yezhuAndroid/article/details/78647984 . Anyway, I looked pretty good writing, at least I regarded this white understood. Thanks to the two bloggers.
Finishing fourth chapter to end here.
the above.

Guess you like

Origin blog.csdn.net/tuzi007a/article/details/96474440