Java Learning notes 03--judgment and looping statements

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.