Structure of the program
In general, the structure of the program contains the following three kinds:
Sequential structure
Select structure
Loop structure
When you use switch to make an expression, you can use only numbers or characters in an expression.
PublicClassT{To complete a arithmetic functionPublicStaticvoidMain(StringArgs[]){IntX=3;IntY=6;CharOper=+;Switch(Oper){Case+:{Perform an addition operationSystem.Out.println("x + y ="+(X+Y));Break;}Case‘-‘:{Perform a subtraction operationSystem.Out.println("X-y ="+(X-Y));Break;}Case‘*‘:{Perform multiplication operationsSystem.Out.println("x * y ="+(X*Y));Break;}Case '/' :{//perform division operation system.out. ( "x/y =" + (x /y break } default:{system. Out. ( "Unknown operation! ") break } } } } /span>
In the above operation, there will be a break after each statement, which means exiting the entire switch () statement,
If this statement is not written, all operations will be output after the first satisfy the condition until a break is encountered
PublicClassT{To complete a arithmetic functionPublicStaticvoidMain(StringArgs[]){IntX=1int sum = 0 //Save cumulative results while (x<= 10sum += x //to accumulate operations x++ //Modify loop condition } system.. ( "1--and 10 cumulative result:" + sum ) } } /span>
PublicClassT{PublicStaticvoidMain(StringArgs[]){IntX=1;int sum = 0 //Save cumulative results do{sum += x //perform the cumulative operation x++ }while (x<= 10 system.. ( "1--and 10 cumulative result:" + sum ) } } /span>
PublicClassT{PublicStaticvoidMain(StringArgs[]){int sum = 0 //Save cumulative results for (int x =1; X<=10; X++) {sum += x } system.. ( "1--and 10 cumulative result:" + sum ) } } /span>
Interrupt statement
PublicClassT{PublicStaticvoidMain(Stringargs[]) { for(int i=0; I<ten; I+ +) { If(i= =3) {break ; } System. Out. println("i =" + i) ; } } }< /c21>
Output results
i = 0 i = 1
Using continue is to interrupt the execution of one cycle
PublicClassT{PublicStaticvoidMain(Stringargs[]) { for(int i=0; I<ten; I+ +) { If(i= =3) { continue; } System. Out. println("i =" + i) ; } } }< /c21>
Output results
i = 0 i = 1 i = 2 i = 4 i = 5 i = 6 i = 7 i = 8 i = 9
Copyright NOTICE: This article uses the BY-NC-SA agreement to authorize, reproduced the Wheat Field Technical blog article please indicate the source
Original address: Http://itmyhome.com/2015/03/java-study-notes-judge-and-loop
Java Learning notes 03--judgment and looping statements