Hello, everyone. Last time we talked about the example of Java operators. This time we will talk about the example of Java program structure. Stop gossiping and return to the truth. Let’s Talk Android!

Crossing the us in the previous chapter introduced the types of variables in Java and operators, through variable and operators can form a program statements, lines of program statements can we program, program statements can also be less, much more is a mess, but it doesn’t matter, we can manage the messy program statements, Make them orderly for our use. So how to manage that, that’s the topic of this chapter: program structure.

Program structure is a way of organizing programs, which can organize chaotic program statements into orderly programs, so that programs can execute program statements according to our requirements. There are three kinds of program structure: sequence structure, judgment structure and loop structure. Let’s look at each of the three program constructs.

1. Sequential structure

The sequential structure allows the statements of a program to be executed in order from first to last. The so-called “first to last” means that in the entire program, the preceding statements are executed first and the following programs are executed later. It’s a bit like reading a book, looking at the whole page, starting with the top line and working down line by line.

2. Judge the structure

Judgment structure is to let the program according to the result of judgment to execute the corresponding program statement, if the judgment result meets the program requirements, then the program execute the program statement that meets the program requirements, otherwise execute other program statements. Now, if this sounds abstract to you, let’s use pseudocode to demonstrate the structure of the judgment.

There are two common code forms in Java:

if(condition)
{
    //do something
}
else
{
    //do otherthing
}
Copy the code

There’s another way

switch(condition)
{
case 1: 
    do case 1;
    break;
case 2: 
    do case 2;
    break; .default:}Copy the code

A few notes about the switch: condition requires that the variable be an integer, an enumeration constant, a character, or a string constant. Integer types only support variable types: byte,short, and int. Enumerated constants are easy to understand without much explanation. There is only one type of character, char. String constants are common, such as “hello”, but string variables are not.

3. Loop structure

The result of a loop is to have a program statement execute repeatedly according to a loop condition until the loop condition is not met. Again, we use pseudocode to demonstrate the loop structure.

There are four types of loops in Java:

do
{
    //something
}while(condition);

while(condition)
{
    //do something
};

for(; condition;) {//do something
};

for(type v: object)
{
    //do something};Copy the code

Of these four forms, the first three are also seen in C, and only the last is unique to the Java language. It is similar to the use of the for loop and comes in handy when iterating through groups of numbers.

Everybody see officer, on the Java program structure example let’s introduce here, want to know what examples there are behind, and listen to the next decomposition!