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

## 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:

Chapter 4 Control Structures I

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

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)

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

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

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

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; }

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

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; }

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:

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; } }

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; }

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

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

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

switch Statement

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

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

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

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

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

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

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

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

Relational Operators and the Unicode Collating Sequence

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

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