Using Decision Structures CSCI N 201 Programming Concepts

  • Slides: 33
Download presentation
Using Decision Structures CSCI N 201: Programming Concepts Copyright © 2005 Department of Computer

Using Decision Structures CSCI N 201: Programming Concepts Copyright © 2005 Department of Computer & Information Science

Goals • Understand why we use decision-making structures in programming • Understand how to

Goals • Understand why we use decision-making structures in programming • Understand how to create If/Else structures • Understand how to test multiple conditions • Understand how to use Boolean operators • Understand how to create switch structures CSCI N 201: Programming Concepts Copyright © 2004 Department of Computer & Information Science

Computers as “Decision Makers” • • • Modern computers seem to “think”. Early computers,

Computers as “Decision Makers” • • • Modern computers seem to “think”. Early computers, however, did not have such an ability. Early programs used sequential logic. That is, Step A happened before Step B happened before Step C, etc. • Sequential programs are easy to write, but lack flexibility. • One of the ways we can simulate a computer’s ability to think is to program using decision structures. . . CSCI N 201: Programming Concepts Copyright © 2004 Department of Computer & Information Science

Computing Structures • In modern programming, all executable statements fall into one of three

Computing Structures • In modern programming, all executable statements fall into one of three categories: – Sequential Structures – those structures where instructions happen in sequence. That is “A before B”, “B before C”, “C before D”, etc. – Looping (Iterative) Structures – those structures where instructions repeat until the program meets a given condition (test). – Decision (Selection) Structures – Today’s unit. CSCI N 201: Programming Concepts Copyright © 2004 Department of Computer & Information Science

Introducing Decision Structures • Decision structures consist “of a test condition together with one

Introducing Decision Structures • Decision structures consist “of a test condition together with one or more groups (or blocks) of statements. The result of the ‘test’ determines which of these blocks” the program will execute (Venit) • In all decision structures, ONE AND ONLY ONE block executes. • The program will then execute the next sequential section of code, after it evaluates a decision structure and executes the appropriate code block. CSCI N 201: Programming Concepts Copyright © 2004 Department of Computer & Information Science

Types of Decision Structures • Java. Script gives us three distinct types of Decision

Types of Decision Structures • Java. Script gives us three distinct types of Decision Structures: – Single-Alternative (a. k. a – If-Then) Structure • Contains a single block of executable code that will execute IF AND ONLY IF the associated condition (test) evaluates to TRUE (we don’t care if it evaluates to false). – Dual-Alternative (a. k. a – If-Then-Else) Structure • Contains exactly two blocks of code. One block will execute IF AND ONLY IF the associated condition (test) evaluates to TRUE; the other block will execute IF AND ONLY IF the associated condition (test) evaluates to FALSE. CSCI N 201: Programming Concepts Copyright © 2004 Department of Computer & Information Science

Types of Decision Structures • Java. Script gives us three distinct types of Decision

Types of Decision Structures • Java. Script gives us three distinct types of Decision Structures: – Multiple-Alternative (Select Case) Structure • Contains multiple blocks (called cases), each of which will execute IF AND ONLY IF the program matches ONLY one of multiple condition (test) results. ONLY ONE BLOCK (case) will execute. CSCI N 201: Programming Concepts Copyright © 2004 Department of Computer & Information Science

Humans as “Decision Makers” • Human beings are very good at understanding concepts. •

Humans as “Decision Makers” • Human beings are very good at understanding concepts. • We understand multiple types of syntax, too. • Additionally, we understand meaning. • This allows for some “gray area” in our language. CSCI N 201: Programming Concepts Copyright © 2004 Department of Computer & Information Science

“Concepts” to “Conditions” Consider the following statements: • “It’s cool outside. ” • “The

“Concepts” to “Conditions” Consider the following statements: • “It’s cool outside. ” • “The weather is cool outside. ” • “It’s somewhat chilly out there!” • “The temperature is expected to be around 65°” CSCI N 201: Programming Concepts Copyright © 2004 Department of Computer & Information Science

“Concepts” to “Conditions” What does “cool” mean? Its meaning is arbitrary … • Humans

“Concepts” to “Conditions” What does “cool” mean? Its meaning is arbitrary … • Humans can understand multiple types of syntax • • • and are able to extrapolate meaning from different syntax. However, computers don’t “think” on that level. As programmers, we need to refine human language to a more restrictive language that computers understand. One way to do this is to translate concepts to conditions which can be tested … CSCI N 201: Programming Concepts Copyright © 2004 Department of Computer & Information Science

