CSCI N201: Programming Concepts Copyright ©2005  Department of Computer & Information Science Working with Selection Structures.

Slides:



Advertisements
Similar presentations
Control Statements. Define the way of flow in which the program statements should take place. Control Statements Implement decisions and repetitions.
Advertisements

Fundamental of C programming
Decisions If statements in C.
Chapter 13 Control Structures. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Control Structures Conditional.
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.
Describing Process Specifications and Structured Decisions Systems Analysis and Design, 7e Kendall & Kendall 9 © 2008 Pearson Prentice Hall.
 Control structures  Algorithm & flowchart  If statements  While statements.
ITEC113 Algorithms and Programming Techniques
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
Chapter 9 Describing Process Specifications and Structured Decisions
Program Design and Development
CSCI N201: Programming Concepts Copyright ©2005  Department of Computer & Information Science Working with Loops.
CSCI N201: Programming Concepts Copyright ©2005  Department of Computer & Information Science Using Decision Structures.
Using Control Structures. Goals Understand the three basic forms of control structures Understand how to create and manage conditionals Understand how.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Decision Structures and Boolean Logic.
Decision Structures and Boolean Logic
Branching and Conditions CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Computer Science Selection Structures.
Chapter 3 Making Decisions
CPS120: Introduction to Computer Science Decision Making in Programs.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
Chapter 4 Selection Structures: Making Decisions.
1 Lecture 5: Selection Structures. Outline 2  Control Structures  Conditions  Relational Operators  Logical Operators  if statements  Two-Alternatives.
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.
6/3/2016 CSI Chapter 02 1 Introduction of Flow of Control There are times when you need to vary the way your program executes based on given input.
ITEC113 Algorithms and Programming Techniques
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
Lecture 3 – Selection. Outline Recall selection control structure Types of selection One-way selection Two-way selection Multi-selection Compound statement.
Conditional Structures UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
Dale Roberts 1 Program Control - Algorithms Department of Computer and Information Science, School of Science, IUPUI CSCI N305.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 3 (2) & 4 September 8 & 10, 2009.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
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.
Chapter 2 Inequalities. Lesson 2-1 Graphing and Writing Inequalities INEQUALITY – a statement that two quantities are not equal. SOLUTION OF AN INEQUALITY.
CSCI 161 Lecture 7 Martin van Bommel. Control Statements Statements that affect the sequence of execution of other statements Normal is sequential May.
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.
An Object-Oriented Approach to Programming Logic and Design Chapter 5 Making Decisions.
Using Control Structures. Goals Understand the three basic forms of control structures Understand how to create and manage conditionals Understand how.
Decision Statements, Short- Circuit Evaluation, Errors.
C++ Programming Lecture 5 Control Structure I (Selection) – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 5 Making Decisions.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Chapter 5 – Decision Making. Structured Programming Sequence Selection Repetition yesno yes no.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
Topics The if Statement The if-else Statement Comparing Strings
Programming Fundamentals
Topics The if Statement The if-else Statement Comparing Strings
Selection CIS 40 – Introduction to Programming in Python
Control Structure Senior Lecturer
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
Chapter 5: Control Structure
Chapter 11 Describing Process Specifications and Structured Decisions
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
Using Decision Structures
Chapter 3: Selection Structures: Making Decisions
Chapter 3: Selection Structures: Making Decisions
Chapter 3: Selection Structures: Making Decisions
Chapter 3: Selection Structures: Making Decisions
ICS103: Programming in C 4: Selection Structures
Decision Making Using the IF and EVALUATE Statements
Control Structures.
Presentation transcript:

