Java basic knowledge points (select structure, loop structure)

One, choose the structure

1. The structure of the program is divided into:

Sequence structure selection structure loop structure

2. The selection structure is divided into:

Basic: if (1 condition) {} (no enlarging brackets after the judgment condition means that only one statement is executed)

Complex conditions: if (multiple conditions, logical operator connection) {}

if else the combination of two basic if conditions are mutually exclusive

Multiple if else: only one of them will be executed

Nested if: if(){if(){}}

switch statement (case compares the contents, the address does not compare values): suitable equivalent determination, if the multiple is converted to equivalent switch
switch (NUM) { Case. 6: Sout (); Break; Case. 7: : : default: Sout(); } switch does not accept the value passed by long. If there is a break after Default, it can be placed anywhere on the switch, if not, it can only be placed at the end.








String is not a data type, it is a reference type

== compares whether the address value and the content are equal (basic data type, the content is the same, the address value is the same)
String is not a data type, it is a reference type

3. Code block:

All {} can be called code blocks,
for example:
class code block
method code block
loop code block
conditional code block
static code block
instance code block
local code block

4.equals():

equals() is equivalent to ==, but it only compares whether the specific content is equal, and does not care whether the address value is equal.
For example: a==b is equivalent to a.equals(b)

Second, the loop structure

1. Three elements of circulation:

1. Loop variable: counter

2. Loop conditions: the range of loop variables

3. Loop body: Repeating things

2. Loop:

while loop:

while(condition){ loop body; loop variable; }


do-while loop:

do{ loop body; loop variable; }while(loop variable);


for loop:

for (define variables and initial values; loop conditions; loop variables) { loop body; }

Scenarios used in while and do-while loops: the number of loops is determined.

3. The difference between while and do -while:

While the condition is judged first, then the loop body is executed, and do-while executes the loop body first, and then the condition judgment.

4. The difference between break and continue:

Break is to jump out of the entire loop, and directly execute the following code.
continue is to jump out of the loop of the current value, and the body of the loop continues

Guess you like

Origin blog.csdn.net/StruggleBamboo/article/details/111387861