Javase Basic Note Three

Source: Internet
Author: User

Four. Process Control

Branching structure:

1. Condition Judgment:

①IF (conditional expression) {}

②IF (conditional expression) {

//EXECUTE statement 1;

}else{

//EXECUTE statement 2;

}

③IF (conditional expression 1) {

//EXECUTE statement 1;

}else if (conditional expression 2) {

//EXECUTE statement 2;

}...

}else{

//EXECUTE statement;

}


Attention:

1. Once a conditional expression is met, the execution of the statement is executed, execution is completed, and the current condition is determined, and the following conditional structure statements are not executed.

2. If there is a "mutex" relationship between many conditional expressions, multiple structures can be swapped up and down in order

If there is a "containment" relationship between many conditional expressions, it is required that the scope of the conditional expression be small to be written on a large range.


2. Select structure

Switch (variable) {

Case value 1:

Case Value 2:

Break

...

Default

Break

}

Attention:

1. Variables can be of the following data types: byte short int Char enumeration String

2.case can only fill in the value of the variable, cannot write range

3.default; can be omitted, and its position is flexible, but usually placed after the case statement

4. Once a case statement is satisfied, enter to perform its operation until a break is encountered or the program terminates

5. In some cases, to determine if the condition of the statement satisfies the data type of the switch variable, and if the value is not many, it is recommended to select Swith-case, in addition, select If-else


Loop structure

1. Format:

① initialization conditions

② Cycle Conditions

③ Iteration Conditions

④ Loop Body


for (①;②;③) {

}


while (②) {

}


do{

}while (②);


Note: 1. Different loops can be converted to each other

The difference between 2.while and Do-while: The Do-while program executes at least once


2. Nested loops: Loops can also be declared in a looping structure. Let the inner Loop Structure Act as the loop body of the outer loop, if the outer loop executes MCI, the inner layer loops n times, and the whole program executes m*n times.


Topic:

for (int i = 0;i<3;i++) {

for (int j = 0;j<6;j++) {

System.out.print ("*");

}

System.out.println ();

}

Outer control number of rows, inner control number of columns


Example: 1.99 multiplication table 2. Prime numbers within 100 of output


3. Infinite loops

for (;;) {

...

if () {

Break

}

}

Or

while (ture)

...

if () {

Break

}

...

}

Often we provide a loop termination condition inside an infinite loop structure, using the break keyword. Otherwise, this loop will be executed indefinitely, resulting in a dead loop, which we want to avoid.


Break & Continue Keywords

Break

Used in swith-case structures or in cyclic structures

In the loop structure, once the break is executed, it jumps out of the current loop


Continue

Used in the loop structure

Once executed to continue, jump out of the loop


for (int i = 1;i <= 10;i++) {

if (i% 4 = = 0) {

Break 123

continue;//123567910

}

System.out.println (i);

}

In nested loops, a tagged break and continue are used.

lable:for (int i = 1;i < 5;i++) {

for (int j = 1;j <= 10;i++) {

if (i% 4 = = 0) {

Break able;

Continue lable;

}

System.out.println (j);

}

System.out.println ();

}



This article from the "Ah Cheng Blog" blog, reproduced please contact the author!

Javase Basic Note Three

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.