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

An example Analysis of the selection structure of Java process Control

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces the example analysis of the selection structure of Java process control, which is very detailed and has a certain reference value. Interested friends must read it!

Boolean expression:

A Boolean expression (Boolean expression) is a code declaration that ends up with only true (true) and false (false) values.

The simplest Boolean expression is equality, which is used to test whether one value is the same as another. For example:

2 = 4? -> false1, if single selection structure

We are often faced with choices, so choosing structure is also very important for our programming, such as:

If it's sunny tomorrow, we'll go to the meadow to fly kites.

As shown in the figure:

If (Boolean expression) {/ / statement to be executed if the Boolean expression is true

Note: if single selection structure: the if statement makes a judgment on the conditional expression. If the judgment is true, the following statement is executed, otherwise it is skipped.

Example solution: receive input from a user and determine whether the input is the string "Liusu":

Import java.util.Scanner;public static void main (String [] args) {Scanner scanner = new Scanner (System.in); / / receive user input System.out.print ("Please input:"); String s = scanner.nextLine (); if (s.equals ("Liusu")) {System.out.println ("input is:" + s);} System.out.println ("End"); scanner.close ();}

Output the demo, as shown in the figure:

Note: the equals method is used to compare strings

2. If double selection structure

For example, if you buy a keyboard, the keyboard costs 108 yuan. If you have 108 yuan or more on you at that time, buy a keyboard, otherwise, don't buy it. (examples only)

So we can't handle such a requirement with an if, we need two judgments, we need a double-choice structure, so we have an if-else structure.

As shown in the figure:

If (Boolean expression) {/ / if the value of the Boolean expression is true} else {/ / if the Boolean expression is false}

If double selection structure: when the conditional expression is true, execute statement block 1, otherwise, execute statement block 2. That's the else part.

Example to help: you go to buy a keyboard, keyboard 108 yuan. If you have 108 yuan or more on you at that time, buy a keyboard, otherwise, don't buy it.

Import java.util.Scanner;public static void main (String [] args) {Scanner scanner = new Scanner (System.in); System.out.print ("Please enter x value:"); int x = scanner.nextInt (); if (x > = 108) {System.out.println ("keyboard");} else {System.out.println ("balance is loading...");} scanner.close ();}

Output the demo, as shown in the figure:

3. If multi-choice structure

There may be many kinds of real situation, and there are interval multi-level judgments. In life, we often have more than two choices, so we need a multi-choice structure to deal with this kind of problem!

As shown in the figure:

If (Boolean expression 1) {/ / if Boolean expression 1 has the value of true execute code} else if (Boolean expression 2) {/ / if the value of Boolean expression 2 is true, execute code} else if (Boolean expression 3) {/ / if the value of Boolean expression 3 is true, execute code} else {/ / if none of the above Boolean expressions execute code for true

If multiple selection structure: the if statement can be followed by else if. Else statement, you can judge a variety of 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.

An if statement can have several else if statements.

Else if before else

One else if statement is detected as true, while other else if and else statements will skip execution.

Example-assisted solution: simple numerical judgment

Public class Test {public static void main (String args []) {int x = 66; if (x = 22) {System.out.print ("XRV 22");} else if (x = 44) {System.out.print ("XRV 44");} else if (x = 66) {System.out.print ("XRV 66") } else {System.out.print ("it seems that your choice is not among the options.");}

Output the demo, as shown in the figure:

4. Nested if structure

Note: nested if... The else statement is also possible. All process control statements can be nested and do not affect each other!

If (Boolean expression 1) {if Boolean expression 1 has the value of true execute code if (Boolean expression 2) {if Boolean expression 2 has the value of true execute code}}

Example help to solve: judge x

Public static void main (String args []) {int x = 66; if (x > 22) {System.out.println ("x > 22"); if (x > 44) {System.out.println ("x > 44");}

Output the demo, as shown in the figure:

5. Switch multi-choice structure

Another way to implement the multi-choice structure is the switch case statement.

The switch case statement determines whether a variable is equal to a value in a series of values, each of which is called a branch.

Switch (expression) {case value: / / statement break; / / optional case value: / / statement break; / / optional / / case... Default: / / optional / / statement}

The switch case statement has the following rules:

Variable types in switch statements can be: byte, short, int, char, string String type.

Switch statements can have multiple case statements.

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.

Note: 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 break statement appears. (break statement is not required)

About default in switch:

The switch statement can contain a default branch-> the last branch (anywhere, but recommended).

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.

Example-assisted solution:

Public static void main (String args []) {char grade = 'Clearswitch switch (grade) {case 'A': System.out.println ("excellent"); break; case 'B': case 'C': System.out.println ("good"); break; case 'D': System.out.println ("pass"); break; case 'F': System.out.println ("Thank you! come on!") ; break; default: System.out.println ("your level is no longer within this range...");} System.out.println ("your level is" + grade);}

Output the demo, as shown in the figure:

6. Add: case penetration

If there is no break statement in the case statement block, after the match is successful, starting from the current case, all subsequent case values will be entered.

Out. If the subsequent case statement block has a break statement, it will jump out of the judgment.

Public static void main (String args []) {int I = 4 switch (I) {case 0: System.out.println ("0"); case 1: System.out.println ("1"); case 2: System.out.println ("2"); case 3: System.out.println ("3"); case 4: System.out.println ("4"); case 5: System.out.println ("5"); break Default: System.out.println ("default");}}

Output the demo, as shown in the figure:

The above is all the contents of the article "sample Analysis of the selective structure of Java process Control". Thank you for reading! Hope to share the content to help you, more related knowledge, 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