chapter 3:decision structures. 3.1 the if statement 3.2 the if-else statement 3.3 the if-else-if...

22
Chapter 3:Decision Structures

Upload: lizbeth-parks

Post on 02-Jan-2016

245 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Chapter 3:Decision Structures.  3.1 The if Statement  3.2 The if-else Statement  3.3 The if-else-if Statement  3.4 Nested if Statements  3.5 Logical

Chapter 3:Decision Structures

Page 2: Chapter 3:Decision Structures.  3.1 The if Statement  3.2 The if-else Statement  3.3 The if-else-if Statement  3.4 Nested if Statements  3.5 Logical

Chapter 3:Decision Structures 3.1 The if Statement 3.2 The if-else Statement 3.3 The if-else-if Statement  3.4 Nested if Statements  3.5 Logical Operators 3.6 Comparing String Objects 3.7 More About Variable Declaration and Scope 3.9 The switch Statement 3.10 Creating Objects with the DecimalFormat Class 3.12 Common Errors to Avoid

Page 3: Chapter 3:Decision Structures.  3.1 The if Statement  3.2 The if-else Statement  3.3 The if-else-if Statement  3.4 Nested if Statements  3.5 Logical

Variable Scope In Java, a local variable does not have to be

declared at the beginning of the method. The scope of a local variable begins at the

point it is declared and terminates at the end of the method.

When a program enters a section of code where a variable has scope, that variable has come into scope, which means the variable is visible to the program.

Page 4: Chapter 3:Decision Structures.  3.1 The if Statement  3.2 The if-else Statement  3.3 The if-else-if Statement  3.4 Nested if Statements  3.5 Logical

VariableScope.java// Get the user's first name. String firstName; firstName = JOptionPane.showInputDialog( "Enter

your " + "first name."); String lastName; lastName = JOptionPane.showInputDialog( "Enter

your " + "last name.");

JOptionPane.showMessageDialog(null, "Hello " + firstName + " " + lastName);

Page 5: Chapter 3:Decision Structures.  3.1 The if Statement  3.2 The if-else Statement  3.3 The if-else-if Statement  3.4 Nested if Statements  3.5 Logical

The Conditional Operator The conditional operator is a ternary

(three operand) operator. The conditional operator allows a

programmer to write a simple if-else type statement.

The format of the operators is:expression1 ? expression2 : expression3

The conditional operator can also return a value.

Page 6: Chapter 3:Decision Structures.  3.1 The if Statement  3.2 The if-else Statement  3.3 The if-else-if Statement  3.4 Nested if Statements  3.5 Logical

The Conditional Operator

The conditional operator can be used as a shortened if-else statement:x > y ? z = 10 : z = 5;

This line is functionally equivalent to:if(x > )

z = 10;

else

z = 5;

Page 7: Chapter 3:Decision Structures.  3.1 The if Statement  3.2 The if-else Statement  3.3 The if-else-if Statement  3.4 Nested if Statements  3.5 Logical

The Conditional Operator

Many times, the conditional operator is used to supply a value.number = x > y ? 10 : 5;

This is functionally equivalent to:if(x > y)

number = 10;

else

number = 5;

Page 8: Chapter 3:Decision Structures.  3.1 The if Statement  3.2 The if-else Statement  3.3 The if-else-if Statement  3.4 Nested if Statements  3.5 Logical

ConsultantCharges.java

double hours, // To hold the hours worked charges; // To hold the chargesString input; // To hold user inputinput = JOptionPane.showInputDialog( "How

many hours were worked? ");hours = Double.parseDouble(input);hours = hours < 5 ? 5 : hours;charges = 50.0 * hours;JOptionPane.showMessageDialog(null,

"The charges are $" + charges);

Page 9: Chapter 3:Decision Structures.  3.1 The if Statement  3.2 The if-else Statement  3.3 The if-else-if Statement  3.4 Nested if Statements  3.5 Logical

The switch Statement The if-else statements allow the

programmer to make true / false branches. The switch statement allows the

programmer to use an ordinal value to determine how a program will branch.

The switch statement can evaluate an integer type or character type variable and make decisions based on the value.

Page 10: Chapter 3:Decision Structures.  3.1 The if Statement  3.2 The if-else Statement  3.3 The if-else-if Statement  3.4 Nested if Statements  3.5 Logical

The switch Statement The switch statement takes the

