Presentation is loading. Please wait.

Presentation is loading. Please wait.

Branches and Program Design

Similar presentations


Presentation on theme: "Branches and Program Design"— Presentation transcript:

1 Branches and Program Design
Chapter 3 Branches and Program Design Chapter 3 - Branches and Program Design

2 Program Design Process (1)
1. Clearly state the problem that you are trying to solve. 2. Define the inputs required by the program and the outputs to be produced by the program. 3. Decompose the program into classes and their associated methods. 4. Design the algorithm that you intend to implement for each method. 5. Turn the algorithm(s) into Java statements. 6. Test the Java program. Chapter 3 - Branches and Program Design

3 Program Design Process (2)
Chapter 3 - Branches and Program Design

4 Chapter 3 - Branches and Program Design
Pseudocode Pseudocode is a hybrid mixture of Java and English. It is structured like Java, with a separate line for each distinct idea or segment of code, but the descriptions on each line are in English. Each line of the pseudocode should describe its idea in plain, easily understandable English. Example: Prompt user to enter temperature in degrees Fahrenheit Read temperature in degrees Fahrenheit (tempF) tempK in kelvins ¬ (5./9.) * (tempF - 32) Write temperature in kelvins Chapter 3 - Branches and Program Design

5 Chapter 3 - Branches and Program Design
Relational Operators Relational operators are operators with two numerical operands that yield a boolean (true/false) result. Example: 23.6 > 20.0 produces the result true Chapter 3 - Branches and Program Design

6 Chapter 3 - Branches and Program Design
Logical Operators Logical operators are operators with one or two boolean operands that yield a boolean (true/false) result. Example: If test1 is true and test2 is false, then the expression test1 || test2 is true. Chapter 3 - Branches and Program Design

7 Hierarchy of Operations
Operations within expressions are evaluated in the following order: Chapter 3 - Branches and Program Design

8 Chapter 3 - Branches and Program Design
The if Structure The if structure specifies that a statement will be executed if and only if a certain boolean expression is true. Multiple statements can be executed by including them in a compound statement (between { and } ). Examples: if ( grade > 70 ) System.out.println("Passed"); if ( discr > 0. ) { x1 = ( -b + Math.sqrt(discr) ) / (2.*a); x2 = ( -b - Math.sqrt(discr) ) / (2.*a); } Chapter 3 - Branches and Program Design

9 Chapter 3 - Branches and Program Design
The if/else Structure The if/else structure specifies that certain statements will be executed if a boolean expression is true, and different statements will be executed if the expression is false. Example: if ( discr < 0. ) { System.out.println(”There are complex roots.”); } else { System.out.println(”There are real roots.”); Chapter 3 - Branches and Program Design

10 The Conditional Operator
The conditional operator is essentially a compact if/else structure. The form of this operator is: boolean_expr ? expr1 : expr2 If the boolean expression is true, the result of this operator is expression 1. Otherwise, the result of this operator is expression 2. Example–the following statements print out “Passed”: grade = 82; System.out.println(grade > 70 ? ”Passed" : ”Failed”); Chapter 3 - Branches and Program Design

11 The switch Structure (1)
The switch structure permits a programmer to select a particular code block to execute based on the value of a single integer or character expression. Chapter 3 - Branches and Program Design

12 The switch Structure (2)
If the switch expression matches a particular case selector, the statements following that case selector will be executed. If the switch expression does not match any case selector, then the statements following the default case selector will be executed. Note that all statements from the chosen case selector until the end of the structure will be executed, unless a break statement is included to stop execution! The break state-ments are necessary for this structure to work properly. Chapter 3 - Branches and Program Design


Download ppt "Branches and Program Design"

Similar presentations


Ads by Google