The Selection Control Structure 1. 1.Module charts. 2. Flow-charts. 3. Selection. 4. Conditions.

Slides:



Advertisements
Similar presentations
Chapter 4 - Control Statements
Advertisements

Control Statements. Define the way of flow in which the program statements should take place. Control Statements Implement decisions and repetitions.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
Introduction to Computers and Programming Lecture 6 Professor: Evan Korth New York University.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 5: Looping by Tony.
1 9/26/08CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
Program Design and Development
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
1 9/28/07CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
Presented by Joaquin Vila Prepared by Sally Scott ACS 168 Problem Solving Using the Computer Week 12 Boolean Expressions, Switches, For-Loops Chapter 7.
The Selection Control Structure Questions on Program 3? 1.Review. 2. Truth-tables. 3. Forms of the if statement. 4. The switch statement.
CONTROL STATEMENTS IF-ELSE, SWITCH- CASE Introduction to Computer Science I - COMP 1005, 1405 Instructor : Behnam Hajian
Computer Science Selection Structures.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
COIT29222 Structured Programming Slide 1 COIT29222-Structured Programming Lecture Week 06  Reading: Study Guide Book 2, Modules 9 & 10 Textbook (4 th.
CPS120: Introduction to Computer Science Decision Making in Programs.
1 Conditional statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
2 Objectives You should be able to describe: Relational Expressions Relational Expressions The if-else Statement The if-else Statement Nested if Statements.
Programming Logic and Design Fifth Edition, Comprehensive
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Decision II. CSCE 1062 Outline  Boolean expressions  switch statement (section 4.8)
Flow of Control Part 1: Selection
Making Decisions. 4.1 Relational Operators Used to compare numbers to determine relative order Operators: > Greater than < Less than >= Greater than.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
Decisions in Python Bools and simple if statements.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
Making Decisions (True or False) Relational Operators >greater than =greater than or equal to
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Making Decisions.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
TK 1914 : C++ Programming Control Structures I (Selection)
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Chapter 4 Controlling Execution CSE Objectives Evaluate logical expressions –Boolean –Relational Change the flow of execution –Diagrams (e.g.,
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions.
Chapter Making Decisions 4. Relational Operators 4.1.
ICT Introduction to Programming Chapter 4 – Control Structures I.
Chapter 5: Control Structures I (Selection). Objectives In this chapter you will: Learn about control structures Examine relational and logical operators.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions 1.
CPS120: Introduction to Computer Science Decision Making in Programs.
Lecture 6 – Selection FTMK, UTeM – Sem /2014.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
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.
Chapter 4: Making Decisions.
Chapter 4: Making Decisions.
Conditions and Ifs BIS1523 – Lecture 8.
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
Chapter 3: Selection Structures: Making Decisions
Module 3 Selection Structures 6/25/2019 CSE 1321 Module 3.
Presentation transcript:

The Selection Control Structure 1. 1.Module charts. 2. Flow-charts. 3. Selection. 4. Conditions.

1.Module charts. Print amortization table InitializeProcessFinalize

Module charts (continued): Print amortization table Initialize Get Input Initialize Variables Process Calculate Table Line Print Table Line Finalize Calculate Last Line Print Last Line Until Payment > Beginning Balance

2.Flow-charts. §Problem: Using a loop, process a series of school soccer players. For each player, input the Age and Gender (the Gender could be represented by a character, either 'M' or 'F'). §The loop should terminate when an age of -1 (a sentinel value) is input. Inside the loop, for each player, add 1 to the appropriate total in the following list: §(1) MaleYouth (2) Female Youth §(3) Male Adult (4) Female Adult. §(Note: A Youth is anyone < 18.) §When the loop is exited, display the four totals.

3. Selection. §Recall that the default control-structure is called the sequential or straight-line control structure. §What code gets executed? §What is its limitation? What can it not handle? §Often, we need to give the user choices. §How do we do this?

Selection. §A.What? §Using selection, we can ask T/F questions about our data, and process it accordingly. For example, if Age is input, we can ask if Age >=18. If it is, we can do “adult” processing; otherwise we can do “youth” processing. Age >= 18? Y N

Selection (cont.). §B.Why? §In this way, we can process data differentially: §Different data values can lead to different processing. E.g. an Age of 21 leads to adult processing, while an age of 15 yields to youth processing.

Selection (cont.). §C.How? §In every procedural language, selection is implemented by some sort of conditional or “if” statement. §What is meant by saying something is “conditional”? How is this different from its opposite, “unconditional.” E.g. conditional vs. unconditional love (liberalitas vs. agape/caritas).

Selection (cont.). §How (cont.). §What is meant by a conditional statement? §If antecedent condition then dependent consequent, e.g. §If it rains, then the farmers will be happy. §“it rains” is the antecedent condition, “the farmers will be happy” is the dependent consequent.

Selection (cont.). §How (cont.). §In C#, we code: §if (condition) § statement1; §E.g. §if (Age >= 18) Console.Write (“Adult”);

Selection (cont.). §How (cont.). §What happens if an Age of 25 is entered? §What happens if an Age of 15 is entered? §What if we wanted to display “Youth” as well? §if (Age >= 18) Console.Write (“Adult”); // consequent else Console.Write (“Youth”); // alternate consequent

Selection (cont.). §How (cont.). §Note that the ( ) around the condition are mandatory and that there must be a semi- colon after the consequent *and* alternate consequent. §See more examples in Ifs1.cs and Ifs2.cs.

Selection (cont.). §How (cont.). §Notes on Ifs1.cs. §1. How characters are handled. l Note single quotes. l Note Convert.ToChar. 2. Assignment v. identity.

Selection (cont.). §How (cont.). §Notes on Ifs2.cs. §1. Use of character string. §2. Note double quotes. §3. Note the Equals method. §4.Efficiency: l Which is more efficient, a long if…else statement or many separate ifs? Why?

Selection (cont.). §How (cont.). §With separate ifs, what happens if the Grade is “A”? §4. Testing: Why is testing more complex once ifs are used? §How many times will we need to run the program?

4. Conditions. §An if statement is of the form: §if (condition) statement1; But: what exactly is a condition? It is an expression, but of a special type. NOT like is an arithmetic expression, so evaluates to a number

Conditions (cont.). §But a condition, such as (Age>= 18) evaluates to…? §True or False. §I.e. it is a logical (Boolean) expression. §Note the significance of the ALU: the arithmetic and logic unit.

Conditions (cont.). §1. Relational conditions: compare data values using the relational operators: §= = identity (‘equals’) §!= non-identity (‘does not equal’) §< §> § =

Conditions (cont.). §Relational conditions can be used to compare numbers (int, float) and single characters. §Limitation 1: Be careful with real numbers because of limited precision § do { § } while (Num ! = 0) // this condition may never // become false. Num may be

Conditions (cont.). §Limitation 2: §Strings are arrays of characters and so, although “==“ is overloaded to handle strings, a more sophisticated method is typically used to compare them, such as: §if Astring.Equals (AnotherString) Console.Write (“The strings are the same”);

Conditions (cont.). §How are comparison of characters made? How does it know ‘A’ > ‘B’? §ASCII codes. ‘0’ 48 …. ‘9’ 57 § ‘A’ 65 …. ‘Z’ 90 § ‘a’ 97 …. ‘z’ 122

Conditions (cont.). §2. Logical conditions. §A.Negation. (Not), uses ! §Reverses truth value !X is true iff X is false. §(!Age = 18) §Note: are not opposites. Fallacy of false alternatives. §(!Age = = 18) is equivalent to (Age != 18)

Conditions (cont.). §Logical conditions. §B.Conjunction. (And), uses &&. §E.g. if ((Age >= 18) && (Gender = = ‘M’)) Console.Write (“Adult male.”); §When is the Write statement executed? §When would it NOT be executed?

Conditions (cont.). §Logical conditions. §C.Disjunction. (Or), uses | |. §E.g. if ((Grade = = ‘A’) | | (Grade = = ‘B’)) Console.Write (“Well done.”); §When is the Write statement executed? §When would it NOT be executed?

Conditions (cont.). §In that case the 2 conditions are mutually exclusive. But could have a test like: §if ( (Doughnuts > 50) || (LemonBars > 30) ) Console.Write (“Sufficient for bake sale”); Yet, given the fact that these 2 conditions are not mutually exclusive---and Lutherans’ notorious zeal for bakery---both conditions could be true.

Conditions (cont.). §Logical conditions. §D.Complex conditions. § if ( ((Temp <= 75) && (Humidity <= 60)) | | ((Temp <= 85) && (Humidity <= 40))) Console.Write (“Nice weather.”);

Conditions (cont.). §3.Boolean constants and variables. §a.Built in constants are true and false. §b. Boolean variables are flags/switches, variables of type bool that can only have the values true or false. E.g. §bool OverDrawn;

Conditions (cont.). §Boolean constants and variables (cont.). §There are two ways to use the variable: §(1) if (Withdrawal > Balance) OverDrawn = true; else OverDrawn = false; (2) OverDrawn = (Withdrawal > Balance);

Conditions (cont.). §Boolean constants and variables (cont.). §Then one can use the Boolean variable, either: §if (OverDrawn = = true)…. §Or more simply: §if(OverDrawn) Console.Write(“Oh, no, not you again.”);

Conditions (cont.). §Boolean constants and variables (cont.). §To test the opposite value: §if(!OverDrawn) Console.Write (“Modern banking charges will soon rectify this”); §Uses of Boolean variables: §1. Switches/flags. §2. Self-documenting abbreviations of complex conditions.

Conditions (cont.). §Boolean constants and variables (cont.). §ValidGrade = ((Grade = = ‘J’) | | (Grade = = ‘M’) | | (Grade = = ‘S’ ) ); if (!ValidGrade) Console.Write (“Invalid staff grade”);