Day04 program Process Control

Source: Internet
Author: User
# Program process control * judgment structure * selection structure * Cycle Structure
### Judgment structure #### three formats of the IF statement ''' Java/* layer-by-layer judgment */If (conditional expression) // if only one statement is controlled by the if statement, the following braces can be omitted, that is, a single statement closest to the IF statement; the same applies to Else.
{Execution statement ;}
''''' Java/* Two-layer judgment */
/* Variable = (conditional expression )? Expression 1: expression 2; a ternary operator can be considered as a simplified if else statement. Its disadvantage is that it is an operator and there must be a result after the operation. */
If (conditional expression)
{Execution statement;
} Else
{Execution statement ;}''' '''Java/* multi-layer judgment */
If (conditional expression)
{Execution statement;
} Else if (conditional expression)
{Execution statement ;}......
Else {execution statement ;} ''' Example: ''' Java // requirement. The season is determined based on the value defined by the customer. // It is defined as. Belongs to spring, and belongs to summer, belongs to autumn, and belongs to winter. Class ifseason {public static void main (string [] ARGs)
{Int month = 7; If (month> 12 | month <1)
System. Out. println ("this month does not exist ");
Else if (month> = 3 & month <= 5)
System. Out. println (month + "month belongs to spring ");
Else if (month> = 6 & month <= 8) system. out. println (month + "month belongs to summer"); else if (month> = 9 & month <= 11) system. out. println (month + "month belongs to autumn"); else system. out. println (month + "month belongs to winter "); }
} ''' Result: Tips: Pay attention to the format specification of the main function. There is no dot in the middle, and there is no semicolon at the end. In addition, pay attention to class name and program consistency when executing the. Class file.
### Select structure >#### switch statement format ''' invalid witch (expression) // The expression only accepts four types of variables: byte, short, Int, and char. {/* There is no sequence between case and default. It is always executed starting from the first case. Default is executed only after all cases cannot be determined at any location. * // * If the switch statement is executed to break or braces are closed, the execution is complete. If the case or default statement meets the condition but does not have a break, it will execute all the statements after the case or default statement in sequence and no longer judge the value of the case until it encounters a break or braces. */Case value 1: execution statement; break; case value 2: execution statement; break ;......
Default:
Execute the statement;
Break; // The last break can be omitted.
} ''' Example: ''' javaclass switchdemo {public static void main (string [] ARGs) {int x = 5; Switch (X) {/* Execute case determination in sequence. If there are no items that meet the conditions, the default statement is executed directly. After the execution, neither the break statement nor the braces are encountered, execute the statements after default without determining the case until the break or braces are encountered. */Default: system. out. println ("wrong"); Case 1: system. out. println ("1"); Case 2: system. out. println ("2"); Case 3: system. out. println ("3") ;}} ''' result:
TIPS: multiple cases can correspond to the same execution statement, for example, ''' javaint x = 4; swich (x) {Case 3:
Case 4:
Case 5:
System. Out. println (x + "Spring ");
Break; default:
System. Out. println (x + "does not belong to spring or input error"); break;
} ''' ** The association and difference between the if statement and the switch statement: ** if statements have a wide range of applicability, and can be determined by the if expression if they are boolean expressions; switch can only compare values of four basic types. (The enumerated type is added in version 5.0 and the string is added in version 7.0 .) If there are not many values and the values match the four types, you can use switch to improve efficiency. Other cases: the range and Boolean types are determined. If and if are used in a wider range.
### Loop structure indicates the statement: while, do while, for >### while statement format '''javawhile (conditional expression) // The while statement condition expression cannot be followed by a semicolon. Although compilation is successful, it will be stuck in an endless loop. {Execution statement (loop body );
} // While is characterized by first determining the condition before executing the loop body. '''Tips: 'ctrl + C' force end command line execution. #### Do while statement format ''' javado {execution statement;
} While (condition expression); // do while is characterized by executing a loop body first and then determining the condition. That is, the loop body is executed at least once no matter whether the condition is met or not. ''' ##### For statement format ''' javafor (initialization expression, cyclic condition expression, and cyclic Operation expression) {(Cyclic body) execution statement;
} // The execution sequence is: initialization expression-> cyclic condition expression-> cyclic body-operation expression after the loop-> cyclic condition expression ...... Until the cycle condition is not met. '''

To be continued


From Weizhi note (wiz)

Day04 program Process Control

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.