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

A tutorial on the methods of logical Control statements in Java Program Sequential structure

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

Share

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

This article introduces the relevant knowledge of "the method tutorial of logical control statements in the sequential structure of Java program". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Catalogue

one。 Sequential structure

two。 Branching structure

1.if statement

2.switch statement

three。 Cyclic structure

1. While loop

2. Break

3. Continue

4.for cycle

5.do while cycle

four。 Input and output

1. Output to console

2. Enter from the keyboard

five。 Practice

Enjoy Meitu before looking!

Get into talent!

Catalogue one. Sequence structure II. Branch structure 1.if statement 2.switch statement three. Loop structure 1. While loop 2. Break3. Continue4.for cycle 5.do while cycle four. Input and output 1. Output to console 2. Enter five from the keyboard. Practice

one。 Sequential structure

The sequential structure is relatively simple. For example, the code we wrote before is sequentially structured, and it can be executed one by one in the order in which the code is written.

System.out.println ("1"); System.out.println ("2"); System.out.println ("3"); System.out.println ("4"); / / Sequential structure`

Results:

two。 Branch structure 1.if statement

Basic grammatical form 1

Basic grammatical form 2

Basic grammatical form 3

Code example: determine whether a number is odd or even

Public static void main (String [] args) {int n = 10; if (n% 2 = = 0) {/ / the yellow box is a warning that the computer thinks it must be the real System.out.println ("even");} else {System.out.println ("odd");}}

Results:

2.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;]

}

}

Code example: outputs the week based on the value of day

Public static void main (String [] args) {int day = 1; 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 ("incorrect typing"); break;}}

Results:

Depending on the value in switch, the corresponding case statement is executed. The case statement ends when a break is encountered.

If the value in switch does not have a matching case, the statement in default is executed.

We suggest that a switch statement should be accompanied by default.

Note 1 break should not be omitted, otherwise the effect of "multi-branch selection" will be lost

Note 2 values in switch can only be integers | enumerations | characters | string

Note 3 switch cannot express complex conditions

Note 4 although switch supports nesting, it is ugly ~

three。 Loop structure 1. While loop

Basic grammatical format:

While (cyclic condition) {

Loop statement

}

If the loop condition is true, the loop statement is executed; otherwise, the loop ends.

Code example 1: calculate the sum of numbers from 1 to 100

Public static void main (String [] args) {int n = 1; int result = 0; 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