CSCI N201: Programming Concepts Copyright ©2005  Department of Computer & Information Science Using Decision Structures.

Slides:



Advertisements
Similar presentations
1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
Advertisements

CSCI N201: Programming Concepts Copyright ©2005  Department of Computer & Information Science Introducing JavaScript Loops.
Pascal Programming Today Chapter 4 1 »Conditional statements allow the execution of one of a number of possible operations. »Conditional statements include:
Introduction to C Programming
1 CS101 Introduction to Computing Lecture 23 Flow Control & Loops (Web Development Lecture 8)
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
Basic Elements of Programming A VB program is built from statements, statements from expressions, expressions from operators and operands, and operands.
If Statements Sections 1.25, Control Structures o All code thus far executes every line of code sequentially o We want to be able to repeat,
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
Programming Concepts MIT - AITI. Variables l A variable is a name associated with a piece of data l Variables allow you to store and manipulate data in.
Summer Computing Workshop. Session 4 Loops  At the heart of their functionality, loops enable blocks of code to be executed more than once  Loops represent.
Chapter 8 High-Level Programming Languages (modified by Erin Chambers)
Using Control Structures. Goals Understand the three basic forms of control structures Understand how to create and manage conditionals Understand how.
Computer Science Selection Structures.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 4 Decision.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
1 Boolean Expressions to Make Comparisons Boolean expression –Represents only one of two states –Expression evaluates to either true or false Expressions.
CPS120: Introduction to Computer Science Decision Making in Programs.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Making Decisions.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
Copyright 2003 Scott/Jones Publishing Making Decisions.
© ABB University - 1 Revision C E x t e n d e d A u t o m a t i o n S y s t e m x A Chapter 11 Structured Text Course T314.
Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 7 Conditionals and Loops 4/18/09 Python Mini-Course: Day 2 - Lesson 7.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions.
JavaScript, Fourth Edition
CSCI N201: Programming Concepts Copyright ©2005  Department of Computer & Information Science Working with Selection Structures.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Selection Statements. Introduction Today we learn more about learn to make decisions in Turing ▫Nested if statements, ▫case statements.
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 4 Making Decisions.
Using Loops. Goals Understand how to create while loops in JavaScript. Understand how to create do/while loops in JavaScript. Understand how to create.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
Java Programming Fifth Edition Chapter 5 Making Decisions.
Chapter 5: Making Decisions. Objectives Plan decision-making logic Make decisions with the if and if…else structures Use multiple statements in if and.
CPS120: Introduction to Computer Science Decision Making in Programs.
Using Control Structures. Goals Understand the three basic forms of control structures Understand how to create and manage conditionals Understand how.
Copyright ©2005  Department of Computer & Information Science If-Then-Else Structures.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
C++ Programming Lecture 5 Control Structure I (Selection) – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 5 Decision Making.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Silberschatz and Galvin  C Programming Language Decision making in C Kingdom of Saudi Arabia Ministry of Higher Education Al-Majma’ah University.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Java Programming Fifth Edition
Chapter 4: Making Decisions.
Chapter 5 Decisions. Chapter 5 Decisions ssential uestion: How are Boolean expressions or operators used in everyday life?
Chapter 6: Conditional Statements and Loops
Web Programming– UFCFB Lecture 16
Sequential Structures
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
Chapter 3: Introduction to Problem Solving and Control Statements
During the last lecture we had a discussion on Data Types, Variables & Operators
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Computer Science Core Concepts
ICT Programming Lesson 3:
Chapter 3: Selection Structures: Making Decisions
Using Decision Structures
Chapter 3: Selection Structures: Making Decisions
Presentation transcript:

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

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

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

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Computing Structures In modern programming, all executable statements fall into one of three categories: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 N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science 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)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.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.The program will then execute the next sequential section of code, after it evaluates a decision structure and executes the appropriate code block.

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Types of Decision Structures JavaScript gives us three distinct types of Decision Structures:JavaScript 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).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.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 N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Types of Decision Structures JavaScript gives us three distinct types of Decision Structures:JavaScript 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.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 N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Humans as “Decision Makers” Human beings are very good at understanding concepts.Human beings are very good at understanding concepts. We understand multiple types of syntax, too.We understand multiple types of syntax, too. Additionally, we understand meaning.Additionally, we understand meaning. This allows for some “gray area” in our language.This allows for some “gray area” in our language.

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

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science “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.Humans can understand multiple types of syntax and are able to extrapolate meaning from different syntax. However, computers don’t “think” on that level.However, computers don’t “think” on that level. As programmers, we need to refine human language to a more restrictive language that computers understand.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 …One way to do this is to translate concepts to conditions which can be tested …

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science 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 N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Available Relational Operators Comparison JavaScript Relational Operator Is equal to == Is not equal to != Is less than < Is less than or equal to <= Is greater than > Is greater than or equal to >=

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science 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 N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science 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 N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science 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 N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science The Conditional Operator Sometimes, it may be convenient for you to use a shortcut for dual alternative (If- Then-Else) structures.Sometimes, it may be convenient for you to use a shortcut for dual alternative (If- Then-Else) structures. The shortcut is called the conditional operator. Its general form looks like this:The shortcut is called the conditional operator. Its general form looks like this: Condition ? Action If True : Action If False

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science The Conditional Operator We can rewrite the following code using a conditional operator:We can rewrite the following code using a conditional operator: strGreeting = “Hello ”; if(strUserName != null) { strGreeting += strUserName; }else{ strGreeting += “there!”; } Example adapted from JavaScript: The Definitive Guide (Flanagan)

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science The Conditional Operator The previous code using a conditional operator:The previous code using a conditional operator: strGreeting = “Hello ” + (strUserName != null ? strUserName : “there.”); Example adapted from JavaScript: The Definitive Guide (Flanagan)

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science What about multiple conditions? Sometimes, we need to consider the possibility that we need to evaluate more than value as a response to user inputSometimes, 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.We can do this using nested branching.

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Nested Branching Consider the following statements: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!These statements present us with one condition to test (the temperature), but more than two (three) possible actions!

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Nested Branching We could write the code using a bunch of “if” statements, each independent of the otherWe 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

CSCI N201: 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 ORBoolean Precedence: NOT, followed by AND, followed by OR

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

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Multiple Conditions Cond.QuestionYesNoResponse I Weekend? X- Response 1 First day? X- Response 2 II Weekend? X- Response 3 First day? -X Response 4 III Weekend? -X Response 5 First day? X- Response 6 IV Weekend? -X Response 7 First day? -X Response 8

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

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

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Questions?

Resources JavaScript: The Definitive Guide by David Flanagan (O’Reilly, 2002)JavaScript: The Definitive Guide by David Flanagan (O’Reilly, 2002) JavaScript Concepts & Techniques: Programming Interactive Web Sites by Tina Spain McDuffie (Franklin, Beedle & Associates, 2003)JavaScript Concepts & Techniques: Programming Interactive Web Sites by Tina Spain McDuffie (Franklin, Beedle & Associates, 2003) Extended Prelude to Programming: Concepts and Design by Stewart Venit (Scott/Jones, Inc., 2002)Extended Prelude to Programming: Concepts and Design by Stewart Venit (Scott/Jones, Inc., 2002)