Control the execution process-(Java Learning Note III)

Source: Internet
Author: User

If-else the most basic form of control program flow

Format:
if (boolean-expresion) {
Statement
}
Or
if (boolean-expresion) {
Statement
}else{
Statement
}
Cases:

 Public Static intTestif (intTestVal,inttarget) {  intresult = 0; if(TestVal >target) {Result+=1; }Else if(testval<target) {Result-=1; }Else{result= 0; }  returnresult;} Public Static voidMain (string[] args) {intTest1 = Testif (10, 9);  System.out.println (test1); //1}
Iteration Statements:while, do-while, for control loops, also called iteration statementsExample: Use while, do-while, for statement to ask 1! + 2! +...+10! factorial and.
1 While loop format:2    while(Boolean-expresion) {3 Statement4     }  5 While loop to find factorial:6     intI1 = 1, sum1 = 0, jc1 = 1;7    while(i1<=10){8JC1 = JC1 *I1;9SUM1 = sum1 +jc1;Teni1++; One   } ASystem.out.println (SUM1);//4037913
1  Do-While loop format:2      Do{3 Statement4} while(Boolean-expresion);5 6  Do-While loop to find factorial:7     intI2 = 1, sum2 = 0, JC2 = 1;8    Do{9JC2 = JC2 *I2;Tensum2 = sum2 +JC2; Onei2++; A} while(i2<=10); -System.out.println (SUM2);//4037913

The only difference between a while statement and a do-while statement is that the Do-while statement executes at least once, even if the do-while loop expression evaluates to false the first time, and the while loop evaluates to False for the first time and its statement does not execute at all

The for For statement is a frequently used iteration form whose format: for (initialization;boolean-expresion;step) {statemnet} initialization initialization expression; boolean-expresion boolean expression; step stepping
 1   for loop factorial:  2  int  i3, sum3 = 0, jc3 = 1 3  for  (i3 = 1;i3 <= 10;i3++ 4  jc3 = i3 * jc3;  5  sum3 += jc3;  6   7  System.out.println (SUM3); // 4037913  
Return Keywords: Two purpose: Specifies the return value of a method and causes the current method to exit, that is, the statement following the return statement will not be executed, and return that value. Break and continue can be used in the body portion of any iteration statement to control the looping process with break and continue: to forcibly exit the loop, not to execute the remaining statements in the loop continue: Stop executing the current iteration statement, then exit the start of the loop, and re- Sub-iteration

Switch switch is divided into SELECT statements.       Depending on the integer expression, the switch statement can be selected from a series of code to execute the switch statement format: switch (key) {case value:statement break;       Case Value:statement break;      。。。  Default:statement break; }key: Represents an integer selection factor value: An integer that is a switch that compares the result of the entire expression to each case value, executes the corresponding statement if found to be compliant, executes the default statement if no match is found
1 Example: 9x9 multiplication Table2Method One: Loop nesting ( for Switch)3   Public Static voidMain (String args[]) {4   //9x9 Multiplication Table5  6   inti,j;7   //outer control row, inner control column8System.out.println ("*************** method one: ******************");9    for(i = 1;i<=9;i++){Ten     for(j = 1; J <= 9;j++){ One     Switch(j) { A      Case1: -System.out.print (j+ "x" +i+ "=" +i*j); -        Break; the      Case2: -      if(i>=2){ -System.out.print ("\ T" +j+ "x" +i+ "=" +i*j); -        Break; +      } -      Case3: +      if(i>=3){ ASystem.out.print ("\ T" +i+ "x" +j+ "=" +i*j); at        Break; -      } -      Case4: -      if(i>=4){ -System.out.print ("\ T" +i+ "x" +j+ "=" +i*j); -        Break; in      } -      Case5: to      if(i>=5){ +System.out.print ("\ T" +i+ "x" +j+ "=" +i*j); -        Break; the      } *      Case6: $      if(i>=6){Panax NotoginsengSystem.out.print ("\ T" +i+ "x" +j+ "=" +i*j); -        Break; the      } +      Case7: A      if(i>=7){ theSystem.out.print ("\ T" +i+ "x" +j+ "=" +i*j); +        Break; -      } $      Case8: $      if(i>=8){ -System.out.print ("\ T" +i+ "x" +j+ "=" +i*j); -        Break; the      } -      Case9:Wuyi      if(i>=9){ theSystem.out.print ("\ T" +i+ "x" +j+ "=" +i*j); -        Break; Wu      } -     } About    } $System.out.println (""); -   } -System.out.println ("*************** Method II: *********************"); -   inti1,j1; A    for(I1 = 1;i1<=9;i1++){ +     for(J1 = 1; J1 <= i1;j1++){ theSystem.out.print (i1+ "x" +j1+ "=" +i1*j1+ "\ T"); -    } $System.out.println (""); the   } the  } the}

Control the execution process-(Java Learning Note III)

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.