My thinking in Java Study Notes (5)

Source: Internet
Author: User
Process Control
The process control statement in Java includes if-else whlie do-whlie for switch-case but does not provide Goto. in Java, It is a reserved word, not a keyword (it may be ~ in the future ~)
True and false
Java does not support using numbers to represent boolean values. If you must use them, convert them to boolean values using expressions, such as if (a = 0)
If-Else
This process control statement is probably the most commonly used, among which else can be used
If (Boolean) if (Boolean)
Statement statement
Else
Or statement
Statement can be either a separate statement or a composite statement composed of several statements. When a composite statement is used, it must be enclosed in braces. However, a separate statement may be inappropriate.
For example
Public class test
{
Public static void main (string ARGs [])
{
Int I = 0;
If (I> = 0) // do not add the number here.
{
System. Out. println ("I> = 0 ");
}
Else
{
System. Out. println ("I <0 ");
}
}
}
Return
Keyword return: specify the value to be returned by the function and return immediately. Here we need to know that the expression range of the return value type is equal to or less than the return type of the function. For example, if the return value of the function is int, the compiler will hold an error. Otherwise, it will not
Public class test
{
Public static double go ()
{
If (1> 0)
{
System. Out. println ("hello ");
Return 100l; // return 100 of a long type.
//! System. Out. println ("hello"); note that no statement can be added after return. Return is the end point of a program block.
}
Else
Return 100f; // return 100 of the flout type. // note that when if-else is used to return a value respectively, add return after if and Else.
}
Public static void main (string ARGs [])
{
System. Out. println (go ());
}
}
You can also use return without returning values to jump out of the function, but note that if you want to use this method, the return value type of the function must be void.
Iteration
The while do-while for keywords are used to control loops, which are sometimes attributed to iterative statements.
While
Example while (Boolean)
{
//......
} // Execute the internal code until the Boolean value is set to false.
Do-while
Example Do
//........
While (Boolean)
//......
The only difference between him and while is that do-while will be executed at least once. Even if the true running condition is not met at the beginning, the statement to be executed at least once should be placed in do.
For
The for loop performs the following steps: 1. initialize the variable; 2. determine the conditions. 3. Perform one step after each execution.
For example, for (INT I = 0; I <10; I ++) First initializes I and determines whether I is less than 10. If it is less than the statement in the for loop, add the value of I to 1 until I is greater than 10. The three steps can be empty, that is, for (;). The meaning is the same as that of while.
You can also define multiple variables in the for statement, but their types must be the same. This defined method can only be used in the for statement.
For (INT I = 0, j = 1; I <10 & J! = 11; I ++, J ++)
Note that the variables defined in for parentheses can only be used and survive in the expression control of the for loop, rather than in the braces of the loop body!
Break and continue
In iterative statements, you can use break and continue to control loop statements. The only difference is that break jumps out of the loop and does not execute the remaining part, while continue stops the current iteration, return to the cycle and start the next iteration.
Public class test
{
Public static void main (string ARGs [])
{
For (INT I = 0; I <100; I ++) // 2
{
If (I = 88)
Break; // The value of I will never be greater than 88, because if it is greater than 88, the program will jump out and reach the position 1.
If (I % 2! = 0)
Continue; // when I is an even number, the program will execute downward and print the I value. If it is not an even number, it will jump to 2 and reloop I ++.
System. Out. println (I );
} // 1
}
}
Actually, Which I = 88 is a nonsense and can be controlled in the for loop? I just gave an example.
The notorious "Goto"
Since the birth of the first programming language, the keyword goto exists. However, many problems arise due to the improper use of most programmers. This is not a goto problem. Just like guns, good people use weapons, and bad people use weapons ~ Hey hey ~
Java does not have a Goto, but it has a statement similar to the Goto function ----- mark, you can execute a jump-like function, but it is not really a Skip, instead, it is a way to interrupt iteration statements that can be shared with break and continue. In this way, all nested loops in progress can be interrupted until the label is located. A tag is an identifier followed by a colon, for example, thisislabel :. The only place in Java that can put a label is before the iteration statement starts. do not add anything between the label and the iteration statement.
Rules
1. Continue jumps to the top of its inner loop to continue execution.
2. The contine label jumps to the position where the label is located, and then enters the loop again.
3. The break will skip the cycle.
4. The break label will jump from the location where the label is located.
Label example in while
Public class test
{
Public static void main (string ARGs [])
{
Int I = 0;
Onelabel:
While (true)
{
System. Out. println ("this is while method ");
While (true)
{
I ++;
System. Out. println (I );
If (I = 1)
{
System. Out. Print (I );
System. Out. println ("continue ");
Continue;
}
If (I = 3)
{
System. Out. Print (I );
System. Out. println ("Continue onelabel ");
Continue onelabel;
}
If (I = 5)
{
System. Out. Print (I );
System. Out. println ("break ");
Break;
}
If (I = 7)
{
System. Out. Print (I );
System. Out. println ("break onelabel ");
Break onelabel;
}
}
}
}
}
For label examples
Public class test
{
Public static void main (string ARGs [])
{
Int I = 0;
Onelabel:
While (true)
{
System. Out. println ("this is while method ");
For (; I <8; I ++)
{
System. Out. println (I );
If (I = 1)
{
System. Out. Print (I );
System. Out. println ("continue ");
Continue;
}
If (I = 3)
{
System. Out. Print (I );
System. Out. println ("Continue onelabel ");
I ++; // The continue onelabel will jump out of the for loop, but it does not carry out a complete iteration, so accumulation does not happen. For your supplement, we will put I + 1 here
Continue onelabel;
}
If (I = 5)
{
System. Out. Print (I );
System. Out. println ("break ");
I ++; // The break will jump out of the for loop, so there is no complete iteration, so accumulation will not happen. To supplement you, we will put I + 1 here
Break;
}
If (I = 7)
{
System. Out. Print (I );
System. Out. println ("break onelabel ");
Break onelabel;
}
}
}
}
}
Remember! The only reason for using label in Java is: In a nested loop, the break and continue should be crossed by more than one nested
Switch
Switch (I)
{
Case 0: system. Out. println ("0 ");
Break;
Case 1: system. Out. println ("1 ");
Break;
Case 2: system. Out. println ("2 ");
Break;
Case 3: system. Out. println ("3 ");
Break;
Default: system. Out. println ("default ");
}
A switch is a selection statement that selects a qualified value based on the value generated by an expression for execution. Each case is followed by a break. If there is no break, he will find a qualified statement and continue to execute the following sentence, note that the value after the case cannot be repeated until the break is met. If the value does not match, the system will automatically execute default. In addition, swtich can only determine the basic integer data type. It cannot be a string or floating point number.
Exercise answers
5. Write a program to stop it when it is printed to 47 (range: 100). Use the keyword break to try return.
Public class test
{
Public static void main (string ARGs [])
{
For (INT I = 0; I <100; I ++)
{
If (I <47)
System. Out. println (I );
Else
Break; // return;
}
}
}
7. Write a program to generate 24 random numbers. For each value, use the IF-else statement to identify whether the value is greater than, less than, or equal to the next random number.
Import java. Math .*;
Class Test
{
Public static void main (string ARGs [])
{
Int num [] = new int [25];

For (INT I = 0; I <25; I ++)
{
Num [I] = (INT) (math. Random () * 100 );
}
For (INT I = 0, j = 1; I <25 & J <25; I ++, J ++)
{
System. Out. println (J + "" + num [I] + "" + num [J]);
If (Num [I]> num [J])
{
System. Out. println (Num [I] + ">" + num [J]);
}
If (Num [I] <num [J])
{
System. Out. println (Num [I] + "<" + num [J]);
}
If (Num [I] = num [J])
{
System. Out. println (Num [I] + "=" + num [J]);
}

}
}
}
As for question 8th, we have not learned how to listen to events, so this question is of little significance.
Other questions are also very simple. You can ask me if you have any questions.
 
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.