Understand [logic control] it's enough to read this article

Sequential structure

Execute line by line in the order in which the code is written

Code slice
System.out.pprintln("1");
System.out.pprintln("2");
System.out.pprintln("3");

Branching structure

if statement

  • Basic grammatical form 1

if (Boolean expression){
Statement block
}

  • Basic grammatical form 2

if (Boolean expression){
Execute when conditions are met
}else{
Execute when conditions are not met
}

  • Basic grammatical form 3

if (Boolean expression){
Execute when conditions are met
}else if (Boolean expression){
Execute when conditions are met
}else{
Execute when none of the conditions are met
}

πŸ’Œ Code example

  1. Judge whether a number is odd or even
Code slice
int n=10;
if(n%2==0){
  System.out.println("even numbers");
}else{
 System.out.println("Odd number");
}
  1. Judge whether a number is positive or negative
Code slice
int num=10;
if(num > 0){
  System.out.println("num Is a positive number");
} else  if(num < 0) {
  System.out.println("num Is a negative number");
 } else {
  System.out.println("num Is 0");
}

  1. Determine whether a number is a leap year
Code slice
int year=2000;
if(yera %4==0  && year %100 !=0  ||  year %400==0){
    System.oout.println(year +"It's a leap year");
}else {
  System.oout.println(year +"Not a leap year");
}

switch Statements

  • Basic grammatical form

switch (parameters cannot be complex expressions){
case content 1:{
Execute the statement when the content is satisfied;
breakοΌ›
}
case content 2:{
Execute the statement when the content is satisfied;
breakοΌ›
}
...
default :{
Execute the statement when the content is not satisfied;
breakοΌ›
}
}

πŸ”΄ Note: data types that cannot be used as parameters in Java: long float double and Boolean types

πŸ’Œ Code example

Code slice
int  a=10;
switch(a){
   case 1:
         System.out.println("1");
         break;
    case 2:
         System.out.println("2");
         break;
    default :
         System.out.println("Parameter mismatch");
         break;
}

Cyclic structure

while Loop

Basic grammatical form

While (Boolean expression){
Statement block
}

Code example

  1. Find the sum of 1 to 10
Code slice
int i=1;
int sum=0;
while(i<=10){
    sum=sum+i;
    i++;
}
System.out.println(sum);
  1. Calculate the factorial of 5
Code slice
int n=5;
int i=1;
int ret=1;
while(i<=n){
     ret =ret * i;
     i++;
}
System.out.println(ret);

  1. Find the sum of factorials of n
Code slice
int j=1;
int sum=0;
while(j<=5){
     int i=1;
     int ret=1;
     while(i<=j){
            ret=ret * i;
            i++;
      }
      sum=sum+ret;
      j++;
 }
 System.out.println(sum);

break

function

The function of break is to end the loop early.

Code example

Find the first number between 1 and 10 divided by 3.

Code slice
int i=1;
while(i<=10){
    if(i % 3 ==0){
         System.out.println(i);
         break;
    }
    i++;
}

continue

function

The function of continue is to skip this cycle and immediately enter the next cycle.

Code example

  1. Find the number of multiples of all 3 from 1 to 10
Code slice
int i=1;
while(i<=10){
    if(i % 3!=0){
         i++;
         continue;
    }
    System.out.println(i);
    i++;
}
  1. Find a number that can be divided by both 3 and 5
Code slice
int i=1;
while(i<=10){
    if(i % 15 !=0){
        i++;
        continue;
    }
 System.out.println(i);
  i++;

}

πŸ”΄ Note: both break and continue must be used in the loop [Special: break can be used in switch]

for loop

Basic grammatical form

For (expression 1; Boolean expression 2; expression 3){
Statement block;
}

Expression 1: used to initialize loop variables
Expression 2: loop condition
Expression 3: update loop variable

Code example

  1. Sum to 1
Code slice
int sum=0;
for(int i=1;i<=100;i++){
  sum=sum+i;
 }
 System.out.println(sum);
  1. Find the factorial of 1 to 5
Code slice
int ret=1;
for(int i=1;i<=5;i++){
   ret *=i;
}
System.out.println(ret);
  1. Find the sum of factorials from 1 to 5
Code slice
int sum=0;
for(int j=1;j<=5;j++){
  int ret=1;
   for(int i=1;i<=j;i++){
    ret *=i;
   }
   sum+=ret;
}
System.out.println(sum);

do... while loop

Code slice
int i=1;
int sum=0;
do{
  sum=sum+i;
  i++;

}while(i<=10);
System.out.println(sum);

πŸ’™ do... while will be executed at least once
verification

Code slice
int i=0;
do{
  System.out.println("hello");
}while(i!=0);

🎈🎈🎈🎈🎈🎈 Original is not easy. If there are errors, please leave a message in the comment area for correction. 🧑🧑🧑🧑🧑🧑🧑🧑🧑

Keywords: Java Back-end

Added by Charlie9809 on Tue, 04 Jan 2022 13:36:17 +0200