Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Java block scope, conditional statements and switch statements

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the Java block scope, conditional statements and switch statements how to use the relevant knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that you read this Java block scope, conditional statements and switch statements how to use the article will have a harvest, let's take a look at it.

Block scope

Before further studying the control structure, it is necessary to understand the function of the block.

Definition: a statement consisting of multiple Java statements enclosed in a pair of curly braces.

Function: the block determines the scope of the variable, and one block can be nested on the other.

Example:

Package decom1;public class cuowu {public static void main (String [] args) {/ / the second block is nested within the first block. Byte I = 12; / / variable I only works in the second block area, including the blocks within the nesting. {/ / the third block is nested in the second block as well as in the first block. Int a = 3; / / variable an only works in the block in which it is located. System.out.println (a);} / / A block of code written in main (the entrance to program execution) is called a local block of code. / / the role of local code: can make variables disappear in memory earlier, saving memory space. System.out.println (I);}}

You cannot declare a variable with the same name in two nested blocks.

Example:

Package decom1;public class cuowu {public static void main (String [] args) {byte I = 12; {int I = 3; / / error: Duplicate local variable i} System.out.println (I);}} conditional statement

There are three formats of conditional statements. Let me decrypt which three formats.

Format one

If (conditional expression) {sentence aspect;}

The representation of conditional statements in Java:

If (condition) statement

The conditions here must be enclosed in parentheses.

The final result of a conditional expression can only be of type boolean, either true or false.

Process:

1. If the program executes the if statement, it will see whether the result of the conditional expression is true or false.

two。 If it is a true, it goes into the if and executes the body of the statement.

3. If it is false, it will not enter the if and the body content of the statement will not be executed.

Package com;public class liu {public static void main (String [] args) {int I = 1; int j = 2; if (I > j) {System.out.println (I);} System.out.println (j) / / because I > j is not valid, the statements inside the if are not executed, and the outer statements are skipped. Format two

If (conditional expression) {statement aspect;} else {sentence aspect;}

The form of sentence expression:

If (condition) statement1 else statement2

Perform the process:

1. If the program executes the if statement, it will see whether the result of the conditional expression is true or false.

two。 If it is a true, it goes into the if and executes the body of the statement.

3. If it is false, it will not enter the if, it will enter the else and execute the body of the statement.

Example:

Package com;public class liu {public static void main (String [] args) {/ / get the larger values of two numbers int I = 1; int j = 2; int max = 0; if (I > j) {max = I / / assign I to max} else {max = j; / assign j to max} System.out.println (max); / / execute the statement in else because the I > j condition is false, so max gets a value of 2. Format 3 (commonly used)

If (conditional expression) {statement aspect;} else if {sentence aspect;} … Else {sentence style;}

Statement expression:

If... Else if...

Perform the process:

1. If the program executes the if statement, it will see whether the result of the conditional expression is true or false.

two。 If true, the body content of the statement in if will be executed, and other statement bodies will not be executed.

3. If false, it continues to go down to see whether the conditional expression of else if results in true or false.

4. If it's true, go into elseif and execute the body of the statement.

5. If it's false, keep going down.

6. If the conditional expression in if and all elseif is false, the body content of the statement in else is executed.

Example:

Package com;public class liu {public static void main (String [] args) int a = 0; int I = 7; if (I > 8) {a = 1;} else if (I > 7) {a = 2 } else if (I > 6) {a = 3;} else {a = 4;} System.out.println (a);}} switch statement

If conditional statements are a bit clumsy when dealing with multiple options so why not have a new way of playing at this time? Let me introduce the switch statement.

The structure is put into an example, and the following is about the execution process:

1. When the program executes to the switch, it enters the switch, finds the first case to match, and if the match succeeds, it goes to the case to execute.

two。 The content of the sentence and the break. If the match is not successful, it will continue to go down and find the second case to continue the match. ... no, no, no.

3. If all the case do not match, the body content of the statement in the default is finally executed.

Example:

Package com;public class liu {public static void main (String [] args) {int I = 3; switch (I) {case 1: System.out.println ("1"); break Case 2: System.out.println ("2"); break; case 3: System.out.println ("3"); / / iTun3 conforms to case 3, so execute the commands in case, regardless of the rest of the statements. Break; default: System.out.println ("3"); break;}}

Case tag:

A constant expression of type char, byte, short, or int.

Enumerates constants.

Starting with Java 7, case tags can be literal strings.

Character constant example:

String input....switch (input.tolowerCase ()) {case "yes":... Break;...}

Warning: if there is no break statement at the end of the case branch statement, the next case branch statement will be executed.

If you are easy to forget this stubble, you can add this statement in front of it, so that if a break is missing after case, an error will be prompted during compilation.

Javac-Xlint:fallthrough Test.java

Switch end flag:

1.break

two。 Encounter the end}

This is the end of the article on "how to use Java block scope, conditional statements and switch statements". Thank you for reading! I believe you all have a certain understanding of "Java block scope, conditional statements and how to use switch statements". If you want to learn more, you are welcome to follow the industry information channel.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report