What are the control structures in java
This article focuses on "what are the control structures in java". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "what are the control structures in java"?
Control structure in java:
Conditional structure here are mainly some if,if else conditional statements to control the flow of the code, needless to say. Loop control structure for cycle while cycle do while cycle
1. While and do while
While and do while loops are relatively simple compared to for, and they are used in the same way as the C language, so I won't talk too much about them here. The style is shown below.
While (bool expression) {} do {} while (bool expression)
The do while expression is special in that it is executed at least once.
2. For cycle structure
General for loop structure
For (initialization; Boolean expression; condition update) {/ / Code statement}
Let's take an example.
Public class for_t {public static void main (String [] args) {for (int I = 0; I < 10; iTunes +) {System.out.printf ("I =% d\ n", I);}
The following test results should be known to everyone. It's too easy. That's it.
I = 0i = 1i = 2i = 3i = 4i = 5i = 6i = 7i = 8i = 9
VIP for cycle structure
This usage has been encountered before when learning other object-oriented languages. Maybe I haven't programmed in an object-oriented language for a long time, but I look a little new here. It is used in the following format
For (child elements: collection of elements) {}
Points to pay attention to:
The type of the child element should be the same as the type of the element collection, otherwise an error will be compiled. Such a loop iterates through all the elements in the collection of elements, and we need to use break and continue in the body of the loop to deal with which one needs to be processed and which one doesn't.
Public class for_vip {public static void main (String [] args) {int [] money = {2000, 3000, 4000, 5000}; for (int I: money) {System.out.printf ("I =% d\ n", I);} String [] name = {"arm", "intel", "qulcom", "sprd"}; for (String na: name) {System.out.printf ("name:%s\ n", na.toString ());}}
Experimental results:
I = 2000I = 3000I = 4000I = 5000name:armname:intelname:qulcomname:sprd
At this point, I believe you have a deeper understanding of "what are the control structures in java?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!