Using the Relational Operator • The relational (comparison) operator compares two values of THE

Using the Relational Operator • The relational (comparison) operator compares two values of THE SAME TYPE (i. e. – Strings are compared to strings, integers are compared to integers, etc. ) • There are several different types of comparison operators (see next slide) • Comparison operators HAVE NO PRECEDENCE among themselves and are evaluated LEFT to RIGHT (however, parentheses can be added to alter precedence) CSCI N 201: Programming Concepts Copyright © 2004 Department of Computer & Information Science

Available Relational Operators Comparison Is equal to Is not equal to Is less than

Available Relational Operators Comparison Is equal to Is not equal to Is less than or equal to Is greater than or equal to Java. Script Relational Operator == != < <= > >= CSCI N 201: Programming Concepts Copyright © 2004 Department of Computer & Information Science

Translating the Concept • Remember, that computers don’t understand arbitrary concepts • Instead, we

Translating the Concept • Remember, that computers don’t understand arbitrary concepts • Instead, we need to define the meaning of the concept for the computer • Once we define what “cool” means, we can write our conditional statement CSCI N 201: Programming Concepts Copyright © 2004 Department of Computer & Information Science

The “If” Conditional • A single-alternative (If-Then) structure, allows us to specify an action

The “If” Conditional • A single-alternative (If-Then) structure, allows us to specify an action IF AND ONLY IF a condition tests to TRUE. • The parts of the If-Then structure: if (condition is true) { perform this action } CSCI N 201: Programming Concepts Copyright © 2004 Department of Computer & Information Science

What if the “if” condition is not met? • It is a good idea

What if the “if” condition is not met? • It is a good idea to provide an alternative plan if a condition is not met (it evaluates to FALSE). • To do this, we use an “If-Then-Else” structure: if (condition is true) { perform action 1 }else{ perform action 2 } CSCI N 201: Programming Concepts Copyright © 2004 Department of Computer & Information Science

Making Case-Insensitive Comparisons • We can use a special method of the String object

Making Case-Insensitive Comparisons • We can use a special method of the String object called the String. to. Upper. Case() method to make case-insensitive comparisons. • Using String. to. Upper. Case() will see the values “YES”, “Yes”, “y. Es”, etc. as the same thing. CSCI N 201: Programming Concepts Copyright © 2004 Department of Computer & Information Science

