How do you write Java code without loop structure

Posted by horseygirl on Tue, 04 Jan 2022 10:28:14 +0100

logical control

Sequential structure

The order structure is relatively simple, that is, write line by line in order. If you adjust the writing code, the execution order will also change

public class Main{
      public static void main(String[] args){
         System.out.println("aa");
         System.out.println("bb");
         System.out.println("cc");
         }
}

Operation results:

public class Main{
      public static void main(String[] args){
         System.out.println("aa");
         System.out.println("cc");
         System.out.println("bb");
         }
}

Operation results:

Branching structure

if statement

Syntax 1:
If (Boolean expression){
//Execute code when conditions are met
}
Syntax 2:
If (Boolean expression){
//Execute code when conditions are met
}else{
//Code executed when conditions are not met
}
Syntax 3 (multi branch structure):
If (Boolean expression){
//Execute code when conditions are met
}Else if (Boolean expression){
//Execute code when conditions are met
}else{
//Execute code when none of the conditions are met
}

Code example:

//Example 1
int num = 10;
if (num % 2 == 0) {
    System.out.println("num It's an even number");
} else {
    System.out.println("num It's an odd number");
}

//Example 2
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");
}

//Example 3
int year = 2000;
if (year % 100 == 0) {
    // Judgment century leap year
    if (year % 400 == 0) {
        System.out.println("It's a leap year");
   } else {
        System.out.println("Not a leap year");
   }
} else {
    // Ordinary leap year
    if (year % 4 == 0) {
        System.out.println("It's a leap year");
   } else {
        System.out.println("Not a leap year");
   }
}

When writing if {}else {} statements, you should pay attention to else dangling

int x = 10;
int y = 10;
if (x == 10) 
 if (y == 10)
 System.out.println("aaa");
else
 System.out.println("bbb");

if / else statements may not be bracketed But you can also write statements (only one statement can be written) At this point else matches the closest if

switch statement

Switch (integer | enumeration | character | string){
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;]
}
}

Code example:

int day = 1;
switch(day) {
    case 1:
        System.out.println("Monday");
        break;
    case 2:
        System.out.println("Tuesday");
        break;
    case 3:
        System.out.println("Wednesday");
        break;
    case 4:
        System.out.println("Thursday");
        break;
    case 5:
        System.out.println("Friday");
        break;
    case 6:
        System.out.println("Saturday");
        break;
    case 7:
        System.out.println("Sunday");
        break;
    default:
        System.out.println("Incorrect input");
        break; }

According to the different values of switch, the corresponding case statement will be executed If you encounter a break, you will end the case statement. If you do not write a break, the program will run down until you encounter a break.
If the value in the switch does not have a matching case, the statement in default will be executed, so it is best to bring default with the switch statement
In addition, the value in switch () can only be an integer | enumeration | character | string, otherwise the compilation will report an error of "incompatible type"
switch cannot be used for complex conditions, otherwise it cannot be expressed.
To sum up, the limitations of using switch statements are still great

Cyclic structure

while Loop

If the loop condition is true, execute the statement, otherwise end the loop
Basic syntax format:
While (loop condition){
Loop statement;
}

Code example:

int n = 1; 
int result = 0; 
while (n <= 100) { 
 result += n; 
 n++; 
} 
System.out.println(num); 
// results of enforcement
5050 

matters needing attention:

  1. Similar to if, the statement under while may not write {}, but only one statement can be supported when it is not written It is suggested to add {}
  2. Similar to if, the {suggestion after while is written on the same line as while
  3. Similar to if, do not write more semicolons after while, otherwise the loop may not execute correctly

break

The function of break is to end the cycle ahead of time
Code example:

int num = 100; 
while (num <= 200) { 
 if (num % 3 == 0) { 
 System.out.println("A multiple of 3 was found, by:" + num); 
 break; 
 } 
 num++; 
} 
// results of enforcement
 A multiple of 3 was found, For: 102

continue

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

Code example: find multiples of all 3 in 100-200

int num = 100; 
while (num <= 200) { 
 if (num % 3 != 0) { 
 num++; // Don't forget the + + here! Otherwise, there will be a dead cycle 
 continue; 
 } 
 System.out.println("A multiple of 3 was found, by:" + num); 
 num++; 
}

When you execute continue, you will immediately enter the next loop without executing the following print statement

for loop

Basic grammar
For (expression 1; expression 2; expression 3){
Circulatory body;
}
Expression 1: used to initialize a loop variable
Expression 2: loop condition
Expression 3: update loop variable

Code example: calculate the sum of 1 to 100

int sum = 0; 
for (int i = 1; i <= 100; i++) { 
 sum += i; 
} 
System.out.println("sum = " + sum); 
// results of enforcement
5050

matters needing attention:

  1. Similar to if, the statement below for can not write {}, but only one statement can be supported when it is not written It is suggested to add {}
  2. Similar to if, the {suggestion after for is written on the same line as while
  3. Similar to if, do not write more semicolons after for, otherwise the loop may not execute correctly

do while loop

Basic grammar
do{
Loop statement;
}While (cycle condition);
Execute the loop statement first, and then determine the loop condition

Code example:

int num = 1; 
do { 
 System.out.println(num); 
 num++; 
} while (num <= 10);

result:

matters needing attention:

  1. Don't forget the semicolon at the end of the do while loop
  2. Generally, do while is rarely used, and for and while are recommended

Topics: Java