27
Chapter 4 Chapter 4 Control Structures I Control Structures I

Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions

  • View
    233

  • Download
    5

Embed Size (px)

Citation preview

Page 1: Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions

Chapter 4Chapter 4

Control Structures IControl Structures I

Page 2: Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions

ObjectivesObjectives

►Examine relational and logical operatorsExamine relational and logical operators►Explore how to form and evaluate logical Explore how to form and evaluate logical

(Boolean) expressions(Boolean) expressions►Learn how to use the selection control structures Learn how to use the selection control structures

if, if…else, and switch in a programif, if…else, and switch in a program

Page 3: Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions

Control StructuresControl Structures

►Three methods of processing a programThree methods of processing a program In sequenceIn sequence BranchingBranching LoopingLooping

►Branch: Branch: Altering the flow of program execution Altering the flow of program execution by making a selection or choiceby making a selection or choice

►Loop: Altering the flow of program execution by Loop: Altering the flow of program execution by repetition of statement(s)repetition of statement(s)

Page 4: Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions

FlowchartsFlowcharts

► Flowcharts use some basic symbolsFlowcharts use some basic symbols

► To contain calculationsTo contain calculations

► To make decisionsTo make decisions

► To connect different parts of an To connect different parts of an algorithmalgorithm

Page 5: Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions

SelectionSelection

►One-Way SelectionOne-Way Selection►Two-Way SelectionTwo-Way Selection►Compound (Block of) StatementsCompound (Block of) Statements►Multiple Selections (Nested if)Multiple Selections (Nested if)►Conditional OperatorConditional Operator►switch Structuresswitch Structures

Page 6: Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions

Decisions and Sequence StructuresDecisions and Sequence Structures

► When an action is to be taken only if a particular When an action is to be taken only if a particular condition holds a decision statement is used.condition holds a decision statement is used.

► The The conditioncondition which must hold may be logical or which must hold may be logical or relational. The value of the condition must be booleanrelational. The value of the condition must be boolean

► Each possible path through a condition statement will Each possible path through a condition statement will contain a contain a sequencesequence of steps to be executed of steps to be executed

► The condition and the sequences of steps that are The condition and the sequences of steps that are executed for each outcome of the condition form a executed for each outcome of the condition form a selection structure. selection structure.

► A selection structure is a type of control structureA selection structure is a type of control structure

Page 7: Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions

One way selectionOne way selection► Expression referred to as decision makerExpression referred to as decision maker► Statement referred to as action statement Statement referred to as action statement

if ( if ( condition condition )){{

// Series of actions to be taken // Series of actions to be taken // when the condition is TRUE // when the condition is TRUE action 1;action 1;action 2;action 2;

}}

Page 8: Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions

Flowchart for a one way selectionFlowchart for a one way selection

►A simple decision uses a decision box to hold A simple decision uses a decision box to hold the conditionthe condition

►The sequence of statements is held in a sequence The sequence of statements is held in a sequence box of boxesbox of boxes

conditionStatement 1:

Statement n:

T

F

Page 9: Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions

Two way selectionTwo way selection

if (if (condition)condition){{ // Series of actions to be taken when the condition is TRUE // Series of actions to be taken when the condition is TRUE

action 1;action 1;

action n;action n;}}

elseelse{{

// Series of actions to be taken when the condition is FALSE // Series of actions to be taken when the condition is FALSE action 1;action 1;

action n;action n;}}

Page 10: Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions

Flowchart for a two way selectionFlowchart for a two way selection

►A selection structure uses a decision box, and A selection structure uses a decision box, and sequence boxes. There may be multiple sequence boxes. There may be multiple sequence boxes along each pathsequence boxes along each path

conditionStatement 1:

Statement n:

T

F

Statement 1:

Statement n:

Page 11: Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions

Multiple Selections (Nested if)Multiple Selections (Nested if)

if (if (condition)condition){ // Series of actions to be taken when the condition is TRUE { // Series of actions to be taken when the condition is TRUE action 1;action 1; action n;action n;}}elseelse{{ if (condition 2)if (condition 2) { // Series of actions to be taken when condition is FALSE and condition2 is TRUE { // Series of actions to be taken when condition is FALSE and condition2 is TRUE

action 1;action 1;action n;action n;

} }

elseelse { { // Series of actions to be taken when condition and condition 2 are FALSE // Series of actions to be taken when condition and condition 2 are FALSE

action 1;action 1;action n;action n;

}} }}

Page 12: Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions

Multiple Selections (else if)Multiple Selections (else if)

if (if (condition)condition){{ // Series of actions to be taken when the condition is TRUE // Series of actions to be taken when the condition is TRUE

action 1;action 1;action n;action n;

}}else if (condition 2)else if (condition 2){{

// Series of actions to be taken when condition is FALSE and condition2 is // Series of actions to be taken when condition is FALSE and condition2 is TRUE TRUE

action 1;action 1;action n;action n;

} }

elseelse{{

// Series of actions to be taken when condition and condition 2 are FALSE // Series of actions to be taken when condition and condition 2 are FALSE action 1;action 1;action n;action n;

}}

Page 13: Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions

Flowchart for a decisionFlowchart for a decision

condition2

Statement 1:

Statement n:T

F

Statement 1:

Statement n:

condition

F

Statement 1:

Statement n:

T

Page 14: Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions

The switch statementThe switch statement

►An alternative method to if statements with An alternative method to if statements with multiple else clausesmultiple else clauses

►The controlling expression (selector) must The controlling expression (selector) must have an integral typehave an integral type

►Case labels are particular values of the Case labels are particular values of the controlling expressioncontrolling expression

►Break statements exit from the switch structureBreak statements exit from the switch structure

Page 15: Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions

switch Structuresswitch Structures

switch(controling expression){case value1: statements1

break;case value2: statements2

break; ...case valuen: statementsn

break;default: statements}

Page 16: Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions

switch Statementswitch Statement

Page 17: Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions

Defining the conditionDefining the condition

►Each decision statement is based upon a Each decision statement is based upon a conditioncondition

►The condition is a statement with a logical value The condition is a statement with a logical value (boolean true or false), and is commonly (boolean true or false), and is commonly referred to as a logical expressionreferred to as a logical expression

►A logical expression may consist of boolean A logical expression may consist of boolean operands and a logical operationoperands and a logical operation

Page 18: Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions

Binary Logical OperatorsBinary Logical Operators

► && ANDAND► || OROR► ^̂ Exclusive ORExclusive OR► &&&& Short Circuit ANDShort Circuit AND► |||| Short Circuit ORShort Circuit OR

► !! NotNot

Unary Logical Operators Unary Logical Operators

Page 19: Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions

AND and OR OperatorsAND and OR Operators

The & , && (And) operatorThe & , && (And) operator

The | , || (Or) operatorThe | , || (Or) operator

Page 20: Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions

Logical (Boolean) OperatorsLogical (Boolean) Operators The ^ (XOR) operatorThe ^ (XOR) operator

The ! (not) Operator

^

false

true

true

false

false

true

true

false

Page 21: Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions

Short-Circuit EvaluationShort-Circuit Evaluation

►Definition: Definition: a process in which the computer a process in which the computer evaluates a logical expression from left to right evaluates a logical expression from left to right and stops as soon as the value of the expression and stops as soon as the value of the expression is knownis known

Page 22: Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions

Relational OperatorsRelational Operators

►Relational OperatorRelational Operator Allows you to make comparisons in a programAllows you to make comparisons in a program Binary operatorBinary operator

►Condition is represented by a logical expressionCondition is represented by a logical expression►A Logical expression has a value of either true A Logical expression has a value of either true

or falseor false

Page 23: Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions

Relational Operators and Primitive Relational Operators and Primitive Data TypesData Types

►Can be used with integral and floating-point data Can be used with integral and floating-point data typestypes

►Can be used with the char data typeCan be used with the char data type►A relational expression consists of a relational A relational expression consists of a relational

operator and two integral, floating point, or operator and two integral, floating point, or character variablescharacter variables

►A relational expression is a type of logical A relational expression is a type of logical expression with a logical (boolean) value of true expression with a logical (boolean) value of true or false or false

Page 24: Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions

Binary Relational Operators in JavaBinary Relational Operators in JavaBinary Relational Operators in JavaBinary Relational Operators in Java

► < less thanless than► <=<= less than or equal toless than or equal to► >> greater thangreater than► >=>= greater than or equal togreater than or equal to

► ==== equal toequal to► !=!= not equal tonot equal to

Binary Equality Operators in JavaBinary Equality Operators in Java

Page 25: Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions

Relational Operators and the Relational Operators and the Unicode Collating SequenceUnicode Collating Sequence

Page 26: Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions

Comparing StringsComparing Strings

►class String class String Method compareToMethod compareTo Method equalsMethod equals

►Given string str1 and str2Given string str1 and str2integer < 0 if str1 < str2integer < 0 if str1 < str2

Str1.compareTo(str2) = { 0 if str1 = str2Str1.compareTo(str2) = { 0 if str1 = str2

integer > 0 if str1 > str2 integer > 0 if str1 > str2

Page 27: Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions

Assignment operatorsAssignment operators

►A A == B assign value of expression B to B assign value of expression B to variable A, store result in A variable A, store result in A

► A A &=&= B B add the value of expression B to add the value of expression B to variable A, store result in A variable A, store result in A

►A A |=|= B B subtract the value of expression B subtract the value of expression B from variable A, store result in A from variable A, store result in A

►A A ^=^= B B multiply the value of expression B multiply the value of expression B by variable A, store result in A by variable A, store result in A