form:switch (SwitchExpression){ case CaseExpression: // place one or more statements here break; case CaseExpression: // place one or more statements here break;default: // place one or more statements here}

Page 11: Chapter 3:Decision Structures.  3.1 The if Statement  3.2 The if-else Statement  3.3 The if-else-if Statement  3.4 Nested if Statements  3.5 Logical

The switch Statement The switch statement takes an ordinal

value (byte, short, int, long, char) as the SwitchExpression.switch (SwitchExpression){

…}

The switch statement will evaluate the expression.

If there is an associated case statement that matches that value, program execution will be transferred to that case statement.

Page 12: Chapter 3:Decision Structures.  3.1 The if Statement  3.2 The if-else Statement  3.3 The if-else-if Statement  3.4 Nested if Statements  3.5 Logical

The switch Statement Each case statement will have a

corresponding CaseExpression that must be unique.case CaseExpression:

// place one or more statements here

break;

If the SwitchExpression matches the CaseExpression, the Java statements between the colon and the break statement will be executed.

Page 13: Chapter 3:Decision Structures.  3.1 The if Statement  3.2 The if-else Statement  3.3 The if-else-if Statement  3.4 Nested if Statements  3.5 Logical

The switch Case

The break statement ends the case statement.

The break statement is optional. If a case does not contain a break,

then program execution continues into the next case.

Page 14: Chapter 3:Decision Structures.  3.1 The if Statement  3.2 The if-else Statement  3.3 The if-else-if Statement  3.4 Nested if Statements  3.5 Logical

NoBreaks.javaswitch (number) { case 1: System.out.println("You entered 1."); case 2: System.out.println("You entered 2."); case 3: System.out.println("You entered 3."); default: System.out.println("That's not 1, 2, or 3!"); }

Page 15: Chapter 3:Decision Structures.  3.1 The if Statement  3.2 The if-else Statement  3.3 The if-else-if Statement  3.4 Nested if Statements  3.5 Logical

PetFood.javaswitch(foodGrade) { case 'a': case 'A': System.out.println("30 cents per lb."); break; case 'b': case 'B': System.out.println("20 cents per lb."); break; case 'c': case 'C': System.out.println("15 cents per lb."); break; default: System.out.println("Invalid choice."); }

Page 16: Chapter 3:Decision Structures.  3.1 The if Statement  3.2 The if-else Statement  3.3 The if-else-if Statement  3.4 Nested if Statements  3.5 Logical

The switch Case

The default case is optional and will be executed if no CaseExpression matches the SwitchExpression.

Page 17: Chapter 3:Decision Structures.  3.1 The if Statement  3.2 The if-else Statement  3.3 The if-else-if Statement  3.4 Nested if Statements  3.5 Logical

SwitchDemo.java// Determine the number entered. switch (number) { case 1: System.out.println("You entered 1."); break; case 2: System.out.println("You entered 2."); break; case 3: System.out.println("You entered 3."); break; default: System.out.println("That's not 1, 2, or 3!"); }

Page 18: Chapter 3:Decision Structures.  3.1 The if Statement  3.2 The if-else Statement  3.3 The if-else-if Statement  3.4 Nested if Statements  3.5 Logical

The DecimalFormat Class When printing out double and float values,

the full fractional value will be printed. The DecimalFormat class can be used to

format these values. In order to use the DecimalFormat class, the

Java import statement must be used. At the top of the program the statement

import java.text.DecimalFormat; must be used.

Page 19: Chapter 3:Decision Structures.  3.1 The if Statement  3.2 The if-else Statement  3.3 The if-else-if Statement  3.4 Nested if Statements  3.5 Logical

Format1.javadouble number1 = 0.166666666666667;double number2 = 1.666666666666667;double number3 = 16.666666666666667;double number4 = 166.666666666666667;DecimalFormat formatter = new

DecimalFormat("#0.00");// Display the formatted variable contents.System.out.println(formatter.format(number1));System.out.println(formatter.format(number2));System.out.println(formatter.format(number3));System.out.println(formatter.format(number4));

Page 20: Chapter 3:Decision Structures.  3.1 The if Statement  3.2 The if-else Statement  3.3 The if-else-if Statement  3.4 Nested if Statements  3.5 Logical

Format2.java double number1 = 0.166666666666667; double number2 = 1.666666666666667; double number3 = 16.666666666666667; double number4 = 166.666666666666667;// Create a DecimalFormat object. DecimalFormat formatter = new

DecimalFormat("000.00");// Display the formatted variable contents. System.out.println(formatter.format(number1)); System.out.println(formatter.format(number2)); System.out.println(formatter.format(number3)); System.out.println(formatter.format(number4));

Page 21: Chapter 3:Decision Structures.  3.1 The if Statement  3.2 The if-else Statement  3.3 The if-else-if Statement  3.4 Nested if Statements  3.5 Logical

Format3.javadouble number1 = 123.899;double number2 = 1233.899;double number3 = 12345.899;double number4 = 123456.899; double number5 = 1234567.899;DecimalFormat formatter = new

DecimalFormat("#,##0.00");System.out.println(formatter.format(number1));System.out.println(formatter.format(number2));System.out.println(formatter.format(number3));System.out.println(formatter.format(number4));System.out.println(formatter.format(number5));

Page 22: Chapter 3:Decision Structures.  3.1 The if Statement  3.2 The if-else Statement  3.3 The if-else-if Statement  3.4 Nested if Statements  3.5 Logical

Format4.java double number1 = 0.12; double number2 = 0.05; DecimalFormat formatter = new DecimalFormat("#0%");

// Display the formatted variable contents. System.out.println(formatter.format(number1)); System.out.println(formatter.format(number2));