In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to analyze Java selection statements, the quality of the article content is high, so Xiaobian shared with you as a reference, I hope you have a certain understanding of related knowledge after reading this article.
Programming languages use control statements to generate flow of execution to effect changes in program state, such as sequential execution and branching. Java program control statements fall into the following categories: select, repeat, and jump. Make your program choose different execution paths based on the result of an expression or variable state selection statement. Iteration statements enable a program to execute one or more statements repeatedly (that is, repeat statements to form loops). Jump statements allow your program to execute in a non-linear manner. All Java control statements are analyzed below.
If you are familiar with C/C++, mastering Java control statements will be easy. In fact, Java control statements are almost identical to those in C/C++. Of course, there are some differences--especially break statements and continue statements.
Java supports two types of selection statements: if statements and switch statements. These statements allow you to control the execution of a program if the state of the program is known only at runtime. If you don't have a C/C++ programming background, you'll be amazed at the power and flexibility of these two statements.
1 if statement
The if statement was introduced in Chapter 2, and we'll discuss it in detail here. If statements are conditional branch statements in Java. It can divide the execution path of the program into two. The full format of the if statement is as follows:
if (condition) statement1;
else statement2;
The objects of if and else are both single statements or blocks. Condition can be any expression that returns a Boolean value. The else clause is optional.
If the condition is true, execute the if object (statement1); otherwise, execute the else object (statement2). At no time can two statements be executed simultaneously. Consider the following example:
int a,b; // ... if(a
< b) a = 0;else b = 0; 本例中,如果a小于b,那么a被赋值为0;否则,b被赋值为0。任何情况下都不可能使a 和b都被赋值为0。 通常,用于控制if语句的表达式都包含关系运算符。当然,这在技术上并不是必要的。仅用一个布尔值来控制if语句也是可以的,如下面的程序段: boolean dataAvailable; // ... if (dataAvailable) ProcessData(); else waitForMoreData(); 记住,直接跟在if 或else语句后的语句只能有一句。如果你想包含更多的语句,你需要建一个程序块,如下面的例子: int bytesAvailable; // ... if (bytesAvailable >0) {
ProcessData();bytesAvailable -= n;} elsewaitForMoreData();
Here, if the variable bytesAvailable is greater than 0, all statements within the if block are executed.
Some programmers find it convenient to use an if statement followed by a brace, even when there is only one statement. This makes it easy to add other statements later, and you don't have to worry about forgetting the brackets. In fact, not defining blocks when they need to be defined is a common cause of error. For example, consider the following program segment:
int bytesAvailable;
// ...
if (bytesAvailable > 0) {
ProcessData();bytesAvailable -= n;
} else
waitForMoreData();
bytesAvailable = n;
Because of the choreography, it seems that the bytesAvailable = n statement should be executed in the else clause. However, white space doesn't matter to Java when you call, and the compiler has no way of knowing your intent. This program will compile, but errors will occur when it is used. The above example should read as follows:
int bytesAvailable;
// ...
if (bytesAvailable > 0) {
ProcessData();
bytesAvailable -= n;
} else {
waitForMoreData();
bytesAvailable = n;
}
nested if statements
A nested if statement is the object of another if or else statement. Nested if statements are often used in programming. When you use nested if statements, the important thing to remember is that an else statement always corresponds to the closest if statement in the same block as it, and that if statement is not associated with other else statements. Here is an example:
if(i == 10) {if(j
< 20) a = b;if(k >100) c = d; // this if iselse a = c; // associated with this else
}
else a = d; // this else refers to if(i == 10)
As the comment shows, the last else statement has no if (j) associated with it
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.