Example of String. to. Upper. Case() if(str. Ans. to. Upper. Case() == “YES”) {

Example of String. to. Upper. Case() if(str. Ans. to. Upper. Case() == “YES”) { //Code for YES answer }else{ //Code for NO answer }//end if CSCI N 201: Programming Concepts Copyright © 2004 Department of Computer & Information Science

What about multiple conditions? • Sometimes, we need to consider the possibility that we

What about multiple conditions? • Sometimes, we need to consider the possibility that we need to evaluate more than value as a response to user input • We can do this using nested branching. CSCI N 201: Programming Concepts Copyright © 2004 Department of Computer & Information Science

Nested Branching • Consider the following statements: – “If the temperature outside is greater

Nested Branching • Consider the following statements: – “If the temperature outside is greater than 65°, I’ll wear a short-sleeved shirt. ” – “If the temperature outside is less than 65°, I’ll wear a sweater. ” – “If the temperature outside is less than 65° and is less than 45°, I’ll need to wear a coat. ” • These statements present us with one condition to test (the temperature), but more than two (three) possible actions! CSCI N 201: Programming Concepts Copyright © 2004 Department of Computer & Information Science

Nested Branching • We could write the code using a bunch of “if” statements,

Nested Branching • We could write the code using a bunch of “if” statements, each independent of the other: • However, a much more efficient (and cleaner, code-wise) way is to use a nested structure: • Still, the best solution is to use an if-else if structure: CSCI N 201: Programming Concepts Copyright © 2004 Department of Computer & Information Science

Multiple Conditions • Sometimes, it’s necessary to combine multiple conditions to form a single,

Multiple Conditions • Sometimes, it’s necessary to combine multiple conditions to form a single, more complex text. • It is a good idea to first establish a table (on paper) mapping the multiple conditions, the possible responses and the program’s reaction to responses. CSCI N 201: Programming Concepts Copyright © 2004 Department of Computer & Information Science

Multiple Conditions • After constructing our conditions table, we could use nested branching to

Multiple Conditions • After constructing our conditions table, we could use nested branching to solve the problem: • A better idea is to use Boolean operators to clarify our code: CSCI N 201: Programming Concepts Copyright © 2004 Department of Computer & Information Science

Multiple Conditions Cond. I II IV Question Yes No Weekend? First day? Weekend? X

Multiple Conditions Cond. I II IV Question Yes No Weekend? First day? Weekend? X X X - First day? Weekend? First day? X - X X Response 1 Response 2 Response 3 Response 4 CSCI N 201: Programming Concepts Copyright © 2004 Department of Computer & Information Science

Boolean Operators • Sometimes, conditions can be joined together in the same test using

Boolean Operators • Sometimes, conditions can be joined together in the same test using Boolean operators: – Logical AND is represented by && Example: if (A = B && C = D) … – Logical OR is represented by || Example: if(A = B || C = D) … – Logical NOT is represented by ! (bang symbol) Example: if( !(A = B) ) … • Boolean Precedence: NOT, followed by AND, followed by OR CSCI N 201: Programming Concepts Copyright © 2004 Department of Computer & Information Science

More on Boolean Operators • From Boolean expressions, we can develop Truth Tables: Cond

More on Boolean Operators • From Boolean expressions, we can develop Truth Tables: Cond A Cond. B A && B A || B !(A) T T F F T F T F F F T T CSCI N 201: Programming Concepts Copyright © 2004 Department of Computer & Information Science

Boolean Values • When testing against Boolean values, you can use shortcuts (True Example):

Boolean Values • When testing against Boolean values, you can use shortcuts (True Example): if(my. Var == true) … is the same thing as if(my. Var) … • False Example: if(my. Var == false) … is the same thing as if(!(my. Var)) … CSCI N 201: Programming Concepts Copyright © 2004 Department of Computer & Information Science

The Switch Structure • Similar to multiple if/else statements, in that ONLY ONE BLOCK

The Switch Structure • Similar to multiple if/else statements, in that ONLY ONE BLOCK OF CODE EXECUTES and executes ONLY ONCE. • Useful when the program asks the user to choose among more than two choices. • Structure is divided into two parts: – Switch statement – sets up the condition (test) – Multiple cases – values to text against the condition CSCI N 201: Programming Concepts Copyright © 2004 Department of Computer & Information Science

Parts of a Switch Structure • Switch keyword – Tells Java. Script to test

Parts of a Switch Structure • Switch keyword – Tells Java. Script to test against a variable value. • Cases – Tells the program what to do based on different types of test results. • Breaks (CRITICAL) - Tells the program to “break out” of the ENTIRE structure once a case has been evaluates to TRUE and executes. • Default – What to do when there are no more cases to evaluate (all previous cases evaluated to FALSE). CSCI N 201: Programming Concepts Copyright © 2004 Department of Computer & Information Science

window. confirm() Method • Boolean values are returned by the window. confirm() method. •

window. confirm() Method • Boolean values are returned by the window. confirm() method. • window. confirm() takes input from the user, based on which button they choose (OK=TRUE & CANCEL=FALSE) • It looks and feels a lot like window. alert(), except that it has both an OK and a CANCEL button CSCI N 201: Programming Concepts Copyright © 2004 Department of Computer & Information Science

Operator Precedence Order Description 1 Dot Operator 2 Parenthesis (Work from inside out) 3

Operator Precedence Order Description 1 Dot Operator 2 Parenthesis (Work from inside out) 3 Instance Operator 4 Multiplication, Division, Modulus 5 Addition, Subtraction, Concatenation Operator. () new * / % + - CSCI N 201: Programming Concepts Copyright © 2004 Department of Computer & Information Science

Operator Precedence (continued) Order Description Operator 6 Assignment = += -= *= /= %=

Operator Precedence (continued) Order Description Operator 6 Assignment = += -= *= /= %= 7 Comparison Operators <, >, <=, >= 8 Inequality 9 Boolean AND && Boolean OR || 10 != CSCI N 201: Programming Concepts Copyright © 2004 Department of Computer & Information Science

Questions? CSCI N 201: Programming Concepts Copyright © 2004 Department of Computer & Information

Questions? CSCI N 201: Programming Concepts Copyright © 2004 Department of Computer & Information Science

Resources • Java. Script: The Definitive Guide by David Flanagan (O’Reilly, 2002) • Java.

Resources • Java. Script: The Definitive Guide by David Flanagan (O’Reilly, 2002) • Java. Script Concepts & Techniques: Programming Interactive Web Sites by Tina Spain Mc. Duffie (Franklin, Beedle & Associates, 2003) • Extended Prelude to Programming: Concepts and Design by Stewart Venit (Scott/Jones, Inc. , 2002) CSCI N 201: Programming Concepts Copyright © 2004 Department of Computer & Information Science