Java basic syntax - process control statement

Posted by cityboy101 on Fri, 11 Feb 2022 01:25:28 +0100

1, Branch statement

1.if statement

Conditional statement, which determines whether to execute its statement block according to the value of the expression

Syntax:

if   (Boolean expression) {
<Statement block>
}

Common Boolean expression operators:

==  >  <  >=  <=  !=   &&  ||  !

Code example:

int num =20;
if(num>20){
system.out.println("Number greater than 20\n");
}
if(num<20){
system.out.println("Number less than 20\n");
}
if(num=20){
system.out.println("The number equals 20\n");
}

Code example:

Simple if else statement

The execution of if code block or else code block is determined according to the value of the expression
Syntax:
if (Boolean expression){
< statement block >
} else {
< statement block >
}

int num = ;
if(num=0){
system.out.println("The number equals 0\n");
}else{
system.out.println("The number is not equal to 0\n");
}

2.switch statement

Multi branch statement
Syntax structure:

switch((expression){
	case <constant>:  Statement block;
	break;
	case <constant>:  Statement block;
	break;
	default : Statement block;
	break;
}

break keyword: jump out after the current statement block is executed, and do not continue to execute downward.
In a switch statement, if there is no break statement in the current statement block and there is a statement block after it, the subsequent statement block will continue to be executed after the statement block is executed.
default statement:
After all case statements are matched, if there is no matching item, execute the statement (whether there is a default statement or not, if there is no break statement before the default statement, the default statement will be executed)

In the switch statement, the expression can be:

  • int type
  • byte or short type
  • char type
  • Enumeration types (introduced in subsequent courses)
  • String type
    But the expression cannot be:
  • float type
  • long type
  • Object reference type
String trafficSingle = ...;  
switch (trafficSingle) {
    case "red":
        System.out.println(""Red light");
        break;
    case "yellow":
        System.out.println(""Yellow light");
        break;
    case "green":
        System.out.println(""Green light");
        break;
    default:
        System.out.println(""Invalid value");
        break;
}

2, Circular statement

Circular statements can execute statement blocks in a circular manner:

1.while loop

grammar

while(<expression>){
Statement block;
}
Example:
int i=0;
while(i<10){
System.out.println(i);
i++;
}

The feature is to judge whether the condition is true first, and then execute the statement block.

2. Do while loop

grammar

do{
Statement block;
}

Example:

int i=0;
while(i<10){
System.out.println(i);
i++;
}while(i);

The feature is to execute a statement block first, and then judge the conditions.

3.for loop

Syntax:

for(<Initial expression>;<Judgment expression>;<Variation expression>){
Statement block;
}

Example:

for (int i = 1; i <= 60; i++) {
           if (i % 13 == 0)  System.out.print(i + "  ");
      }
       System.out.println();
  }

4.break and continue statements

break statements are generally used to jump out of the end loop;
The continue statement is generally used to jump out of this cycle and directly carry out the next cycle.

Summary:

  • for, do while, and while loop statements can be nested
  • The for loop is usually used in a loop that specifies the number of cycles
  • While / do while is usually used in a loop that ends when conditions change during the loop
  • while: the loop body can be executed 0-n times
  • Do while: the loop body can be executed 1-n times

3, Method declaration and invocation

Declaration syntax of method:

<Access modifier > Return type method name(parameter list) {
	Method body
 }

Example:

public static int print(){
	
}
Call:
public static void main(String[] args) {
           ......
           print();
           ......
   }

1.return keyword

When you declare a method, you can specify the return type for it:

  • Basic type
  • void (indicates no return value)

After the method is executed, it needs to return a value of the specified type (except void)
Use the return statement to return a value

2. Parameter value transfer

When a method is called, parameters are passed values.
In other words, if the parameter is modified inside the called method, the original value will not be affected.

3. Overload method

Methods with the same method name but different parameters (type or quantity) in the same class
Example:

	public static void changInt(int a, int b) 
	public static void changInt(int a, int b, int c)
	public static void changInt(float a, int b)

rule

  • The parameter list must be different
  • The return type can be different

Topics: Java Back-end