In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
Most people do not understand the knowledge points of this article "how to use Java logic control", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to use Java logic control" article.
One, block scope
First of all, before delving into the control structure, you need to understand the concept of block.
Block: a compound statement, which refers to several simple Java statements enclosed by a pair of parentheses. Block determines the scope of the variable. One block can be nested within another block.
Public class TestDemo220425 {public static void main (String [] args) {int n = 10; {int k = 20; int n = 20;}
In this code, the parenthesized part of our main function is the block. One block can be nested within another, but note that in Java, variables with the same name cannot be declared in nested blocks, such as int n = 20 in the inner block of the above code. It should be distinguished here that local variables with the same name will be given priority to local variables when encountered with global variables in Java Cure +, which is not allowed in Java (global variables do not have the concept of global variables, here is just an analogy).
Second, sequential structure
The sequence structure is relatively simple, and it is executed one by one in the order in which the code is written.
The figure shows:
Third, branch structure
The figure shows:
Note: if / else if is a juxtaposition relationship, unlike if else is an either-or relationship, which is called a multi-branching structure.
Several points to note about the branch structure:
1 the expression in parentheses can only be a Boolean expression, nothing that is not 0 is true and 0 is false.
2Code style in Java We recommend the end of line style.
3. Hanging else problem, that is to say, it is best to add parentheses to the if/else statement, so that the code is more readable. If not, when there are multiple if/else statements, the else must match the nearest else.
Public class TeseDemo220424 {public static void main (String [] args) {int x = 10; int y = 10; if (x = = 10) if (y = = 10) System.out.println ("aaa"); else / / this else actually matches if (y = 10) System.out.println ("bbb") }} switch statement problem (key) public class TeseDemo220424 {public static void main (String [] args) {int a = 3; switch (a) {default: System.out.println ("input error!"); break; case 1: System.out.println (1); break Case 2: System.out.println (2); break;}}
Note:
1. No matter where the default is located, as long as it does not match any case, it will go directly to default.
2, every case statement and the break statement after the default statement cannot be omitted, otherwise it will be executed through and through, and the break cannot be dropped except in specific circumstances.
3The keyword in the case,break,continue switch structure is only used in the loop, here is the branch structure, do not be confused.
4 the data types in parentheses can only be byte, short, int, char,String, enum, and then the constant after case matches switch. (note that it can not be long, because plastic surgery will be transferred to int, but there will be a loss of precision from long to int.)
The constant value after case case cannot be repeated, and each case represents a different situation.
6 you can put expressions in parentheses of the if switch, but you can't put some more complex expressions in parentheses like the Jet switch statement.
7Gravity switch statements can be nested, but they don't look good.
Four, cycle
Three major loops: while loop, for loop, do while loop (the specific basic definition of loop will not be introduced, here are just some points to pay attention to)
4.1, error prone point
1, several parts of the cycle, the definition of the loop variable, the condition judgment, the loop body, the iteration of the loop variable. Be sure to pay attention to the iterations of loop variables. if a loop variable does not iterate until it jumps out of the loop, it may lead to an endless loop.
2. Don't tamper with the loop variable inside the loop, even if you need to use your loop variable, use a temporary variable to receive its value, and then use it.
3, it is easy not to use floating point numbers as loop variables.
Public class TestDemo220425 {public static void main (String [] args) {for (double x = 0 * x! = 10 * x + = 0.1) {System.out.println ("");}
Not surprisingly, this code loops, that is, I said to use a floating-point number as a loop variable. Because 0.1 is stored, the decimal part cannot be accurately represented in binary, so we add an approximate value every time, so we may just add it here and skip 10, so the loop goes on all the time.
4.2 public class TeseDemo220424 {public static void main (String [] args) {int I = 1; while (I)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.