In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "Java judgment sentence if … else/switch example usage", in the daily operation, I believe that many people in the Java judgment sentence if. There are doubts about the usage of else/switch examples. The editor has consulted all kinds of materials and sorted out simple and easy-to-use operation methods, hoping to help you answer the doubts of "Java judgment sentence if … example usage of else/switch"! Next, please follow the editor to study!
Java judgment statement-if... Else/switch
.
Java branching structure-if...else/switch
The sequential structure can only be executed sequentially, and can not be judged and selected, so the branch structure is needed.
Java has two branch structures:
1. If statement
2. Switch statement
. . .
If statement
An if statement contains a Boolean expression and one or more statements.
Syntax:
The syntax for the If statement is as follows:
If (Boolean expression)
{
/ / if the Boolean expression is the statement to be executed by true
}
If the value of the Boolean expression is true, the code block in the if statement is executed. Otherwise, the code following the If statement block is executed.
. . .
If...else statement
The if statement can be followed by the else statement, and when the Boolean expression value of the if statement is false, the else statement block is executed.
Syntax:
If... The usage of else is as follows:
If (Boolean expression) {
/ / if the value of the Boolean expression is true
} else {
/ / if the value of the Boolean expression is false
}
. . .
If...else if...else statement
The if statement can be followed by else if. Else statement, which can detect a variety of possible situations.
When using the if,else if,else statement, you need to be aware of the following:
The if statement has at most one else statement, and the else statement comes after all else if statements.
An If statement can have several else if statements, which must precede the else statement.
Once one of the else if statements is detected as true, the other else if and else statements will skip execution.
Syntax:
The format of the if...else syntax is as follows:
If (Boolean expression 1) {
/ / execute code if the value of Boolean expression 1 is true
} else if (Boolean expression 2) {
/ / execute code if the value of Boolean expression 2 is true
} else if (Boolean expression 3) {
/ / execute code if the value of Boolean expression 3 is true
} else {
/ / if none of the above Boolean expressions executes code for true
}
. . .
Nested if... Else statement http://www.iis7.com/a/lm/zzxzgj/
It is legal to use nested if-else statements. This means that you can use if or else if statements in another if or else if statement.
Syntax:
Nested if... The format of the else syntax is as follows:
If (Boolean expression 1) {
/ / execute code if the value of Boolean expression 1 is true
If (Boolean expression 2) {
Execute code if the value of Boolean expression 2 is true
}
}
You can nest else if...else like if statements.
. . .
Switch statement
The switch statement determines whether a variable is equal to a value in a series of values, each of which is called a branch.
Syntax:
The format of the switch syntax is as follows:
Switch (expression) {
Case value:
/ / statement
Break; / / optional
Case value:
/ / statement
Break; / / optional
/ / you can have any number of case statements
Default: / / optional
/ / statement
}
The switch statement has the following rules:
The variable type in the-switch statement can only be byte, short, int, or char.
A-switch statement can have multiple case statements. Each case is followed by a value to be compared and a colon.
The data type of the value in the-case statement must be the same as the data type of the variable and can only be constant or literal constant.
When the value of the variable equals the value of the case statement, the statement after the case statement starts execution and does not jump out of the switch statement until the breakstatement appears.
-when a break statement is encountered, the switch statement terminates. The program jumps to the statement execution after the switch statement. Case statements do not have to contain break statements. If no break statement appears, the program continues to execute the next case statement until the break statement appears.
The-switch statement can contain a default branch, which must be the last branch of the switch statement. Default executes when there is no case statement whose value is equal to the value of the variable. The default branch does not require a break statement.
At this point, the study on the example usage of "Java judgment sentence if … else/switch" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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
Replace some dates of the month with words
© 2024 shulou.com SLNews company. All rights reserved.