Java learning basics daysof03

Source: Internet
Author: User

1.1. While loop and do ...... While Loop

Syntax format of the while loop:

 

While (condition)

 

{

 

// Execute the statement

 

}

 

// The Infinite Loop format of while is: While (true)

 

Do ...... Syntax format of the while loop:

 

Do

 

{

 

// Execute the statement

 

} While (condition );

 

Do ...... The while statement is executed once regardless of whether the condition result is true or false.

1.2. For Loop

Syntax format of the For Loop:

 

Syntax format: For (conditon1; condition2; condition3)

 

{

 

// Execute the statement

 

}

 

 

 

 

 

For execution method: the meaning of condition1 is to initialize the expression and then enter condition3. The meaning of condition is to judge the expression,

If true is returned after comparison, the loop continues. Execute the statement in braces to execute the condition2 expression.

After the execution is complete, the returned result is "continue" and "conditio3n", and so on until the result is "false. Both condition1 and condition3 can be left empty, that is, for (; condition2;) is also possible. In addition, the simplest way of for (;). condition2 in an infinite loop is true.

For statements can be nested. For can be a for loop, which is used in many implementations and development.

Exercise 1:

 

/*

 

Requirement: print format:

 

*

 

**

 

***

 

****

 

*****

 

// Simple analysis: If repeated printing is found *, The while loop and for loop can be used.

 

Here we can say that there is a small Algorithm

 

*/

 

public class PrintSharp {      public  static void main(String[] args)      {            for(int  i = 0; i < 5; i ++)            {                  for(int  j = 0; j <i + 1; j ++ )                  {                       System.out.print("*");                  }                  System.out.println();            }      } }

 

Exercise 2:

 

/*

 

Requirement: print the 9-9 multiplication table

 

1*1 = 1

 

2*1 = 2 2*2 = 4

 

3*1 = 3 3*2 = 6 3*3 = 9

 

4*1 = 4 4 4*2 = 8 4*3 = 12 4*4 = 16

 

5*1 = 5 5*2 = 10 5*3 = 15 5*4 = 20 5*5 = 25

 

6*1 = 6 6*2 = 12 6*3 = 18 6*4 = 24 6*5 = 30 6*6 = 36

 

7*1 = 7 7*2 = 14 7*3 = 21 7*4 = 28 7*5 = 35 7*6 = 42 7*7 = 49

 

8*1 = 8 8*2 = 16 8*3 = 24 8*4 = 32 8*5 = 40 8*6 = 48 8*7 = 56 8*8 = 64

 

9*1 = 9 9*2 = 18 9*3 = 27 9*4 = 36 9*5 = 45 9*6 = 54 9*7 = 63 9*8 = 72 9*9 = 81

 

 

 

*/

 

public class PrintSharp {      public  static void main(String[] args)      {            for(int  i=1;i < 10; i ++)            {                  for(int  j = 1; j < i +1; j ++)                  {                       System.out.print(i  +"*" + j + "=" + i*j + "\t");                  }                  System.out.println();            }      } }

 

 

Supplement: the meaning of break terminates the interruption. The meaning of continue continues. It is generally the same as that of a while or for statement. Break jumps out of the loop and continues the next loop. This query is interrupted. You can also give the for statement an alias, for example:

 

PublicstaticvoidMain (string [] ARGs)

 

{

 

W:For(IntI = 0; I <5; I ++ ){

 

Q:For(IntJ = 0; j <5; j ++ ){

 

System.Out. Println ("J =" + J );

 

BreakQ;

 

}

 

}

 

1.3. Functions

1.3.1. Definition

A function is a piece of code that defines a specific function in a class.

1.3.2. Format

Modifier Return Value Type Function Name (parameter type form parameter 1, parameter type form parameter 2, parameter type form parameter n)

{

Execution statement

Return Value

}

Return Value Type: numeric type of the result after the function is run; parameter type: Data Type of the form parameter; format parameter: a variable, used to store the actual parameters passed to the function when calling the function; Return: Used to end the function; return value: the value changed will be returned to the caller.

1.3.3. Features

Define a function to encapsulate the function code.

This feature is easy to reuse.

The function is executed only when it is called.

The emergence of functions improves code reuse.

If there is no specific return value for the function, use void, then return can be omitted without writing.

Only functions can be called, and functions cannot be defined internally.

1.3.4. Function overload)

Function overloading means that more than one function with the same name can exist in the same class, as long as they have different numbers or types of parameters. The feature of function overload is irrelevant to the type of return value, and only related to the parameter list.

1.3.5. Array

Arrays are collections of the same data type. arrays are actually containers. In simple terms, arrays are used to store things and things are elements. The benefits of arrays can be automatically numbered from 0 to facilitate operations on these elements. What is the array format?

 

Element type [] array name = new element type [number or length of elements]

 

New is used to create a data-type container entity in the memory.

 

PS: Today's status does not require a half-day break.

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.