Lecture 3 Objectives s Learn about control structures

Lecture 3 Objectives s Learn about control structures. s Examine relational and logical operators. s Explore how to form and evaluate logical (Boolean) expressions. s Learn how to use the selection control structures if, if…else, and switch in a program. Java Programming: From Problem Analysis to Program Design, Second Edition

Control Structures s Three methods of processing a program: s In sequence s Branching s Looping s Branch: Altering the flow of program execution by making a selection or choice. s Loop: Altering the flow of program execution by repeating statements. Java Programming: From Problem Analysis to Program Design, Second Edition

Control Structures Java Programming: From Problem Analysis to Program Design, Second Edition

Relational Operators s Relational operator: s Allows you to make comparisons in a program. s Binary operator. s Condition is represented by a logical expression in Java. s Logical expression: An expression that has a value of either true or false. Java Programming: From Problem Analysis to Program Design, Second Edition

Relational Operators Java Programming: From Problem Analysis to Program Design, Second Edition 5

Relational Operators and Primitive Data Types s Can be used with integral and floating-point data types. s Can be used with the char data type. s Unicode Collating Sequence. Java Programming: From Problem Analysis to Program Design, Second Edition

Relational Operators and Primitive Data Types Java Programming: From Problem Analysis to Program Design, Second Edition 7

Comparing Strings s class String s Method compare. To s Method equals s Given string str 1 and str 2 Java Programming: From Problem Analysis to Program Design, Second Edition

Comparing Strings String String str 1 str 2 str 3 str 4 str 5 = = = "Hello"; "Hi"; "Air"; "Bill"; "Bigger"; Java Programming: From Problem Analysis to Program Design, Second Edition 9

Comparing Strings Java Programming: From Problem Analysis to Program Design, Second Edition

Comparing Strings Java Programming: From Problem Analysis to Program Design, Second Edition 11

Comparing Strings Java Programming: From Problem Analysis to Program Design, Second Edition 12

Comparing Strings Java Programming: From Problem Analysis to Program Design, Second Edition

Short-Circuit Evaluation s A process in which the computer evaluates a logical expression from left to right and stops as soon as the value of the expression is known. Java Programming: From Problem Analysis to Program Design, Second Edition

Selection s s s One-way selection Two-way selection Compound (block of) statements Multiple selections (nested if) Conditional operator switch structures Java Programming: From Problem Analysis to Program Design, Second Edition

One-Way Selection s Syntax: if (expression) statement s Expression referred to as decision maker. s Statement referred to as action statement. Java Programming: From Problem Analysis to Program Design, Second Edition 16

One-Way Selection Example: //Determine the absolute value of an integer import javax. swing. JOption. Pane; public class Absolute. Value { public static void main(String[] args) { int number; int temp; String num. String; num. String = JOption. Pane. show. Input. Dialog ("Enter an integer: "); //Line 1 number = Integer. parse. Int(num. String); //Line 2 temp = number; //Line 3 Java Programming: From Problem Analysis to Program Design, Second Edition

One-Way Selection if (number < 0) number = -number; //Line 4 //Line 5 JOption. Pane. show. Message. Dialog(null, "The absolute value of " + temp + " is " + number, "Absolute Value", JOption. Pane. INFORMATION_MESSAGE); //Line 6 System. exit(0); } Java Programming: From Problem Analysis to Program Design, Second Edition 18

Two-Way Selection s Syntax: if (expression) statement 1 else statement 2 s else statement must be paired with an if. Java Programming: From Problem Analysis to Program Design, Second Edition

Two-Way Selection Java Programming: From Problem Analysis to Program Design, Second Edition

Two-Way Selection Example 4 -14 if (hours > 40. 0) wages = 40. 0 * rate + 1. 5 * rate * (hours - 40. 0); else wages = hours * rate; Java Programming: From Problem Analysis to Program Design, Second Edition

Two-Way Selection Example 4 -15 if (hours > 40. 0); //Line 1 wages = 40. 0 * rate + 1. 5 * rate * (hours - 40. 0); //Line 2 else //Line 3 wages = hours * rate; //Line 4 Because a semicolon follows the closing parenthesis of the if statement (Line 1), the else statement stands alone. The semicolon at the end of the if statement (see Line 1) ends the if statement, so the statement at Line 2 separates the else clause from the if statement. That is, else is by itself. Because there is no separate else statement in Java, this code generates a syntax error. Java Programming: From Problem Analysis to Program Design, Second Edition

Compound (Block of) Statements Syntax: { statement 1 statement 2. . . statementn } Java Programming: From Problem Analysis to Program Design, Second Edition

Compound (Block of) Statements if (age > 18) { System. out. println("Eligible to vote. "); System. out. println("No longer a minor. "); } else { System. out. println("Not eligible to vote. "); System. out. println("Still a minor. "); } Java Programming: From Problem Analysis to Program Design, Second Edition

Multiple Selection: Nested if s Else is associated with the s Syntax: most recent incomplete if. s Multiple if statements can if (expression 1) be used in place of if…else statements. statement 1 s May take longer to else if (expression 2) evaluate. statement 2 else statement 3 Java Programming: From Problem Analysis to Program Design, Second Edition 25

Conditional (? : ) Operator s Ternary operator s Syntax: expression 1 ? expression 3 expression 2 : s If expression 1 = true, then the result of the condition is expression 2. Otherwise, the result of the condition is expression 3. Java Programming: From Problem Analysis to Program Design, Second Edition

switch Structures switch (expression) { case value 1: statements 1 break; case value 2: statements 2 break; . . . case valuen: statementsn break; default: statements } s Expression is also known as selector. s Expression can be an identifier. s Value can only be integral. Java Programming: From Problem Analysis to Program Design, Second Edition 27

switch Structures Java Programming: From Problem Analysis to Program Design, Second Edition

switch Structures Example 4 -24 switch (grade) { case 'A': System. out. println("The break; case 'B': System. out. println("The break; case 'C': System. out. println("The break; case 'D': System. out. println("The break; case 'F': System. out. println("The break; default: System. out. println("The invalid. "); } grade is A. "); grade is B. "); grade is C. "); grade is D. "); grade is F. "); grade is Java Programming: From Problem Analysis to Program Design, Second Edition

Programming Example: Cable Company Billing s Input: Customer’s account number, customer code, number of premium channels to which customer subscribes, number of basic service connections (in the case of business customers). s Output: Customer’s account number and the billing amount. Java Programming: From Problem Analysis to Program Design, Second Edition

Programming Example: Cable Company Billing Solution: 1. Prompt user for information. 2. Use switch statements based on customer’s type. 3. Use an if statement nested within a switch statement to determine the amount due by each customer. Java Programming: From Problem Analysis to Program Design, Second Edition

Lecture 3 Summary s Control structures are used to process programs. s Logical expressions and order of precedence of operators are used in expressions. s Compare strings. s If statements. s if…else statements. s switch structures. s Proper syntax for using control statements. Java Programming: From Problem Analysis to Program Design, Second Edition
- Slides: 32