corejavae10v1p3.8 3rd Java BASIC Programming Structure-3.8 control flow

Source: Internet
Author: User

Process control can be achieved by using conditional statements, loop statements, and so on.

3.8.1 Block scope (block scope)

A block is a section surrounded by a pair of curly braces. He specifies the scope of a variable's existence, with a method's operating range. Repeating the definition of a variable in a nested block is not allowed in Java.

3.8.2 Conditional statements

if (condition) statement

12 ...}
 if  (yoursales >= target) {performance  = "satisfactory" ;bonus  = + 0.01 * (yoursales- target);}  else  {performance  = "unsatisfactory" ;bonus  = 0< Span style= "COLOR: #000000" >;}  
if (Yoursales >= 2 *= "excellent"=;} Else if (Yoursales >= 1.5 *= "Fine"=;} Else if (yoursales >== "satisfactory"=;} Else {System.out.println ("You ' re fired");}

3.8.3 Loop Statements

1.while Cycle

while (condition) statement

 while (Balance < goal) {    + = payment    ; double interest = balance * interestrate/100;     + = interest;    Years+ + + "years.");

2.do-while Cycle

Do statement while (condition); Be aware of;

 Do {    + = payment    ; double interest = balance * interestrate/100;     + = interest;    year ++    ; // Print Current balance     . . .     // ask if ready to retire and get input     . . .}  while (Input.equals ("N"));

3.8.4 fixed number of cycles (determinate loop)

1.for Cycle

 for (int i = ten; i > 0; i--)    System.out.println ("Counting down ... "+ i);

3.8.5 Multi-Select Switch statement

Scanner in =NewScanner (system.in); System.out.print ("Select an option (1, 2, 3, 4)");intChoice =in.nextint ();Switch(choice) { Case1:. . . Break; Case2:. . . Break; Case3:. . . Break; Case4:. . . Break;default://Bad input. . . Break;}

The case label (that is, the variable type followed by switch) can be:

? Char, BYTE, short, int and its wrapper class constant expression
? Enum type
? String literals are supported starting from Java SE 7.

String input = ...; Switch (Input.tolowercase ()) {case//  OK since Java SE 7...  Break ;. . .}

3.8.6 jump out of the loop

1. Although Java retains the Goto keyword, it is not recommended to use a goto statement. Generally use break to jump out of a loop. Java supports tagged break statements to jump out of multiple loops. It is equivalent to giving each loop a name and jumping out of which loop you can specify.

label:{ ... if  Break // exits block . . .} // Jumps here is the break statement executes

Examples are as follows:

 while (Years <=+ + + payment; double interest = balance * interestrate/100+ = interest; if  Break ; years+ +;}
Scanner in =NewScanner (system.in);intN;read_data: while(. . .)//This loop statement are tagged with the label{. . . for(. . .)//This inner loop was not labeled{System.out.print ("Enter A number >= 0:"); n=in.nextint ();if(N < 0)//should never Happen-can ' t go on BreakRead_data;//Break out of Read_data loop. . .}}

2.continue statements

Jump out of this cycle

corejavae10v1p3.8 3rd Java BASIC Programming Structure-3.8 control flow

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.