Dark Horse programmer-java basics-Basic Knowledge (3), dark horse java

Source: Internet
Author: User

Dark Horse programmer-java basics-Basic Knowledge (3), dark horse java

 

------ Android training, java training, and hope to communicate with you! ------

 

1. Process Control 1. Select structure statements

Select a structure statement to execute different statements according to different conditions. Conditional statements include if, if... else, and switch.

1. if Condition Statement

An if statement contains a Boolean expression and one or more statements.

Syntax

The syntax of the If statement is as follows:

If (Boolean expression) {// statement to be executed if the Boolean expression is true}

If the Boolean expression is true, the code block in the if statement is executed. Otherwise, run the Code following the If statement block.

For example:

Public class Test {public static void main (String [] args) {int a = 30; if (a> 20) {System. out. println ("condition is true");}/* if there is only one statement to be executed after if, then {} can be omitted without writing, but it is recommended to write it to facilitate reading and code standardization. If you want to execute multiple statements later, you must write them. */}/* Execution result: the condition is true */
2. if... else statement

The if statement can be followed by the else statement.

Syntax

If... Else usage is as follows:

If (Boolean expression) {// if the Boolean expression value is true} else {// if the Boolean expression value is false}

When the Boolean expression value of the if statement is false, the else statement block is executed.

For example:

Public class Test {public static void main (String [] args) {int a = 30, if (a> 200) {System. out. println ("condition is true");}/* if there is only one statement to be executed after if, then {} can be omitted without writing, but it is recommended to write it to facilitate reading and code standardization. If you want to execute multiple statements later, you must write them. */Else {System. out. println ("condition not true") ;}}/ * execution result: the condition is invalid */
3. Ternary Operators

It is similar to the if... else statement.

Syntax

The usage of ternary operators is as follows:

Judgment condition? Expression 1: expression 2/* returns a result. It is usually used to assign values to a variable. When the judgment condition is true, the calculation result is the value of expression 1; otherwise, it is the value of expression 2. */

For example:

Public class Test {public static void main (String [] args) {int a = 30; int B = 2; int max = a> B? A: B; System. out. println (max) ;}/ * execution result: 30 */
4. if... else statement

The if statement can be followed by the else if... Else statements, which can detect multiple possibilities.

When using the if, else if, and else statements, pay attention to the following points:

  • The if statement has at most one else Statement, which is placed after all the else if statements.
  • The If statement can have several else if statements that must be earlier than the else statement.
  • Once one of the else if statements is detected to be true, other else if statements and else statements will be skipped.
Syntax

The syntax format of if... else is as follows:

If (Boolean expression 1) {// if the value of Boolean expression 1 is true, execute code} else if (Boolean expression 2) {// if the value of Boolean expression 2 is true, run the code} else if (Boolean expression 3) {// if the value of Boolean expression 3 is true, run the code} else {// If none of the above boolean expressions are true, run the code}

For example:

Public class Test {public static void main (String [] args) {int a = 100; if (a> = 85) {System. out. println ("excellent");} else if (a <85 & a> = 70) {System. out. println ("good");} else if (a <70 & a> = 60) {System. out. println ("pass");} else {System. out. println ("fail") ;}}/ * execution result: Excellent */
5. if nested syntax

The nested if format is as follows:

If (Boolean expression 1) {// if the value of Boolean expression 1 is true, run the Code if (Boolean expression 2) {// if the value of Boolean expression 2 is true, execute the code }}

For example:

Public class Test {public static void main (String [] args) {int a = 100; if (a> 10) {System. out. println ("condition 1 is true"); a = 10; if (a = 10) {System. out. println ("condition 2 is true") ;}}/ * execution result: Condition 1 is true condition 2 is true */

  

6. switch statement

Switch is also a common choice Statement, which is different from the if Condition Statement.

Syntax

The switch syntax format is as follows:

Switch (expression) {case value: // statement break; // n case statements default: // optional // statement}

For example:

Public class Test {public static void main (String [] args) {int a = 10; switch (a) {case 1: System. out. println ("Haha"); break; case 10: System. out. println (""); break; case 100: System. out. println ("Haha"); break; default: System. out. println ("not found") ;}}/* execution result: */

The switch statement has the following rules:

  • The variable type in the switch statement can only be byte, short, int, or char.
  • The switch statement can have multiple case statements. Each case is followed by a value to be compared and a colon.
  • The data type of the value in the case statement must be the same as that of the variable, and can only be a constant or a literal constant.
  • When the value of the variable is equal to the value of the case statement, the statement after the case statement starts to run until the break statement appears.
  • When a break statement is encountered, the switch statement is terminated. The program jumps to the statement execution after the switch statement. The case statement does not need to contain the break statement. If no break statement appears, the program continues to execute the next case statement until the break statement appears.
  • The switch statement can contain a default Branch, which must be the last branch of the switch statement. The default statement is executed when the value of the case statement is equal to the value of the variable. The default branch does not require a break statement.

 

Summary:

If and switch:

If it is used to determine the specific value, such as the week, date, and so on, and it is recommended to use switch to match the byte, short, int, and char Types, the efficiency is slightly higher.

We recommend that you use if to make judgments based on the boolean' type, for example, score classification. if is more widely used.

2. Loop statements

The Program Statement of the selected structure can only be executed once. If you want to perform the same operation multiple times, you need to use the loop structure.

Java has three main cycle structures:

  • While Loop
  • Do... While Loop
  • For Loop

Of course, an enhanced for loop mainly used for arrays is introduced in Java 5.

1. while statement

The while loop is similar to the selection and judgment statement, which determines whether to execute the statement based on the condition. The difference is that the while LOOP statement will be executed after the condition is set up and stops when the condition is not set, the SELECT statement condition is only executed once.

Syntax

The while syntax format is as follows:

  

While (Boolean expression) {// loop content} // as long as the Boolean expression is true, the loop will continue to execute.

 

For example:

Public class Test {public static void main (String [] args) {int a = 0;/* while (a <= 10) a ++; System. out. println ("I have been executed" + a + "times"); if there is only one statement to be executed after while, then {} can be omitted without writing, but for ease of reading, and code normalization. It is recommended to write it. If you want to execute multiple statements later, you must write them. */While (a <= 10) {a ++;} System. out. println ("I was executed" + a + "times");}/* execution result: I was executed 11 times
*/
2. do... while statement

Similar to the while statement, but the difference is that if the while statement does not meet the conditions, it cannot enter a loop. While do... while does not meet the conditions, it must be executed at least once.

Syntax

Do... while syntax format:

Do {// code statement} while (Boolean expression); // The Boolean expression is behind the loop body, so the statement block is executed before detecting the Boolean expression. // If the value of the Boolean expression is true, the statement block is executed until the value of the Boolean expression is false.

For example:

Public class Test {public static void main (String [] args) {int a = 0; do {a ++; System. out. println ("I was executed" + a + "times") ;}while (a> 10) ;}/ * execution result: I was executed once */
3. for statement

The number of for loop executions is determined before execution.

Syntax

The for syntax format is as follows:

For (initialization; Boolean expression; update) {// code statement}

For example:

Public class Test {public static void main (String [] args) {int a = 0; for (a = 0; a <10; a ++) {System. out. println ("I Have Been cyclically executed" + a + "times") ;}}/ * The execution result is: I have been executed 0 times, I have been executed 1 time, and I have been executed ...... times */

For loops are described as follows:

  • Perform the initialization step first. One or more loop control variables can be declared and initialized, or empty statements can be used.
  • Then, check the value of the Boolean expression. If it is true, the loop body is executed. If it is false, the loop ends and the statement following the loop body starts to be executed.
  • After a loop is executed, update the loop control variable.
  • Check the Boolean expression again. Execute the preceding process cyclically.

For execution sequence:

Public class Test {public static void main (String [] args) {int a = 0; for (initialization; Boolean expression; Update) {// code statement }}/ * step 1. perform initialization. Step 2. execute a Boolean expression. If it is true, perform Step 3. Otherwise, perform Step 5. step 3. execute the for loop body once, that is, the Code statement. Step 4. Execute the update and switch to step 2. Step 5. End the for loop and execute statements other than the for loop. */
4. nested for loop syntax

The syntax format of nested for is as follows:

Public class Test {public static void main (String [] args) {int a = 0; for (initialization; Boolean expression; update) {// code statement for (initialization; boolean expression; update) {// code statement }}}}

For example:

/* 9*9 multiplication table */public class Test {public static void main (String [] args) {int I, j; for (I = 1; I <10; I ++) {for (j = 1; j <= I; j ++) {System. out. print ("\ t" + j + "*" + I + "=" + (I * j);} System. out. println ();}}}

Compile and run:

5. Java enhanced for Loop

Java 5 introduces an enhanced for loop mainly used for arrays.

Java enhanced for loop syntax format:

For (declaration statement: expression) {// code sentence}

Statement:Declare a new local variable. The type of the variable must match the type of the array element. Its scope is limited to loop statement blocks, and its value is equal to the value of the array element.

Expression:The expression is the name of the array to be accessed, or the return value is an array method.

For example:

Public class Test {public static void main (String args []) {int [] numbers = {1, 3, 4, 5}; for (int num: numbers) {System. out. print (num); System. out. print (",") ;}}/ * result: 1, 3, 4, 5 ,*/

  

6. break keywords

Break is mainly used in loop or switch statements to jump out of the entire statement block.

The break jumps out of the innermost loop and continues to execute the statements below the loop.

Syntax

Break is a statement in the loop structure:

break;

For example:

Public class Test {public static void main (String args []) {int [] numbers = {1, 2, 3, 4, 5}; for (int x: numbers) {if (x = 4) {break;} System. out. print (x); System. out. print ("\ n") ;}}/ * result: 1 2 3 */
7. continue keywords

Continue applies to any loop control structure. The function is to immediately jump the program to the next iteration of the loop.

In the for loop, the continue statement causes the program to jump to the update statement immediately.

In the while or do... In the while loop, the program immediately jumps to the judgment statement of the Boolean expression.

Syntax

Continue is a simple statement in the loop body:

continue;

For example:

Public class Test {public static void main (String args []) {int [] numbers = {1, 2, 3, 4, 5}; for (int x: numbers) {if (x = 3) {continue;} System. out. print (x); System. out. print ("\ n") ;}}/ * result: 1 2 4 5 */

  

 

 

 

 

 

 

 

 

 

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