Dark Horse Programmer--java Foundation--Structure

Source: Internet
Author: User

------Java Training, Android training, iOS training,. NET training, look forward to communicating with you! -------

One: Structure

1. Sequential order

2. Select order

3. Cycle Order

Two: Concrete realization

1. Sequential execution

Class Shunxudemo{public static void Main (String args[]) {int a = 1, b = 2;int sum = 0;sum = a + b; System.out.println ("sum =" +sum);}}
All statements are executed in order from top to bottom, and no judgment statement is executed.

2. Select structure

The selection structure involves several statements:

①:if

②:if...else

③:if...else If...else If..........else

If statement:

Class Shunxudemo{public static void Main (String args[]) {int x = 2;if (x > 2) {System.out.println ("x > 2");}}


If...else statement:

Class Shunxudemo{public static void Main (String args[]) {int x = 2;if (x > 2) {System.out.println ("x > 2");} Else{system.out.println ("x < 2");}}


If...else if...else If..........else Statement:

Class Shunxudemo{public static void Main (String args[]) {int x = 2;if (x > 2) {System.out.println ("x > 2");} else if (x > 1) {System.out.println ("x > 1");} else if (x > 0) {System.out.println ("x > 0");} ELSESYSTEM.OUT.PRINTLN ("Error");}}
Note that the first statement in the If...else if...else if..........else statement, if established, cannot be executed even if it is set up.

When some problems are encountered, If...else If...else if..........else statement efficiency is somewhat low, eg:

Class Shunxudemo{public static void Main (String args[]) {int x = 2;if (x = = 3 | | x = = 4 | | x = = 5) {System.out.println ("Spring");} else if ((x ==6 | | x = = 7 | | x = = 8) {System.out.println ("Summer");} else if (x = = 9 | | x = = Ten | | x = = one) {System.out.println ("Autumn");} else if (x = = |-X = = 1 | | x = = 2) {System.out.println ("Winter");} ELSESYSTEM.OUT.PRINTLN ("Error");}}

The switch statement will be used at this time:

The structure of the switch statement:

switch (expression)

{

Case value 1: EXECUTE statement; break;

Case Value 2: EXECUTE statement; break;

....................

Default: Execute statement; (break;) The last break can not

}

Class Xuanzedemo{public static void Main (String args[]) {x = 2;switch (x) {case 3:case 4:case 5:system.out.println ("spring); Brea K;case 6:case 7:case 8:system.out.println ("Summer"); Break;case 9:case 10:case 11:system.out.println ("Autumn"); Break;case 12: Case 1:case 2:system.out.println ("Winter"); Break;default:system.out.println ("error"); break;}}

Switch has no independent execution order for default and other statements, and the default is to execute the case statement first.

Switch cannot use the Boolean type

Where the IF statement and the switch statement apply:

switch is suitable for use with small amounts of data, and if is used more broadly.

3. Cyclic structure

①:while Cycle

②:for Cycle

③:do while loop

While loop:

Class Whiledemo{public static void Main (String args[]) {int i = 0;while (i <) {System.out.println ("x =" +x); i++;}}

Do and loop:

Class Whiledemo{public static void Main (String args[]) {int i = 0;do {System.out.println ("x =" +x); i++;} while (i<30);}
For loop:

Class Fordemo{public static void Main (String args[]) {for (int i = 0; i <; i++) System.out.println ("x =" +x);}}

Three cycle comparisons:

The For loop and while are interchangeable, but the for loop is a little more flexible, their structure is not the same, and the Do While loop executes once, regardless of the condition.

Example: Using a For loop to print a rectangle:

Class Fordemo{public static void Main (String args[]) {for (int i = 0, i < 5; i++) {for (int j = 0; J < 3; J + +) {System.ou T.print ("*");} System.out.println ();}}}
Printing results:

***

***

***

***

***

It is concluded that the outer loop controls the number of rows in the rectangle, and the inner loop controls the number of each row, that is, the number of columns.

This allows you to print:

*

**

***

****

*****

Class Fordemo{public static void Main (String args[]) {for (int i = 0, i < 5; i++) {for (int j = 0; J <= I; j + +) {SYSTEM.O Ut.print ("*");} System.out.println ();}}}


If you print:

*****

****

***

**

*

Class Fordemo{public static void Main (String args[]) {for (int i = 0; i < 5; i++) {for (int j = i; J < 5; J + +) {System.ou T.print ("*");} System.out.println ();}}}

come to a conclusion:

Tip Up, you can change the condition so that the condition changes with the outer loop

Tip-down, you can change the initial value, allowing the condition to change with the outer loop

This allows you to print 99 multiplication tables:


Class Fordemo{public static void Main (String args[]) {for (int i = 0, i < 9; i++) {for (int j = i; J <= I; j + +) {SYSTEM.O Ut.print (i+ "*" + "j+" = "+i*j+" \ T ");} System.out.println ();}}}

Finally, you can print:

*

* *

*  *  *

*  *  *  *

*  *  *   *   *

This can be seen as:

----*

---* *

--* * *

-* * * *

* * * * *

Print the inverted triangle on the left, and then print to the right triangle:

Class Fordemo{public static void Main (String args[]) {for (int i = 0; i < 5; i++) {for (int j = i; J < 5; J + +) {System.ou T.print ("");} for (int j = 0; J <= I; j + +) {System.out.print ("*");} System.out.println ();}}}

In the end, this video basically can be summed up to the end, there are different ideas and hope that you have a lot of advice.


Dark Horse Programmer--java Foundation--Structure

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.