Process Control The process control statement in Java has IF-else whlie do-whlie for switch-case but does not provide goto, he is reserved in Java, not a keyword (possibly there will be oh later ~) True and false java Support with numbers represents the boolean value, if you must use it, first convert him to a boolean value, such as if (a == 0) IF-ELSE this process control statement is prioritized, where else There may be possible to use if (boolean) statement statement else or statement where Statement can be a separate statement, or a composite statement composed of several statements, when using a composite statement, be sure to use braces Incorporated, and separate statements, it is possible to use an example of use. Public class test {public static void main (string args []) {INT i = 0; if (i> = 0) // Here you need to pay attention Don't add it; {System.out.println ("i> = 0");} else {system.out.println ("i <0");}}}}}}}} RETURN keyword Return's role: specify The function needs to be returned and immediately returned.
Here you need to know 2 points, the type of expression of the return value is equal to or less than the return type of the function, for example, the function return value is int, if you return a double value, the compiler will be wrong, but it will not Public class test {public static double go () {if (1> 0) {system.out.println ("Hello"); Return 100L; // Returns 100 ////! system.out.println (" "); Note that after return is not added any statements, return is the endpoint of a block} Else Return 100F; // Returns 100 // Note to the IF-ELSE to return a value. When I remember to add Return} public static void main (String args []) {system.out.println (String args []) {system.out.println (});}} You can also use Return that does not return. To jump out of the function, but pay attention, if you want to use this method, then the function's return value must be a void iteration while do-while for these groups of keywords are used to control cycles, and they are sometimes concatenated Iterative statement. While example while (boolean) {// ...} // Continue to perform internal code until the Boolean becomes false to do-while Examples do // ............. WHILEAN) / / ... His only difference between him and While, do-while will be at least once, even if you don't meet the conditions of True run, at least one statement is to be put in the DO's inside for for for for for for for for for for for for for for for for for for for for for for for for for for for. The following step 1 is performed, and the initialization variable 2 is initialized. The condition determination 3 is performed. Each time is performed once, it is performed, for example, for (INT I = 0; i <10; i ) will initialize i, determine if i is less than 10, If less than the statement within the FOR cycle, the value of I is 1, until I is greater than 10, exit. The 3 steps can be empty, ie for (;;), his meaning is the same as the while. You can also define multiple variables in the for statement, but their type must be the same, this definition method can only use for for for for for (int i = 0, j = 1; i <10 &&j! = 11; i , j ) Pay attention to the variables defined in FOR parentheses, can only be used and survive in the expression control formula of the FOR cycle, rather than in the braces of the cyclic body! Break and Continue In an iterative statement, you can use Break and Continue to control the loop statement, their only difference is that Break will jump out of the loop, do not perform the remainder, and Continue will stop the current iteration, return to the revelation of the loop Start the next iteration.
Public class test {public static void main (string args []) {for (int i = 0; i <100; i ) // 2 {if (i == 88) BREAK; // This sentence causes the value of I Never greater than 88, because if it is greater than 88, the program will jump out, which position IF (i% 2! = 0) Continue; // When I is a noble, the program will be executed down, Print the value of i, if not even, jump to 2, recirculate the i again system.out.println (i);} // 1}} In fact, which i == 88 is a nonsense, you can cycle It can be controlled in it, I just got an example, huh, huh, "Goto" from the first program language, there is a goto this keyword, however, because most programmers are inappropriate, Many problems have been caused. This is not a goto problem, just like guns, good people use it is a weapon, the bad guys use the weapon ~ 嘿嘿 ~ Java has no goto, but there is a statement similar to the GOTO function - the mark can perform similar jumping function But not a real jump, but an interrupt iterative statement. He can share with Break, Continue. The shared effect is to interrupt all in the nesting cycle until the Label. The tag is actually the label that follows the colon, for example thisislabel:. The only place in Java can put Label is before the iterative statement begins, please do not add anything between the Label and iterative statements.
Using Rules 1, Continue will jump to the top of his inner layer, continue to execute 2, and Contine Label will jump to the location of Label, then re-enter the loop 3, Break will jump away from the loop 4, Break Label will jump away from Label Location WHILE uses Label example public class test {public static void main (string args []) {INT i = 0; Onlabel: While (True) {system.out.println ("this is while method"); while ( True) {i ; system.out.println (i); if (i == 1) {system.out.print (i); system.out.println ("continue); continue;} if (i == 3) {system.out.print (i); system.out.println ("Continue ONELABEL"); Continue ONELABEL;} if (i == 5) {system.out.print (i); system.out.println ("Break"); Break;} if (i == 7) {system.out.print (i); system.out.println ("Break Onlabel"); Break ONELABEL;}} }}} For use Label example public class test {public static void main (string args []) {INT i = 0; OnLabel: While (true) {system.out.println ("this is while method"); for ( i <8; i ) {system.out.println (i); if (i == 1) {system.out.print (i); System.out.Println ("Continue"); Continue;} IF i == 3) {system.out.print (i); system.out.println ("Continue OnLabel"); i ;
// Continue Onlabel will jump out of the for loop, so there is no complete iteration, so the accumulation will not happen, in order to make up, we are here to put i 1 Continue OnLabel;} if (i == 5) {system.out. Print (i); system.out.println ("Break"); i ; // Break will jump out of the for loop, then no complete iteration, so tired will not happen, in order to supplement, we are here to put i 1 Break;} if (i == 7) {system.out.print (i); system.out.println ("Break Onlabel"); Break Onlabel;}}}}} Everyone should remember! The only reason to use Label in Java is: In the nesting cycle, you want Break and Continue over more than one nested Switch Switch (i) {Case 0: System.Println ("0"); Break; Case 1: System.Out.println ("1"); Break; Case 2: System.Out.println ("2"); Break; Case 3: System.out.println ("3"); Break; Default: System.out.println ("default");} Switch is a selection statement, and he picked out a qualified manner according to the value generated by an expression. Each CASE will end with a break. If there is no Break, he will find the following sentence until you have stopped it, when you encounter Break, it is necessary to pay attention to where you need it. You can repeat that if you are not in line with, the system will automatically execute the default.
Also, SWTICH can only determine the basic data type of the integer, the string or floating point number is the exercise answer 5, write a program, let him stop when printing to 47, using keyword break, try Test Return Public Class Test {Public Static Void Main (String Args []) {for (INT I = 0; I <100; I ) {IF (i <47) System.out.Println (i); else Break; / / return;}}} 7, write the program, generate 24 random numbers, for each value, use if-else statement to distinguish whether it is greater than, less than, equal to the next random number Import java.math. *; Class Test {public Static void main (string args []) {int num [] = new int [25]; for (int i = 0; i <25; i ) {NUM [i] = (int) (Math.random () * 100);} for (int I = 0, j = 1; i <25 && j <25; i , j ) {system.out.println (j " NUM [i] " Num [j]); IF (NUM [I]> NUM [J]) {System.out.Println (NUM [i] "> NUM [J]);} IF (NUM [i] / 163.com QQ: 31349283 We can learn progress together! Welcome to my blog, http://blog.9cbs.com/maoerzuozuo There are more learning content inside!