This is the third day of my participation in the August Text Challenge.More challenges in August

Day 3 of learning Java from 0

Most languages provide two basic flow control constructs: branch and loop.

In fact, all logic can be done with these two structures, but with great complexity.

Branching structure

If the else structure

// Simple iF
if(a)// The expression results in true OR false
{
    //true executes the logic
}
//if else
if(a)// The expression results in true OR false
{
    //true executes the logic
}
else
{
    //false executes the logic inside
}
//if else if else
if(True OR False)
{
}
else if(True OR False)
{
}
else{}Copy the code

If else is the most common.

If else if should be used sparingly.

It’s easy.

switch

Switch is a little more complicated.

switch(expression)//expression specifies a value, such as a number or a string
{
    case codition1:
     {
        ...
        break;
     }
     case codition1:
     {
        ...
        break; }...default: {}}Copy the code

If the values in the switch() bracket are the same as those in case codition1, then the code block behind the default tag will be executed if all values behind the case tag are not the same as those behind expression.

There is a break after each code block after the case tag; The “break” statement terminates the switch program and does not execute subsequent cases, otherwise it will continue to execute.

Loop structure

There are three types of loops: for, while and foreach.

The first for is the most popular.

The second while is used less frequently. While loop there’s a do while loop, which is even less used, and it’s been working for three years, and I haven’t used it once.

The third foreach is used more often than while but less often than for. The foreach loop is a special, simplified version of the for loop, just to iterate over a value. We don’t need to get into this for a while.

// Execute the loop body if the condition is met
for(Condition) {loop body}// Execute the loop body if the condition is met
// Note the dead loop
while(Condition) {loop body}// Execute the loop body first. If the condition is met, execute the loop body
do{loop body}while(conditions)//foreach    
for(Type variable name: collection) {statement block; }Copy the code

An array of

Arrays are a little bit more difficult, although arrays aren’t really used that much, they’re basically collections, but collections are also wrapped in arrays, so you still have to learn arrays.

An array is also a data type, so the elements in an array are the same.

//type[] arrayName
// Define an array
int[] a;
string[] b;

// Initialize both ways, but cannot specify both length and value
a=new int[] {1.2.2.2.3.1};// Static initialization, specify the value
a=new int[10];// Dynamic initialization, specify the length

// If you want to write this, you can write it together
int[] a=new int[] {1.2.2.2.3.1};// Static initialization
int[] a=new int[10];// Dynamic initialization
Copy the code

After the array is defined, for each value in the array, there is a corresponding index, that is, index. The array is accessed through the index.

if(a[0] = =1)// if a[0] = 1
{
    a[0] =3;// Change a[0]
}
Copy the code

A [0] is the first value of the array, the sequential index.

Therefore, the index cannot be less than 0, and cannot be greater than or equal to the length of the array, ranging from 0 to -1.

This is where the foreach loop mentioned above comes in handy

public static void main(String[] args)
{        
     int[] a=new int[] {1.2.1.34.4.1.1};
    //for loop
     for (int i = 0; i < a.length; i++) 
     {
        System.out.println(a[i]);
     }
    
    / / foreach writing
     for (vari : a) { System.out.println(i); }}Copy the code

Either way, it’s the same thing.

But the I in the for loop is the index, so if you want to get the value, you have to get it by the index, and the I in the foreach loop is the element in the array, so you don’t need to get it by the index.

Foreach’s value is only a temporary variable. The value of an array element is assigned to I. Changing the value of I does not change the value of the array.

As a bit of a joke, C# is much better understood than foreach statements in C#.

C# :

foreach(var i in a)
{
    
}
Copy the code

Guys, I got a point there.

That’s all you need to know about arrays, and you can fill them in later if you need to.