5. Branches and loops in Java - Framingham State University

4 downloads 29 Views 713KB Size Report
Branches and loops in Java. David Keil Computer Science I 10/13. David Keil. Computer Science I Using Java 5. Control structures. 10/13. 1. David M. Keil ...
David Keil

5. Branches and loops in Java

Computer Science I

David M. Keil, Framingham State University CSCI 152 Computer Science I Using Java

5. Branches and loops in Java F

David Keil

T

1. The if branch statement 2. Relational and Boolean expressions 3. Multiway branches: switch 4. Java loop statements 5. Writing correct loops Computer Science I Using Java

5. Control structures

10/13

1

10/13

2

Inquiry How can a Java program • make choices? • perform repeated tasks? • control what it does next?

David Keil

Computer Science I Using Java

5. Control structures

10/13

David Keil

5. Branches and loops in Java

Computer Science I

Topic objective 5. Use and debug a variety of Java branch and loop statements

David Keil

Computer Science I Using Java

5. Control structures

10/13

3

Subtopic outcomes 5.1a 5.1b 5.2a 5.2b 5.3 5.4a 5.4b 5.4c 5.4d 5.4e 5.5 David Keil

Describe the Java if statement* Use branch statements in Java* Describe logical operators and expressions* Use logical operators in a program Use the multi-way branch in Java Describe the Java loop statements* Translate a flowchart into Java* Solve a numeric loop problem in Java* Solve a loop problem with strings Read a file using a loop Debug a defective loop* Computer Science I Using Java

5. Control structures

10/13

4

10/13

David Keil

5. Branches and loops in Java

Computer Science I

1. The if branch statement • How can a Java program make choices? • How are file-reading errors handled? • Are variables accessible everywhere in a program? David Keil

Computer Science I Using Java

5. Control structures

10/13

5

The Java if statement age = in.nextInt(); True/false expression if (age < 0) out.print(“Invalid age”);

• An if statement consists of the keyword if, a Boolean expression (condition) in parentheses, and one statement that is executed if the condition is true David Keil

Computer Science I Using Java

5. Control structures

10/13

6

10/13

David Keil

5. Branches and loops in Java

Computer Science I

Check file stream before reading FileReader freadr = new FileReader(“x.txt”); Scanner fin = new Scanner(freadr); String line; if (fin.hasNextLine()) line = infile.getNextLine();

• Here, the FileReader object fin can detect the state of the stream • Possible errors: file not found; file empty David Keil

Computer Science I Using Java

5. Control structures

10/13

7

if with compound statement int age = in.nextInt(); if (age < 0) { out.print(“Invalid age”); age = in.nextInt(); }

Omitting braces here would change meaning! • When multiple statements execute conditional on an if, they must be part of a compound statement (block) David Keil

Computer Science I Using Java

5. Control structures

10/13

8

10/13

David Keil

5. Branches and loops in Java

Computer Science I

Compound statements and scope of access int curr_year = 1998, expir_date = 2000; if (curr_year = 5) out.print("Discount 10%“); else out.print("No discount“); • Exactly one of the output statements executes David Keil

Computer Science I Using Java

5. Control structures

10/13

10

10/13

David Keil

5. Branches and loops in Java

Computer Science I

The pairing rule with else: Each else is paired with the most recent unpaired if in the same scope. Example (misleading code): if (test1) if (test2) statement1(); else statement2();

(If test2 fails, statement2 executes.) Good solution: use compound statements David Keil

Computer Science I Using Java

5. Control structures

10/13

11

Java syntax for if branch-statement : if ( expr ) statement if ( expr ) statement else statement

• Statement may be assignment, compound statement, if statement, loop statement, method call, etc.

David Keil

Computer Science I Using Java

5. Control structures

10/13

12

10/13

David Keil

5. Branches and loops in Java

Computer Science I

Subtopic outcomes 5.1a Describe the Java if statement* 5.1b Use branch statements in Java*

David Keil

Computer Science I Using Java

5. Control structures

10/13

13

2. Relational and Boolean expressions • What does “>” mean? • What is a relation? • What is logic? • Is programming logic like hardware logic?

David Keil

Computer Science I Using Java

5. Control structures

10/13

14

10/13

David Keil

5. Branches and loops in Java

Computer Science I

Relational operators equal-to = = greater > less-than
120); if (! impossible) [logops.cpp] out.print("Thank you"); David Keil

