JAVA while, for, if, switch, loop control

Source: Internet
Author: User

While Loop statement:

while (Boolean expression)//Boolean expression value is true on execution loop, otherwise false jumps out of loop  

{statement }

 //while cycle, calculate 1 to 100
PackageXunhuan; Public classXun { Public Static voidMain (string[] args) {intSum=0,i,a=1,s=0; for(i=0;i<=100;i++) Sum=sum+i; while(a<101) {s=s+A; A++; } System.out.println (s); SYSTEM.OUT.PRINTLN (sum); }}

Do......while Loop statement:

   Do {

Statement}while (Boolean expression)

 The following: Registration procedure

1  PackageXunhuan;2 3 ImportJava.util.Scanner;4 5  Public classDemo {6      Public Static voidMain (string[] args) {7Scanner sc=NewScanner (system.in); Create a scanner and wait for console input8String zf1= "1"; Create two strings, save two input values9String zf2= "2";Ten          Do{ OneSystem.out.println ("Please enter password"); AZF1 =Sc.nextline (); Enter your password for the first time -System.out.println ("Please enter password"); -ZF2 =Sc.nextline (); Enter the password for the second time the             if(!zf1.equals (ZF2))//Determine if the password is equal to two times -SYSTEM.OUT.PRINTLN ("Password is inconsistent, please re-enter"); -} while(!zf1.equals (ZF2)); The Do...while loop is not exited until the password is incorrect -SYSTEM.OUT.PRINTLN ("Registered successfully! "); +     } -}

For Loop statement (themost commonly used expression in Java )

For (initialization expression 1; Boolean expression 2; expression 3)   {//initialization expression 1: Run once before the start of the loop, can be used as an assignment, run only once;

Statement//Boolean expression 2: Returns a Boolean value, whether the control program continues execution;

} //Expression 3: Typically an assignment expression that resumes execution after a loop is completed;

 PackageXunhuan;/*** Hundred money buy hundred chicken problem *@authorNGB **/ Public classBaiji { Public Static voidMain (string[] args) {inti,j,k;  for(i=1;i<=20;i++){         for(j=1;j<=33;j++){             for(k=3;k<=99;k+=3){                if(i*5+j*3+k/3==100)//Hundred dollars                    if(i+j+k==100) {//Hundred ChickensSYSTEM.OUT.PRINTLN ("Rooster has" +i+ "only" + "hen has" +j+ "only" + "chicks have" +k+ "only"); }            }        }    }}}

1  PackageXunhuan;2 /**3 * Use for loop to calculate 1 plus to4  * @authorNGB5  *6  */7  Public classForxunhuang {8      Public Static voidMain (string[] args) {9         intSum=0;Ten         inti; One          for(i=0;i<101;i++){ Asum=sum+i;} -SYSTEM.OUT.PRINTLN ("The result is:" +sum); -     } the}

foreach statement

Grammar:

for (type X:obj) {statement

}//type x: The loop variable, which in turn assigns the value of obj to x obj: A collection that can be traversed, such as an array

  

 PackageXunhuan;/*** foreach loop, traversing array *@authorNGB **/ Public classForeach_xunhuang { Public Static voidMain (string[] args) {intarr[]={1,2,3,4,5,6};  for(intB:arr) {            intI=0; System.out.println ("Arr[i]=" +b); I++; }    }}

If condition statement

if (Boolean expression) {statement};

if (Boolean expression) {statement} else{statement};

if (Boolean expression) {statement} elseif (Boolean expression) {statement}....else{statement};

Switch Multi-branch statement

 PackageXunhuan;ImportJava.util.Scanner; Public classSwitch_yuju { Public Static voidMain (string[] args) {Scanner sc=NewScanner (system.in); intA; System.out.println ("Please enter a score"); A=Sc.nextint (); Switch(a) { Case10:     Case9:system.out.println ("score is excellent"); Break;  Case8:     Case7:system.out.println ("score is good"); Break;  Case6:system.out.println ("score is passed"); Break;  Case5:     Case4:     Case3:     Case2:     Case1:     Case0:SYSTEM.OUT.PRINTLN ("result is poor"); Break; default: System.out.println ("The result you entered is invalid"); }}}

JAVA while, for, if, switch, loop control

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.