CSCI N201: Programming Concepts Copyright ©2005  Department of Computer & Information Science Working with Selection Structures

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Goals By the end of this lecture, you should understand... When to use a single-alternative selection structure.When to use a single-alternative selection structure. When to use a dual-alternative selection structure.When to use a dual-alternative selection structure. How to use relational operators.How to use relational operators. How to use logical operators.How to use logical operators. When to use a Select-Case structure.When to use a Select-Case structure.

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science The 3 “Families” of Programming Structures Remember that we can choose from any of three families of programming structures to help us manipulate data to make meaningful information:Remember that we can choose from any of three families of programming structures to help us manipulate data to make meaningful information: –Sequential Structures –Selection Structures –Looping Structures

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Introducing Selection Structures Single-alternativeSingle-alternative –A single block of statements to be executed or skipped Dual-alternativeDual-alternative – Two blocks of statements, one of which is to be executed, while the other one is to be skipped Multiple-alternativeMultiple-alternative –More than two blocks of statements, only one of which is to be executed and the rest skipped.

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Single-alternative If something is true Then Do something ( any # of statements) End If If Age >= 18 Set Eligibility = ‘Yes’ End if

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Dual-alternative If something is true Then Do something Else Do something else End If If Age >= 18 set Eligibility = ‘Yes’ Else set Eligibility = ‘No’ End if

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Guidelines An else condition does not have to exist. Sometimes we only want to do something if something is trueAn else condition does not have to exist. Sometimes we only want to do something if something is true Do not manufacture alternative conditionsDo not manufacture alternative conditions In an If statement, the body of the if is executed if, and only if, the test condition is true.Otherwise, no action is takenIn an If statement, the body of the if is executed if, and only if, the test condition is true.Otherwise, no action is taken Be sure to indent for readabilityBe sure to indent for readability Do not use the word ‘Then’ after an ‘Else’Do not use the word ‘Then’ after an ‘Else’ Do use the word ‘Then’ after an ‘Else If’Do use the word ‘Then’ after an ‘Else If’

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Relational Operators Relational operators are the symbols used in the condition to be evaluated in If statements:Relational operators are the symbols used in the condition to be evaluated in If statements: = equal to <> not equal to < less than > greater than <= less than or equal to >= greater than or equal to

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Example The If statement:The If statement: ‘If A > B Then Write “A is greater than B” Write “A is greater than B” End If’ You can read the if statement above like this:You can read the if statement above like this: “ If it is true that A is greater than B, then write the words ‘A is greater than B’ to the screen.”

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Logical Operators Logical operators are used to connect simple conditions into a more complex condition called a ‘compound’ condition.Logical operators are used to connect simple conditions into a more complex condition called a ‘compound’ condition. The simple conditions each contain one relational operator.The simple conditions each contain one relational operator. Using compound conditions reduces the amount of code that must be written.Using compound conditions reduces the amount of code that must be written.

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Example Without Logical Operators Input X If X 10 Then Write “OK” End If

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Example With Logical Operators Input X If (X 10) Then Write “OK” End If

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Building Compound Conditions In a compound condition, it is necessary to use complete simple conditions. Test if each phrase in the compound condition can stand on its own.In a compound condition, it is necessary to use complete simple conditions. Test if each phrase in the compound condition can stand on its own. This is correct: If (X 10) Then …This is correct: If (X 10) Then … This is not correct: If (X 10) Then …This is not correct: If (X 10) Then …

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science The ‘And’ Operator A compound condition consisting of two simple conditions joined by an ‘And’ is true only if both simple conditions are true. It is false if even one of the conditions is false. If (X > 5) AND (X 5) AND (X<10) Then … Is true only if X is 6, 7, 8, or 9. It has to be both greater than 5 and less than 10 at the same time.Is true only if X is 6, 7, 8, or 9. It has to be both greater than 5 and less than 10 at the same time.

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science The ‘Or’ Operator A compound condition consisting of two simple conditions joined by an ‘Or’ is true if even one of the simple conditions is true. It is false only if both are false. If (Response = “Y”) OR (Response = “y”) Then …A compound condition consisting of two simple conditions joined by an ‘Or’ is true if even one of the simple conditions is true. It is false only if both are false. If (Response = “Y”) OR (Response = “y”) Then … Is true if Response is uppercase or lower case “y’. For the above condition to be false, Response would have to be something other than ‘y’.Is true if Response is uppercase or lower case “y’. For the above condition to be false, Response would have to be something other than ‘y’.

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science The ‘Not’ Operator ‘And’ and ‘Or’ affect 2 simple conditions.‘And’ and ‘Or’ affect 2 simple conditions. ‘Not’ affects only one condition.If you need to negate more than one simple condition, you will need more than one ‘Not’.‘Not’ affects only one condition.If you need to negate more than one simple condition, you will need more than one ‘Not’.

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science The ‘Not’ Operator A condition with the ‘Not’ operator is true only if the condition is false.A condition with the ‘Not’ operator is true only if the condition is false. –Not (A < B) is true only if B is greater than or equal to A.is true only if B is greater than or equal to A. –If (X > 100) And Not ( X = Y) Then… is true only if X is greater than 100 but not equal to the value of Y. is true only if X is greater than 100 but not equal to the value of Y.

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Using Nested Structures Sometimes, we must handle more than two options in a program. In many cases, we can use nested structures to accomplish this.Sometimes, we must handle more than two options in a program. In many cases, we can use nested structures to accomplish this. In a nested structure, we build a child structure inside the one branch (either the if branch or the else branch) or a parent structure. The child structure is completely enclosed inside a single branch.In a nested structure, we build a child structure inside the one branch (either the if branch or the else branch) or a parent structure. The child structure is completely enclosed inside a single branch.

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science If Statement 1 is TRUE … Perform Action A Else If Statement 2 is TRUE … Perform Action B Else Perform Action C End If End if Basic Nested Structure

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science If Age >= 18 Then Set Eligibility = ‘Yes’ Else If Age > 15 set Eligibility = ‘Maybe’ Else set Eligibility = “No” End If End if Pseudocode Example

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science More on Nesting … The number of ‘End If’s’ must equal the number of ‘If’s’.The number of ‘End If’s’ must equal the number of ‘If’s’. You can draw a line to connect them to check.You can draw a line to connect them to check. In the previous example, the check for age 5 will never be done if the age is > 18.In the previous example, the check for age 5 will never be done if the age is > 18. Regardless of how many possible conditions are included, only one will ever be executed.Regardless of how many possible conditions are included, only one will ever be executed.

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Case-type Statements ‘Case’ or ‘Switch’ statements can be used to more easily code multiple alternative ‘If’s’‘Case’ or ‘Switch’ statements can be used to more easily code multiple alternative ‘If’s’ Use the special or keyword ‘Case’.Use the special or keyword ‘Case’. Use ‘Select’ to start the Case statement.Use ‘Select’ to start the Case statement. Specify the expression to be evaluated.Specify the expression to be evaluated. List each possible value of the expression and what to do if that is that value is the true one.List each possible value of the expression and what to do if that is that value is the true one. Use ‘End Case’ to end the statementUse ‘End Case’ to end the statement The results will be the same as a correctly code multiple-alternative ‘If’.The results will be the same as a correctly code multiple-alternative ‘If’.

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Example Select Case of Choice Case 1: Set Ops = ‘Add’ Case 2: Case 2: Set Ops = ‘Subtract’ Case 3: Case 3: Set Ops = ‘Multiply’ Case 4: Case 4: Set Ops = ‘Divide’ End Case

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Defensive Programming Be sure to test your program by ‘playing computer’:Be sure to test your program by ‘playing computer’: Perform all calculations multiple times manually or with a calculator.Perform all calculations multiple times manually or with a calculator. Make sure each branch of each selection structure is executed at least once.Make sure each branch of each selection structure is executed at least once. Check for division by zero, negative values for a square root function, and any other special conditions.Check for division by zero, negative values for a square root function, and any other special conditions.

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

Resources Venit, Stewart. Extended Prelude to Programming: Concepts and Design. Scott/Jones, Inc., 2002.Venit, Stewart. Extended Prelude to Programming: Concepts and Design. Scott/Jones, Inc., 2002.