Selection Control Structure. Topics Review sequence control structure Structure theorem Selection control structure If statement Relational operators.

Slides:



Advertisements
Similar presentations
Control Structures Control structures are used to manage the order in which statements in computer programs will be executed Three different approaches.
Advertisements

Computer Science Department FTSM Control Structure: Selection (Part 1) Knowledge: Understand various concepts of selection control structure Skill: Be.
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 6 Horstmann Programs that make decisions: the IF command.
Program Design and Development
ECE122 L7: Conditional Statements February 20, 2007 ECE 122 Engineering Problem Solving with Java Lecture 7 Conditional Statements.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Conditional Statements Control Structures.
Flow control 1: if-statements (Liang 72-80) if(radius < 0) { System.out.println(“cannot get area: radius below zero”); } else { double area = radius *
Pseudocode.
Pseudocode.
DCT 1123 Problem Solving & Algorithms
True or False Unit 3 Lesson 7 Building Blocks of Decision Making With Additions & Modifications by Mr. Dave Clausen True or False.
Programming Logic and Design Sixth Edition
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Decision Structures and Boolean Logic.
Chapter 4 The If…Then Statement
Decision Structures and Boolean Logic
Python – Making Decisions Lecture 02. Control Structures A program that only has one flow is useful but limited. We can use if statements to make these.
Computer Science Selection Structures.
Chapter 3 Making Decisions
Lecture Set 5 Control Structures Part A - Decisions Structures.
CMSC 104, Version 8/061L11Relational&LogicalOps.ppt Relational and Logical Operators Topics Relational Operators and Expressions The if Statement The if-else.
Selection Control Structures Simple Program Design Third Edition A Step-by-Step Approach 4.
Selection Control Structures. Simple Program Design, Fourth Edition Chapter 4 2 Objectives In this chapter you will be able to: Elaborate on the uses.
Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.
1 Boolean Expressions to Make Comparisons Boolean expression –Represents only one of two states –Expression evaluates to either true or false Expressions.
CHAPTER 4: Selection Control Structure. Objectives  Use the relational comparison operators  Learn about AND logic  Learn about OR logic  Make selections.
Chapter 2 Pseudocode. Objectives To introduce common words, keywords and meaningful names when writing pseudocode To define the three basic control structures.
Pseudocode. Simple Program Design, Fourth Edition Chapter 2 2 Objectives In this chapter you will be able to: Introduce common words, keywords, and meaningful.
Pseudocode Simple Program Design Third Edition A Step-by-Step Approach 2.
Dale Roberts 1 Program Control - Algorithms Department of Computer and Information Science, School of Science, IUPUI CSCI N305.
Basic Control Structures
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 3 (2) & 4 September 8 & 10, 2009.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
Flow of Control Unless indicated otherwise, the order of statement execution through a method is linear: one after the other in the order they are written.
Program Development C# Programming January 30, 2007 Professor J. Sciame.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
Chapter 3 Decisions Three control structures Algorithms Pseudocode Flowcharts If…then …else Nested if statements Code blocks { } multi statement blocks.
An Object-Oriented Approach to Programming Logic and Design Chapter 5 Making Decisions.
1 Lecture 2 Control Structures: Part 1 Selection: else / if and switch.
Algorithms and Pseudocode
CMSC 104, Version 9/011 Relational and Logical Operators Topics Relational Operators and Expressions The if Statement The if-else Statement Nesting of.
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.
Pseudocode. Algorithm A procedure for solving a problem in terms of the actions to be executed and the order in which those actions are to be executed.
Programming Logic and Design Fifth Edition, Comprehensive Chapter 4 Making Decisions.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Operator Precedence Operators Precedence Parentheses () unary
The Selection Structure
Topics The if Statement The if-else Statement Comparing Strings
Programming Fundamentals
Relational & Logical Operators
Topics The if Statement The if-else Statement Comparing Strings
Relational and Logical Operators
Control Structure Senior Lecturer
Relational and Logical Operators
1) C program development 2) Selection structure
Chapter 5: Control Structure
Relational & Logical Operators, if and switch Statements
Relational and Logical Operators
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Chapter 3: Selection Structures: Making Decisions
Relational and Logical Operators
Relational and Logical Operators
Chapter 3: Selection Structures: Making Decisions
Relational and Logical Operators
Control Structures.
Structural Program Development: If, If-Else
Presentation transcript:

Selection Control Structure

Topics Review sequence control structure Structure theorem Selection control structure If statement Relational operators Types of selection Truth tables

Sequence Recall that the default control structure is sequence Our examples up to this point have all been of this type That is, the flow of control through the statements in our algorithms and programs is from top to bottom, sequentially

Structure Theorem There are two other control structures used in algorithms: Selection and repetition It has been shown that these three (sequence, selection, and repetition) are enough control structures to write any computer program—The Structure Theorem heorem

Selection We will deal with repetition later Selection involves making a decision to execute several statements or not Computers have the capability of making a decision by comparing one value with another

Selection To introduce selection control, we include a condition statement that compares two values such that the result is true or false That is, there will be a choice between two cases –If the condition is true one choice is selected –If it is false the other choice is selected The condition statement is written as an If statement and it allows the algorithm and program to make a decision

If Statement If condition is true Then Perform these statements Else Perform these statements EndIf Example: If book is hardback Then price = Else price = EndIf condition = “book is hardback” and this will be true or false

Relational Operators Also called comparison operators We use these operators in our conditions to compare two values: < less than<= less than or equal > greater than>= greater than or equal = equal not equal

Examples ConditionResult 10 < 5false 5 < 10true 1 > 3false 3 > 1true 15 >= 10true 15 >= 15true ConditionResult 110 <= 100false 12.5 <= 13.0true 5 = 5true 10.5 = 5.75false 90 80true 90 90false

Different Operators Note that we are using the same character, the equal sign =, as the assignment operator and the equal comparison operator They are different operators Examples (these are different operations) Assignment: tot = tot + 1 Comparison: If tot = 100 Then

Types of Selection There are three main varieties of the selection structure: –Simple selection – choice between two alternatives –Null else selection – perform statements only when condition is true –Combined selection – multiple conditions using And or Or The following slides illustrate these showing the flowchart, pseudocode, and Small Basic code

Simple Selection PseudocodeFlowchart If age < 65 Then charge = Else charge = 8.00 EndIf Small Basic code If age < 65 Then charge = Else charge = 8.00 EndIf

Null Else Selection PseudocodeFlowchart If balance <> 0.0 Then Add 1 to outstanding EndIf Small Basic code If balance <> 0.0 Then outstanding = outstanding + 1 EndIf

Null Else Selection As shown in the last example, when the Else clause is not needed, one simply doesn’t use the Else keyword This situation arises often

Combined Selection (And) Pseudocode If balance > 0.0 And monthsPastDue >= 3 Then Calculate balance = balance + fee EndIf Flowchart Small Basic code If balance > 0.0 And monthsPastDue >= 3 Then balance = balance + fee EndIf

Combined Selection (Or) PseudocodeFlowchart If age 65 Then Display “You are not eligible.” EndIf Small Basic code If age 65 Then TextWindow.WriteLine("You are not eligible.") EndIf

Combined Selection The examples above for combined selection don’t have an Else clause But if the situation warrants it the Else clause can be used with combined selection, for example: If age 65 Then Display “You are not eligible.” Else Display “You are eligible.” EndIf

Multiple Statements The above examples show just one statement in the Then and Else clauses of the If statement But if needed there may be multiple statements in those clauses, for example: If age 65 Then totNotEligible = totNotEligible + 1 Display “You are not eligible.” Else totEligible = totEligible + 1 Display “You are eligible.” EndIf

Structured Programming Notice how the indentation in the above examples helps us to see which statements are dependent on others This is one feature of what is called “structured programming,” that is, using good programming style There are multiple ways to write an algorithm and program, but some ways are better than others

Structured Programming One feature of good programming style, is to make our algorithms and programs typographically readable by indenting statements properly Look at the following two examples Which do you think uses good programming style and is more readable?

Structured Programming Example 1 If age 65 Then totNotEligible = totNotEligible + 1 Display “You are not eligible.” Else totEligible = totEligible + 1 Display “You are eligible.” EndIf Example 2 If age 65 Then totNotEligible = totNotEligible + 1 Display “You are not eligible.” Else totEligible = totEligible + 1 Display “You are eligible.” EndIf

Structured Programming It is not enough to write algorithms and programs that work, we need to follow good programming style so that they are easier to read and maintain That is, well written algorithms and programs allow the person who writes the statements to make fewer errors, and allows the person maintaining the code to do that more easily

Truth Tables for And & Or When combining conditions with And or Or, we are effectively combining true’s and false’s to arrive at a new true or false That is, And and Or are binary operations that take two true/false values and produce one true/false value: The way these operations do this is shown in the following truth tables

Truth Tables for And & Or For example, (1 = 2 And 3 = 3) is false because 1 = 2 is false and 3 = 3 is true, but the And truth table indicates that false And true is false Also, (4 7) is true because 4 7 is false, but the Or truth table indicates that true Or false is true

Summary Control structures refer to the order in which statements are executed in algorithms and programs There are three main control structures: Sequence, selection, and repetition In selection, there is a comparison of values that determines which statement(s) are executed next

Summary We use the If statement in pseudocode and Small Basic to implement the selection control structure The relational operators are used to make the comparison Types of selection include: –Simple selection –Null else selection –Combined selection Truth tables for And and Or help us to understand how the combined selection structure works

Terminology Selection control structure Structure theorem Condition If statement Relational operators Simple selection Null else selection Combined selection Structured programming Programming style Truth tables

End