Java process control

Java's main process control statements include 3 minutes, selection statements, loop statements and jump statements.

The scope in Java is a pair enclosed in curly braces. Blocks define the range of variables. Each block can be nested. Variables declared in a block can only work in the current block. Note: can two nested blocks of variable scope use public void method() {int n; {int k; N + +; / / the statement is legal / / K scope}         // k++; // The statement is illegal} conditional statement if conditional statement the general form of the if statement is as follows if (condition) {/ / statement block 1} else {/ / statement 2} The condition can be a Boolean value or a Boolean, It can also be an expression with a Boolean return value. Nested if conditional statement if (condition 1) {/ / statement 1 if (condition 2) {/ / statement block 2} else {/ / statement block 3}} else {/ / statement block 4} / / make multiple conditional judgments in the form of ladder if (condition 1) {/ / statement block 1} else if (condition 2) {/ / statement block 2} Else if (condition 3) {/ / condition 3} switch conditional statement processing of multi branch statement switch (expression) {case value 1: / / program statement break; case value 2: / / program statement break; case value 3: / / program statement break          break; } where the expression must be of type byte, short, int or char, and the value \ after the case must be the same type as the expression type or compatible type, and the value value cannot be repeated.

Loop statement there are three forms of loops commonly used in Java: for, while and do while loops. While loop statementThe most basic Java loop statementwhile (condition) {/ / loop body} public class demo4 {public static void main (string [] args) {/ / define an int variable int n = 10; / / use the while loop if n > 0; while (n > 0) {system.out.printin ("n =" + n) ; / / reduce the value of N by 1 N --; Do while loop statement. If the loop condition controlling while is false, The loop body will not execute the do {/ / loop body} while statement. The format of the for loop is as follows: for (initial; condition; iterative operation) {/ / loop body} use the for loop to calculate the sum of integers from 1 to 100.       public class Demo7       {         public static void main(String[] args)          {/ / loop control variables int n; / / and int sum = 0; / / use the for loop to sum for (n = 100; n > 0; n --) {             sum +=n;           }         System. out. Printin ("sum of integers from 1 to 100:" + sum)}} / / the program runs the following sum of integers from 1 to 100: 5050 jump statements. Three jump statements, break statements, continue statements and return statements are supported in Java. The break statement has three main purposes: first, it can be used to jump out of the switch statement, second, the break statement can finally jump out of the loop body, and third, it can be used to jump out of the larger than statement. Continue statement although the break statement can jump out of a loop, sometimes it is necessary to stop the rest of a loop and sometimes continue to execute the next word loop. At this time, the continue statement is required.         public class Demo14         {             public static void main(String[] args)             {               for (int i = 1; i < 51; i++)                {                 System.out.print(i+" ");                 if(i%5!=0) / / continue the loop when N 5 cannot divide by 5.                 continue;                 else                   System. out. Println ("*********"}}} return statement return statement is used for the return displayed by a method. It gives program control to the caller of the method. This statement is often called in the method. Public class demo16 {public static void main (string [] args) {for (int i = 0; I < 10; I + +) if (I < 5) system.out.printin ("the" + I + "cycle") else if (I = = 5)         return; / / the following statement will never execute else system out. Printin ("the + I + Th cycle")}}

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>