Process control structures and methods

  • 2020-04-01 01:23:03
  • OfStack

Program flow control structure and method
Program flow control structure is divided into: sequence, selection, loop and exception handling structure. Statements are the basic building blocks of a program. In Java, there are simple statements and coincidence statements. A simple statement is a line of code. For example, privateint3=3; A compound statement is a combination of simple statements, such as a method, etc. Generally, statements are executed in order, but when special conditions, such as loops, are encountered, the statements follow the flow control structure.
(1) select the structure
The selection structure is used to implement different operations according to different conditions. It provides a mechanism for the program to run corresponding statements according to the corresponding conditions. The Java language implementation choice structure comes in two forms: an if-else statement with two branch choices, and a switch statement with multiple branch choices. The choice statement requires something in logic, but it is relatively simple, such as whether a statement is true or false, whether it is true or not, and so on. Logical propositions are used in logical expression representation and are used as logical conditions for two - way or multiway branching structures.
Obviously, we are concerned with the writing of conditions, which are generally: relational expressions, logical expressions, and conditional operational expressions.
(1) relational expression: an expression that connects two expressions with a relational operator. Evaluate the values of two expressions of the same type, and then compare the results: true or false. Such as:
X % 2 = = 0;
X + y> = 0;
(2) logical expression: the expression whose operands are logical values and connected by logical symbols becomes a logical expression, and its value remains a logical value. Such as:
X> 6 && y< 3;
X> 6 | | y> 8;
4 = = 0, y % && y % 100! =0&&y%400==0//y is the leap year condition
(3) conditional operand: an expression connected by a ternary operator, with the syntax: (logical expression)? (expression 1) :(expression 2). Returns the value of expression 1 when the value of the logical expression is true, otherwise, returns the value of expression 2.
(2) if - else statements
The general if-else statement looks like this,
 
if( Logical expression ){ or if( Logical expression ) statements 1 ;  
 statements 1 ; [else statements 2 ; ] 
}else{ 
 statements 2 ;  
} 

An if statement is a statement specifically designed to implement a selection structure that determines whether one of two operations will be run based on the truth or falsehood of a logical condition. For example, a leap year is one that is divisible by 4 but not by 100, or is divisible by 400. Therefore, leap year decisions can be expressed in a logical expression.
Here's how to decide if 2012 is a leap year:
 
publicclassIsLeapYear{ 
publicstaticvoidmain(Stringargs[]){ 
intyear=2012; 
booleanleapYear=(year%4==0&&year%100!=0||year%400==0); 
if(leapYear){ 
System.out.println(year+" Is a leap year "); 
}else{ 
System.out.println(year+" Not a leap year "); 
} 
} 
} 

Nesting of if-else statements:
Statements in statement 1 or statement 2 in an if-else statement can also be if-else statements, thus forming a nesting of if-else statements. The most common of these is the elseif statement nested multi-choice structure:
 
if() statements 1 
elseif( Logical expression ) statements 2 
........ 
elseif( Logical expression ) statements n 
else statements n+1 

The program runs from the top down to determine the logical condition, once a logical condition is satisfied (that is, the Boolean expression value is true), the corresponding statement is run, and then no longer determine the other conditions, go directly to the structure exit, run a subsequent statement of the if statement. Of course, in this multi-choice structure, it is easy to confuse the collocation between if and else. The Java language dictates that the else always matches the nearest if. You can use curly braces {} to change the pairing relationship if you want, which we often do.

Related articles: