Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions."— Presentation transcript:

1 Chapter 4 Control Structures I

2 Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions ► Learn how to use the selection control structures if, if…else, and switch in a program

3 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 repetition of statement(s)

4 Flowcharts ► Flowcharts use some basic symbols ► To contain calculations ► To make decisions ► To connect different parts of an algorithm

5 Selection ► One-Way Selection ► Two-Way Selection ► Compound (Block of) Statements ► Multiple Selections (Nested if) ► Conditional Operator ► switch Structures

6 Decisions and Sequence Structures ► When an action is to be taken only if a particular condition holds a decision statement is used. ► The condition which must hold may be logical or relational. The value of the condition must be boolean ► Each possible path through a condition statement will contain a sequence of steps to be executed ► The condition and the sequences of steps that are executed for each outcome of the condition form a selection structure. ► A selection structure is a type of control structure

7 One way selection ► Expression referred to as decision maker ► Statement referred to as action statement if ( condition ) { // Series of actions to be taken // when the condition is TRUE action 1; action 2; }

8 Flowchart for a one way selection ► A simple decision uses a decision box to hold the condition ► The sequence of statements is held in a sequence box of boxes condition Statement 1: Statement n: T F

9 Two way selection if (condition) if (condition) {// Series of actions to be taken when the condition is TRUE action 1; action n; } else else{ // Series of actions to be taken when the condition is FALSE action 1; action n; }

10 Flowchart for a two way selection ► A selection structure uses a decision box, and sequence boxes. There may be multiple sequence boxes along each path condition Statement 1: Statement n: T F Statement 1: Statement n:

11 Multiple Selections (Nested if) if (condition) if (condition) { // Series of actions to be taken when the condition is TRUE action 1; action 1; action n; action n;}else{ 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 n; } else else { // 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 n; } }

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

13 Flowchart for a decision condition2 Statement 1: Statement n: T F Statement 1: Statement n: condition F Statement 1: Statement n: T

14 The switch statement ► An alternative method to if statements with multiple else clauses ► The controlling expression (selector) must have an integral type ► Case labels are particular values of the controlling expression ► Break statements exit from the switch structure

15 switch Structures switch(controling expression) { case value1: statements1 break; case value2: statements2 break;... case valuen: statementsn break; default: statements }

16 switch Statement

17 Defining the condition ► Each decision statement is based upon a condition ► The condition is a statement with a logical value (boolean true or false), and is commonly referred to as a logical expression ► A logical expression may consist of boolean operands and a logical operation

18 Binary Logical Operators ► &AND ► |OR ► ^Exclusive OR ► &&Short Circuit AND ► ||Short Circuit OR ► !Not Unary Logical Operators

19 AND and OR Operators The &, && (And) operator The |, || (Or) operator

20 Logical (Boolean) Operators The ^ (XOR) operator The ^ (XOR) operator The ! (not) Operator ^ false true false true false

21 Short-Circuit Evaluation ► Definition: 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

22 Relational Operators ► Relational Operator  Allows you to make comparisons in a program  Binary operator ► Condition is represented by a logical expression ► A Logical expression has a value of either true or false

23 Relational Operators and Primitive Data Types ► Can be used with integral and floating-point data types ► Can be used with the char data type ► A relational expression consists of a relational operator and two integral, floating point, or character variables ► A relational expression is a type of logical expression with a logical (boolean) value of true or false

24 Binary Relational Operators in Java ► less than ► <less than ► <=less than or equal to ► > greater than ► >= greater than or equal to ► == equal to ► !=not equal to Binary Equality Operators in Java

25 Relational Operators and the Unicode Collating Sequence

26 Comparing Strings ► class String  Method compareTo  Method equals ► Given string str1 and str2 integer < 0 if str1 < str2 Str1.compareTo(str2) = { 0 if str1 = str2 integer > 0 if str1 > str2

27 Assignment operators ► A = B assign value of expression B to variable A, store result in A ► A &= B add the value of expression B to variable A, store result in A ► A |= B subtract the value of expression B from variable A, store result in A ► A ^= B multiply the value of expression B by variable A, store result in A


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

Similar presentations


Ads by Google