Selection Control Structures - Introduction-Computer Science | CS 110, Study notes of Computer Science

Material Type: Notes; Professor: Adjeroh; Class: Introduction-Computer Science; Subject: Computer Science; University: West Virginia University; Term: Unknown 1989;

Typology: Study notes

Pre 2010

Uploaded on 09/17/2009

koofers-user-hcb
koofers-user-hcb 🇺🇸

8 documents

1 / 16

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Java Programming: From Problem Analysis to Program Design, Second Edition 1
Chapter 4: Selection Control
Structures
Relational and logical operators.
Logical (Boolean) expressions.
The selection control structures if,
ifelse, and switch.
Java Programming: From Problem Analysis to Program Design, Second Edition 2
Control Structures
Three methods of processing a program:
In sequence
Branching
Looping
Branch: Altering the flow of program
execution by making a selection or choice.
Loop: Altering the flow of program
execution by repeating statements.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Selection Control Structures - Introduction-Computer Science | CS 110 and more Study notes Computer Science in PDF only on Docsity!

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

Chapter 4: Selection Control

Structures

Œ Relational and logical operators.

Œ Logical (Boolean) expressions.

Œ The selection control structures if,

if…else, and switch.

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

Control Structures

Œ Three methods of processing a program:

Œ In sequence

Œ Branching

Œ Looping

Œ Branch: Altering the flow of program

execution by making a selection or choice.

Œ Loop: Altering the flow of program

execution by repeating statements.

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

Control Structures

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

Relational Operators

Œ Relational operator:

Œ Allows you to make comparisons in a program.

Œ Binary operator.

Œ Condition is represented by a logical

expression in Java.

Œ Logical expression: An expression that has

a value of either true or false.

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

Relational Operators and Primitive Data Types

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

Comparing Strings

Œ class String

Œ Method compareTo

Œ Method equals

Œ Given string str1 and str

< <

0 str1 str

0 str1 str

0 str1 str str1.compareTo(str2) aninteger if string

ifstring isequaltostring

aninteger ifstring

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

Comparing Strings String str1 = "Hello"; String str2 = "Hi"; String str3 = "Air"; String str4 = "Bill"; String str5 = "Bigger";

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

Comparing Strings

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

Comparing Strings

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

Short-Circuit Evaluation

Œ 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 15

Selection

Œ 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 16

One-Way Selection

Œ Syntax: if (expression) statement Œ Expression referred to as decision maker. Œ Statement referred to as action statement.

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

Two-Way Selection

Œ Syntax:

if (expression)

statement

else

statement

Œ else statement must be paired with an if.

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

Two-Way Selection

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

Two-Way Selection

Example 4-

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 22

Example 4-

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.

Two-Way Selection

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

Conditional (? :) Operator

Œ Ternary operator

Œ Syntax:

expression1? expression2 : expression

Œ If expression1 = true, then the result of the

condition is expression2.

Otherwise, the result of the condition is

expression3.

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

Multiple Selection: Nested if

Œ Syntax:

if (expression1) statement else if (expression2) statement else statement

Œ Else is associated with the most recent incomplete if. Œ Multiple if statements can be used in place of if…else statements. Œ May take longer to evaluate.

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

switch Structures

Œ Expression is also

known as selector.

Œ Expression can be an

identifier.

Œ Value can only be

integral.

switch (expression) { case value1: statements break; case value2: statements break; ... case valuen: statementsn break; default: statements }

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

switch Structures

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

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 32

Chapter Summary

Œ Control structures are used to process programs.

Œ Logical expressions and order of precedence of

operators are used in expressions.

Œ Compare strings.

Œ If statements.

Œ if…else statements.

Œ switch structures.

Œ Proper syntax for using control statements.