Computer Science I Using Java

Truth tables The logical operators in Java have the same results as the basic logic gates used in computer hardware

David Keil

Computer Science I Using Java

5. Control structures

10/13

a 1 0

!a 0 1

a 0 0 1 1

b 0 1 0 1

a 0 0 1 1

b a && b 0 0 1 0 0 0 1 1

5. Control structures

19

a || b 0 1 1 1

10/13

20

10/13

David Keil

5. Branches and loops in Java

Computer Science I

Testing to exercise all paths • To test a program containing an if statement, developer must use test data that will cause a true value and a false value to appear as the result of the if condition • With nested ifs, fully exercising your code (getting test results that reflect execution of each statement in program) may require 4 tests, 8, etc. David Keil

Computer Science I Using Java

5. Control structures

10/13

21

Subtopic outcome 5.2a Describe logical operators and expressions* 5.2b Use logical operators in a program

David Keil

Computer Science I Using Java

5. Control structures

10/13

22

10/13

David Keil

5. Branches and loops in Java

Computer Science I

3. Multiway branches: switch • How do we test an expression for several possible values? • What does a menu do?

David Keil

Computer Science I Using Java

5. Control structures

10/13

23

Multi-way branches • Appropriate for mutually exclusive cases • Test for exact matches, not ranges of values • Selector should be an int or char • Case labels may be stacked • Don’t forget to use break! • default triggers processing when no case label matches the selector David Keil

Computer Science I Using Java

5. Control structures

10/13

24

10/13

David Keil

5. Branches and loops in Java

Computer Science I

Using switch with case labels Nested if:

if (age == 1) out.print(“Baby”); else if (age == 2) out.print(“Toddler”; else if (age == 3) out.print(“Preschool”); else out.print(“Other”);

switch:

Selector

switch (age) { Case labels case 1: out.print(“Baby”); break; case 2: out.print(“Toddler”); break; case 3: out.print(“Preschool”); break; default: out.print(“Other”); } David Keil

Computer Science I Using Java

5. Control structures

10/13

25

Stacking case labels out.print("Grade: “); char grade = in.getNextChar(); switch(toupper(grade)) { Stacked to accept case 'A': either ‘A’ or ‘B’ case 'B': out.print("Honors“); break; case 'C': case 'D': out.print("Passing“); break; case 'E': out.print("Failing“); break; default: out.print("Invalid input“); [honors.cpp] } David Keil

Computer Science I Using Java

5. Control structures

10/13

26

10/13

David Keil

5. Branches and loops in Java

Computer Science I

Menus • A menu is part of a user interface that presents options to the user • A switch statement after the user choose a menu option can specify a response to each case (possible choice) int option = in.nextInt(); switch(option) { case 1: ... break; case 2: ... break; ... default: out.print("Invalid option“); } David Keil

Computer Science I Using Java

5. Control structures

10/13

27

Syntax for switch

branch-statement : switch ( expr ) compound-statement

The subordinate statement in a switch is normally compound, with case labels, alternative statements, breaks David Keil

Computer Science I Using Java

5. Control structures

10/13

28

10/13

David Keil

5. Branches and loops in Java

Computer Science I

Subtopic outcome 5.3 Use the multi-way branch in Java

David Keil

Computer Science I Using Java

5. Control structures

10/13

29

4. Java loop statements • What is a loop? • How do loops terminate? • How are loops verified? • How may we manipulate strings? David Keil

Computer Science I Using Java

5. Control structures

10/13

30

10/13

David Keil

5. Branches and loops in Java

Computer Science I

Loop control structure • Loops are based mathematically on the notion of induction • Algorithm design is chiefly concerned with the design of loops that satisfy a specification • A central goal in this course is for all students to successfully design and implement loops to solve simple problems David Keil

Computer Science I Using Java

5. Control structures

1/09

31

What triggers loop exit? • A loop may terminate (a) after a count, or (b) after a certain sentinel value is input • Other kinds of exit condition are possible • Counted loop: Do 50 times: draw a hyphen

• Sentinel controlled loop: Repeat input a number until number is 0 David Keil

Computer Science I Using Java

Sentinel value 5. Control structures

10/13

32

10/13

David Keil

5. Branches and loops in Java

Computer Science I

Top-tested loops: while A loop to count from 1 to 10: int i = 1; while (i