java study notes 3 flow control statements (sequence, selection, cycle), the control jumps statements

A flow control statement
during execution of a program, the order of execution of the statement of the program is the result of a direct impact. That flow of the program have a direct impact on operating results. So, we have to understand the flow of execution for each statement. And, many times we want to achieve our functions to be performed by the execution order control statements.
Classification: sequential structure, selection structure, loop structure;
1, the structure of the order
in the order of codes, in sequence.
2, selection structure (IF; Switch)
. 1), IF conditional statement
format:

if(比较表达式或者是boolean类型的值) {
			语句体;
		}

value of the result if the value of the expression in parentheses boolean types, the statement is executed if the body is true, false not performed
Note : a, else there is no back comparison expression, if only behind.
B, Comparison of expression whether simple or complex, the result must be a boolean
c, if the control statement is a statement if the statement body, braces can be omitted; if multiple statements, it can not be omitted. We recommend that you never omitted.
d, in general: there will be no opening brace semicolon, the semicolon there will be no opening brace
in other formats if conditionals:

if(比较表达式) {
			语句体1;
		}else {
			语句体2;
		}

Note: there is no comparison expressions else behind, and only if there is behind.

if(比较表达式1) {
		语句体1;
	}else if(比较表达式2) {
		语句体2;
	}else if(比较表达式3) {
		语句体3;
	}
	...
	else {
		语句体n+1;
	}
	**注意:当只要有一个条件满足时,if语句结束。else是可以省略,建议不要省略。**

2), switch statement
format:

switch(表达式){
			case 值1:
				语句体1;
				break;
			case 值2:
				语句体2;
				break;
			case 值3:
				语句体3;
				break;
			....
			default:	
				语句体n+1;
				break;
		}	

①, switch indicates that this is a switch statement; the value of the expression: byte, short, int, char ; after JDK5 can enumerate; after JDK7 can be String.
②, with the latter case is to compare the values of expressions and
③, seeing the body part may be one or more statements
④, break an interrupt, meaning the end, may be the end of the switch statement
⑤, default statement indicates that all cases are not when matched, on the implementation of the contents of the premises, and if else statements similar
process: first calculate the value of the expression, and then the back of the case match, if the match is successful on the implementation of the corresponding statement encounters a break will end. otherwise, execute the statement default control.
Note: ①, behind the case can only be a constant, not a variable, and, more than the back of the case can not appear the same value
②, default can be omitted, but is not recommended, because its role is to give tips on not correct .
③, break can be omitted, but there will be a phenomenon: case penetrate.
④, default does not have to be in any position in the final. But it is recommended in the final.
⑤, the end condition of the switch statement
Ⅰ: break is encountered ended
Ⅱ: execution to the end on the end

. 3), if, switch distinction
if statement for a boolean result of determination, and also a range of a few constants for determination value
switch statement can only be determined for several constant values
3, a cyclic structure (for; while; do ... while )
When the loop may be a case where the loop condition repeatedly execute a section of code, the code is repeated is referred to as loop statement, when this loop is repeatedly performed, it is necessary at the right time cycle is determined to modify the conditions false, thereby ending the cycle, otherwise the loop will always execute it, forming an endless loop
1), for loop
format: for (initialization expression statements; Analyzing conditional statement; statement control condition) {loop statement;}
execution process: a: Executive initialization expression statement
b: determination execution condition statement see the return value is true or false, if it is true, execution continues, if it is false, it ends the loop
c: perform loop statement;
D: execution control conditional statements
e: b back to continue.
For example: between 1 and output ~ 100

    int sum=0;
	for(int i=1;i<=100;i++){
	   sum+=i;
	}	
	System.out.println(sum);

2) while loop
format: initializing a conditional statement; while (judgment condition statement) {loop statement; controlling conditional statement;}
execution process:
A: performing initialization conditional statements;
B: execution judgment condition statement see the return value is true or false, if it is true, execution continues, if it is false, it ends the loop
c: perform loop statement;
D: execution control conditional statements
e: Back b continue.
For example: 1 to 100 and

int i=1,sum=0;
  while(i<=100){
    sum+=i;
    i++;
  }
  System.out.println(sum);

3), do ... while loop
Format: initializing conditional statements; do {loop statement; controlling conditional statement;} while (judgment condition statement);
execution process: a: performing initialization conditional statements;
B: execution loop statement;
C: conditional statement execution control;
D: performing the judgment condition statement see the return value is true or false
If true, execution continues
if it is false, the cycle ends
e: Back b continue.
Note: the loop body at least once in the do ... while statement.
Second, control jumps statements (BREAK, Continue, return)
. 1), BREAK: single exit loop
2), continue: one cycle out, the next step
3), return: a method exits. Jump to the upper method call.

Published 24 original articles · won praise 11 · views 2065

Guess you like

Origin blog.csdn.net/weixin_43791069/article/details/84782237