21
Control Structures Lecture 3 Instructors: Fu-Chiung Cheng ( 鄭鄭鄭 ) Associate Professor Computer Science & Enginee ring Tatung University email: [email protected] http:// www.cse.ttu.edu.tw/~cheng

Control Structures Lecture 3

  • Upload
    taite

  • View
    22

  • Download
    0

Embed Size (px)

DESCRIPTION

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

Citation preview

Page 1: Control Structures Lecture 3

Control StructuresLecture 3

Instructors: Fu-Chiung Cheng

(鄭福炯 )Associate Professor

Computer Science & EngineeringTatung University

email: [email protected]:// www.cse.ttu.edu.tw/~cheng

Page 2: Control Structures Lecture 3

Contents: Control Structures

• DecisionUsing if and if...elseNested if StatementsShortcut if StatementsUsing switch Statements

• RepetitionLooping: for, while, and doNested loopsUsing break and continue

Page 3: Control Structures Lecture 3

Decisions

if Statements

switch Statements

Shortcut if Statements

Page 4: Control Structures Lecture 3

if Statements

Format:Format:if (booleanExpression) if (booleanExpression) { {

statement(s);statement(s);}}

Example:Example:if ((i >= 0) && (i <= 10)) if ((i >= 0) && (i <= 10)) System.out.println("i is between 0 and 10");System.out.println("i is between 0 and 10");

Page 5: Control Structures Lecture 3

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

Page 6: Control Structures Lecture 3

if...else Example

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

Page 7: Control Structures Lecture 3

Nested if Statements

Example 3.1: Using Nested if Example 3.1: Using Nested if StatementsStatements

This program reads in year and loan This program reads in year and loan amount and computes the monthly amount and computes the monthly pay and total pay. The interest rate is pay and total pay. The interest rate is determined by year. determined by year.

TestIfElseTestIfElse Run

Page 8: Control Structures Lecture 3

Shortcut if Statements

if (x > 0) y = 1if (x > 0) y = 1else y = -1;else y = -1;

is equivalent tois equivalent to

y = (x > 0) ? 1 : -1;y = (x > 0) ? 1 : -1;

Page 9: Control Structures Lecture 3

switch Statements

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

Page 10: Control Structures Lecture 3

switch Statement Flow Chart

Page 11: Control Structures Lecture 3

Repetitions

for Loops

while Loops

do Loops

break and continue

Page 12: Control Structures Lecture 3

for Loops Format:Format:

for (control-variable-initializer;for (control-variable-initializer; continue-condition; adjustment-statement)continue-condition; adjustment-statement)

{{ //loop body;//loop body;

}}

Example:Example:int i;int i;for (i = 0; i<100; i++)for (i = 0; i<100; i++){{

System.out.println("Welcome to Java!” + i); System.out.println("Welcome to Java!” + i); }}

// for (int i = 0; i<100; i++) // for (int i = 0; i<100; i++)

Page 13: Control Structures Lecture 3

for Loop Flow Chart

Page 14: Control Structures Lecture 3

for Loop Examples

Examples for using the for loop:

Example 3.2: TestSum.java

TestSumTestSum

TestMulTableTestMulTable

Example 3.3: TestMulTable.java

Run

Run

Page 15: Control Structures Lecture 3

while Loops Format:Format:

while (continue-condition) while (continue-condition) {{ // loop-body;// loop-body;}}

ExampleExample 3.4: TestWhile.java3.4: TestWhile.java

TestWhileTestWhile Run

Page 16: Control Structures Lecture 3

while Loop Flow Chart

Page 17: Control Structures Lecture 3

do Loops Format:Format:

dodo{{ //loop body;//loop body;} while (continue-condition)} while (continue-condition)

Page 18: Control Structures Lecture 3

do Loop Flow Chart

Page 19: Control Structures Lecture 3

The break Keyword

Page 20: Control Structures Lecture 3

The continue Keyword

Page 21: Control Structures Lecture 3

Using break and continue

Examples for using the break and continue keywords:

Example 3.5: TestBreak.java

Example 3.6: TestContinue.java

TestBreakTestBreak

TestContinueTestContinue

Run

Run