1 / 21

Control Structures Lecture 3

Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung U niversity email: cheng@cse.tt u .edu.tw http:/ / www.cse.ttu.edu.tw/~cheng. Control Structures Lecture 3. Decision Using if and if...else Nested if Statements

taite
Download Presentation

Control Structures Lecture 3

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Instructors: Fu-Chiung Cheng (鄭福炯) Associate Professor Computer Science & Engineering Tatung University email: cheng@cse.ttu.edu.tw http:// www.cse.ttu.edu.tw/~cheng Control StructuresLecture 3

  2. Decision • Using if and if...else • Nested if Statements • Shortcut if Statements • Using switch Statements • Repetition • Looping: for, while, and do • Nested loops • Using break and continue Contents: Control Structures

  3. Decisions if Statements switch Statements Shortcut if Statements

  4. if Statements • Format: if (booleanExpression) { statement(s); } • Example: if ((i >= 0) && (i <= 10)) System.out.println("i is between 0 and 10");

  5. The if...else Statement • Format: if (booleanExpression) { statement(s)-for-the-true-case; } else { statement(s)-for-the-false-case; }

  6. if...else Example if (radius >= 0) { area = radius*radius*PI; System.out.println("The area for the circle of radius " + radius + " is " + area); } else { System.out.println("Negative input"); }

  7. Nested if Statements Example 3.1: Using Nested if Statements This program reads in year and loan amount and computes the monthly pay and total pay. The interest rate is determined by year. TestIfElse

  8. Shortcut if Statements if (x > 0) y = 1 else y = -1; is equivalent to y = (x > 0) ? 1 : -1;

  9. switch Statements switch (year) { case 7: interestRate = 7.25; break; case 15: interestRate = 8.50; break; case 30: interestRate = 9.0; break; default: System.out.println( "Wrong Year, enter 7, 15, or 30"); }

  10. switch Statement Flow Chart

  11. Repetitions for Loops while Loops do Loops break and continue

  12. for Loops • Format: for (control-variable-initializer; continue-condition; adjustment-statement) { //loop body; } • Example: int i; for (i = 0; i<100; i++) { System.out.println("Welcome to Java!” + i); } // for (int i = 0; i<100; i++)

  13. for Loop Flow Chart

  14. for Loop Examples • Examples for using the for loop: • Example 3.2: TestSum.java TestSum • Example 3.3: TestMulTable.java TestMulTable

  15. while Loops • Format: while (continue-condition) { // loop-body; } • Example3.4: TestWhile.java TestWhile

  16. while Loop Flow Chart

  17. do Loops • Format: do { //loop body; } while (continue-condition)

  18. do Loop Flow Chart

  19. The break Keyword

  20. The continue Keyword

  21. Using break and continue Examples for using the break and continue keywords: • Example 3.5: TestBreak.java TestBreak • Example 3.6: TestContinue.java TestContinue

More Related