Java Learning notes (Getting Started) _ Program flow control structure and method _java

Source: Internet
Author: User
Tags exception handling
program flow control structure and method
Program flow control structure is divided into: order, selection, cycle and exception handling structure. Statements are the basic constituent units of a program. In Java, a simple statement and a conforming statement, a simple statement is a line of code, for example, a privateint3=3 compound statement is a combination of some simple statements, such as a method, and so on. Generally speaking, the execution flow of statements is done sequentially, but when a particular condition, such as a loop, is encountered, the statement is carried out according to the process control structure.
(1) Select Structure
The selection structure is used to implement different operations according to different conditions, which provides a mechanism for the program to run the corresponding statement according to the corresponding conditions. The Java Speech implementation Choice structure has two kinds of forms: one is the If-else statement chosen by the two branches, the other is the switch statement with many branches selected. The choice of the statement needs to use logic inside the things, but more obvious, such as the true and false proposition, whether or not. Logical propositions are used to represent logical expressions and are used as logical conditions for two-way or multiple-branch structures.
Obviously, we are more concerned with the writing of conditions, which generally include: relational expressions, logical expressions, and conditional operation expressions.
① relational expression: a formula that connects two expressions with a relational operator. Evaluates the value of two similar expressions and compares them with the result: true (True) or False (false). For example:
② logical expression: The operand is a logical value, and the expression of a logical character connection is a logical expression whose value is still a logical value. For example:
x>6| | y>8;
Y%4==0&&y%100!=0&&y%400==0//y is a leap year condition
③ conditional expression: An expression that is joined by a three-mesh operator and has a syntax format: (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 statement
The general If-else statement is this,
Copy Code code as follows:

if (logical expression) {or if (logical expression) statement 1;
Statement 1;[else statement 2;]
Statement 2;

An IF statement is a statement that is specifically designed to implement a selection structure, which determines the operation of one of two operations according to the true or false in the logical condition. For example: A leap year is a term that can be divisible by 4 but not divisible by 100, or is divisible by 400. Therefore, the judgment of a leap year can be expressed in a logical expression.
Here's how to determine if 2012 is a leap year:
Copy Code code as follows:

Publicstaticvoidmain (stringargs[]) {
Booleanleapyear= (year%4==0&&year%100!=0| | year%400==0);
if (leapyear) {
System.out.println (year+ "is a leap year");
System.out.println (year+ "not leap Year");

Nesting of If-else statements:
The statement in Statement 1 or statement 2 in the If-else statement can also be a if-else statement, which forms the nesting of the If-else statement. One of the most common is the multiple-selection structure of the ElseIf statement nesting:
Copy Code code as follows:

if () Statement 1
ElseIf (logical expression) Statement 2
ElseIf (logical expression) statement n
Else Statement n+1

The program runs from top to bottom to judge the logical conditions, once a logical condition is satisfied (that is, the value of Boolean expression is true), then run the corresponding statement, and then no longer judge other conditions, go directly to the structure of the export, run if statement subsequent statements. Of course, in this multiple-selection structure, it is easier to confuse the collocation between if and else. The Java language stipulates that else is always paired with the if closest to it. If you need to be able to change the pairing with curly braces {}, we often do.
Related Article

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: 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.