Java Fundamentals-Basic Syntax 2

Source: Internet
Author: User

First, the statement

  

1.1. Conditional Statement 1.1.1 If statement

  

1.1.2 Branch Statement (switch statement)

  

  

      

1.2. Loop statement 1.2.1 For Loop statement

  

Learn to draw memory analysis diagram analysis of other people write the program, so it is easy to analyze the law, analysis of the law also understand the other people's algorithm, it is easy to read other people's program.

in-memory parsing result=1 ! +2 ! ... +10 ! The algorithm

  

After analysis to I equals 3, basically has obtained the law, this procedure calculates is: the result = 1!+2!+3!...... +10!.

As you can see, it is helpful to know how to draw memory to analyze programs to understand other people's programs. When you read someone else's program, when you find it difficult to read, you draw memory analysis.

The best way to learn about other people's algorithms is to analyze the programs that others write, and analyze the process of combining memory analysis with the best practices.

1.2.2 While loop and do while loops

  

1.2.3 Break and Continue statements

  

Second, the method

  

When the method executes to the return statement, the execution of the method ends, and the method can have a return value, but it can be used without the return value . method is first defined before it can be called.

1 public class testmethod{2 public     static void Main (String args[]) {3         m (); 4         M1 (3); 5         m2 (2,3); 6         int i = m3 (4,5); 7         System.out.println (i); 8     } 9     //The following definitions are static methods, which can be called directly in the main () method by the public static     void m () {11             System.out.println ("hello!");             System.out.println ("Aloof and pale Wolf");         }14 public     static void M1 (int i) {+             if (i==5) {                     return;18                 }19             System.out.println (i);         }21 public     static void m2 (int i,int j) {             System.out.println (i+j);         }25 public     static int m3 (int i,int j) {             return i+j;28}29         }
Three Scope of the variable

The scope of the variable is only valid for "{}", and it doesn't work if the "{}" is out.

Four Recursive invocation

Recursion: a call to itself within a method is called a recursive

  

The entire method executes in memory as shown in the procedure:

  

Example: Using recursion to calculate the number of the 5th Fibonacci sequence
1/* Calculates the 5th Fibonacci number */2/* 3 Fibonacci Sequence features: F (1) =1,f (2) =1,f (3) =f (1) +f (2), f (4) = (F2) + (F3) ... In turn. 4 that is, the last number is equal to the sum of the first two numbers, such a sequence is the Fibonacci sequence. 5 */6//7 use recursive call method to calculate 8 */9 public class Fab{10 public     static void Main (String args[]) {One         System.out.println (f ( 5));     }13 public     static int f (int n) {             if (n==1| | n==2) {                     1;17                 }else{18                         return F (n-1) +f (n-2);                     }20}21         }

The entire in-memory execution process is shown in

  

Five process of program execution

  

Java Fundamentals-Basic Syntax 2

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.