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 analyze Java logic control

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Today, I will talk to you about how to analyze the logic control of Java. Many people may not know much about it. In order to make you understand better, the editor has summarized the following for you. I hope you can get something according to this article.

Sequential structure

The sequential structure is executed according to the code from top to bottom, and the program we run is the sequential structure from top to bottom, and when we encounter a method, we execute the method. For example:

System.out.println ("aaa"); System.out.println ("bbb"); System.out.println ("ccc")

When you change the order of the code, the output is also different. For example:

System.out.println ("bbb"); System.out.println ("ccc"); System.out.println ("aaa")

Branch structure if statement

The basic syntax of the if statement (single judgment):

If (Boolean expression) {

/ / execute code when the condition is met

}

The basic syntax of the if statement (plus negative):

If (Boolean expression) {

/ / execute code when the condition is met

} else {

/ / execute code when the condition is not met

}

The basic syntax of the if statement (multi-branch):

If (Boolean expression) {

/ / execute code when the condition is met

} else if (Boolean expression) {

/ / execute code when the condition is met

} else {

/ / execute code when none of the conditions are met

}

Many else if can be written in if statements, but only one if and else can be written.

Code example 1: determine whether a number is odd or even:

Int num = 10 if (num% 2 = = 0) {System.out.println ("num is even");} else {System.out.println ("num is odd");}

Code example 2: determines whether a number is positive or negative.

Int num = 10 System.out.println if (num > 0) {System.out.println ("num is positive");} else if (num < 0) {System.out.println ("num is negative");} else {System.out.println ("num is 0");}

Code example 3: determine whether a year is a leap year.

Because the law of a leap year is: if the year is a multiple of 4 and not a multiple of 100, then it is a leap year. Or this number can be divisible by 400.

Scanner scanner = new Scanner (System.in); int I = scanner.nextInt (); if ((I% 4 = = 0 & & I% 100! = 0) | | (I% 400 = = 0) {System.out.println (I + "is a leap year");} else {System.out.println (I + "not a leap year");}

Here is to enter a number through Scanner.

Drape else problem

What does this code output?

Int x = 10 int y = 10 X if (x = = 20) if (y = = 10) System.out.println ("aaa"); else System.out.println ("bbb")

Nothing is output here because the drape else is involved. Else only matches the most recent if, where the nearest if is yearly if 10, so nothing is output.

So we usually write code, we must add parentheses, so that it is convenient for others to read the code, but also convenient for us to modify it.

Switch statement

Basic syntax:

Switch (integer | enumeration | character | string) {

Case content 1: {

Execute statement when content is satisfied

[break;]

}

Case content 2: {

Execute statement when content is satisfied

[break;]

}

...

Default: {

Execute the statement when none of the contents are satisfied

[break;]

}

}

Switch statement is our common multi-branch statement, which can execute the corresponding case statement according to the corresponding value of the variable in switch parentheses. And there must be a break under each case statement, otherwise the following case statement will continue to execute. When there is no matching case statement in the switch statement, the default statement is executed. Switch can also be nested, but it will be very messy after nesting, making the code very difficult to read. So try not to nest.

Code example:

Int day = 7 switch (day) {case 1: System.out.println ("Monday"); break; case 2: System.out.println ("Tuesday"); break; case 3: System.out.println ("Wednesday"); break; case 4: System.out.println ("Thursday"); break Case 5: System.out.println ("Friday"); break; case 6: System.out.println ("Saturday"); break; case 7: System.out.println ("Sunday"); break; default: System.out.println ("this number is not a week"); break;}

Cycle structure while cycle

Basic syntax:

While (cyclic condition) {

Loop statement

}

It is important to note that if the loop condition is true, the loop statement is executed, otherwise the loop is ended. For example, find the sum of 1-10:

Int n = 1int sum = 0bot while (n)

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