PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION.

Slides:



Advertisements
Similar presentations
Chapter 4 - Control Statements
Advertisements

 Control structures  Algorithm & flowchart  If statements  While statements.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Selection Statements Primitive Type boolean.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 4 Control Structures I: Selection.
Chapter 4 MATLAB Programming Logical Structures Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 1 st semester King Saud University College of Applied studies and Community Service Csc 1101.
Presented by Joaquin Vila Prepared by Sally Scott ACS 168 Problem Solving Using the Computer Week 12 Boolean Expressions, Switches, For-Loops Chapter 7.
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
Selection Statements in C++ If Statement in C++ Semantics: These statements have the same meaning as in the algorithmic language. 2- Two way selection:
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.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
Selection Control Structures Simple Program Design Third Edition A Step-by-Step Approach 4.
Switch Statement in C++. Syntax switch (selector) { case L1: statements1; break; case L2: statements2; break; … default: statements_n; } Semantics: This.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
Selection Control Structures. Simple Program Design, Fourth Edition Chapter 4 2 Objectives In this chapter you will be able to: Elaborate on the uses.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 4: Control Structures I (Selection)
Multi Way Selection You can choose statement(s) to run from many sets of choices. There are two cases for this: (a)Multi way selection by nested IF structure.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
 Learn about control structures  Examine relational and logical operators  Explore how to form and evaluate logical (Boolean) expressions  Learn how.
Lecture 3 – Selection. Outline Recall selection control structure Types of selection One-way selection Two-way selection Multi-selection Compound statement.
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 1 st semester King Saud University College of Applied studies and Community Service Csc 1101.
Conditional Structures UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
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)
111/18/2015CS150 Introduction to Computer Science 1 Announcements  I have not graded your exam yet.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
Chapter 5: Control Structures I (Selection). Objectives In this chapter you will: Learn about control structures Examine relational and logical operators.
Chapter#3 Part1 Structured Program Development in C++
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
Week 4 Program Control Structure
Lecture 7: Menus and getting input. switch Multiple-selection Statement switch Useful when a variable or expression is tested for all the values it can.
Programming Fundamentals by Dr. Nadia Y. Yousif1 Control Structures (Selections) Topics to cover here: Selection statements in the algorithmic language:
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 2 nd semester King Saud University College of Applied studies and Community Service Csc 1101.
Chapter 5 – Decision Making. Structured Programming Sequence Selection Repetition yesno yes no.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
 Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
Chapter 4: Control Structures I (Selection)
Decisions Chapter 4.
Chapter 4: Making Decisions.
Chapter 4: Making Decisions.
Compound Assignment Operators in C++
Control Structures: Selection Statement
Chapter#3 Structured Program Development in C++
Chapter 5: Control Structure
Chapter 4: Control Structures I (Selection)
SELECTIONS STATEMENTS
Boolean Expressions to Make Comparisons
Week 3 – Program Control Structure
Structured Program Development in C++
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Control Structures: Selection Statement
Selection Control Structure
Presentation transcript:

PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION

CONTENTS One way selection Two way selection Multiple way selection One way selection Two way selection Multiple way selection

What is Selection? Structure in pseudocode to illustrate a choice between two or more actions, depending on whether a condition is true or false

What is Selection? The condition in the IF statement is based on a comparison of two items, and is usually expressed with one of the following relational operators: < less than > greater than <= less than or equal to >= greater than or equal to not equal to The condition in the IF statement is based on a comparison of two items, and is usually expressed with one of the following relational operators: < less than > greater than <= less than or equal to >= greater than or equal to not equal to

One Way Selection Syntax of one-way (if) selection: if (expression) statement1; statement2; if is reserved word expression must evaluate to true (nonzero) or false (0) statement1 executed if value of expression true statement1 bypassed if value false; program execution moves to statement2 statement2 is executed regardless Syntax of one-way (if) selection: if (expression) statement1; statement2; if is reserved word expression must evaluate to true (nonzero) or false (0) statement1 executed if value of expression true statement1 bypassed if value false; program execution moves to statement2 statement2 is executed regardless

One Way Selection Algorithms Example: IF grade >= 90 THEN Student grade = A Algorithms Example: IF grade >= 90 THEN Student grade = A Grade >= 90 ? False True

Two Way Selection Syntax of two-way (if...else) selection: if (expression) statement1; else statement2; If expression true, statement1 executed, otherwise statement2 executed else is reserved word Syntax of two-way (if...else) selection: if (expression) statement1; else statement2; If expression true, statement1 executed, otherwise statement2 executed else is reserved word

Two Way Selection Algorithms Example: IF account balance < 300 THEN service charge = RM1 ELSE service charge = RM2 END IF Algorithms Example: IF account balance < 300 THEN service charge = RM1 ELSE service charge = RM2 END IF Account < 300? Service charge = RM 1 Service charge = RM 2 False True

Combined Selection Each connected with the logical operators AND or OR If the connector AND is used to combine the conditions, then both condition must be true for the combined condition to be true If the connector OR is used to combine any two conditions, then only one of the conditions need to be true for the combined condition to be considered true Each connected with the logical operators AND or OR If the connector AND is used to combine the conditions, then both condition must be true for the combined condition to be true If the connector OR is used to combine any two conditions, then only one of the conditions need to be true for the combined condition to be considered true

Combined Selection (AND) Syntax of combined selection (AND) If (expression1) AND (expression2) statement; If both expression1 and expression2 are true, statement will be executed Syntax of combined selection (AND) If (expression1) AND (expression2) statement; If both expression1 and expression2 are true, statement will be executed

Combined Selection (AND) Syntax of combined selection (AND) If (expression1) AND (expression2) statement; If both expression1 and expression2 are true, statement will be executed Syntax of combined selection (AND) If (expression1) AND (expression2) statement; If both expression1 and expression2 are true, statement will be executed

Combined Selection (AND) Algorithms Example: IF exam marks >= 20 AND project marks >=30 THEN Grade = pass Algorithms Example: IF exam marks >= 20 AND project marks >=30 THEN Grade = pass

Multiple Way Selection You can choose statement(s) to run from many sets of choices. There are two cases for this: Multi way selection by nested IF structure Multi way selection by SWITCH structure You can choose statement(s) to run from many sets of choices. There are two cases for this: Multi way selection by nested IF structure Multi way selection by SWITCH structure

Multiple Way Selection (Nested IF) The structure that contains another structure of the same type is called a nested structure. In the Nested IF structure, the statements that exists between IF and ELSE or between IF and END IF can contain IF statement. The structure that contains another structure of the same type is called a nested structure. In the Nested IF structure, the statements that exists between IF and ELSE or between IF and END IF can contain IF statement.

Multiple Way Selection (Nested IF) Syntax of one possible structure: IF (condition1) THEN Statements1 ELSE IF (condition2) THEN Statements2 ELSE IF (Condition3) THEN Statements3 ELSE IF (Condition4) THEN Statements4 END IF Note: The nest can be to many levels. The following figure shows the execution of this structure. Syntax of one possible structure: IF (condition1) THEN Statements1 ELSE IF (condition2) THEN Statements2 ELSE IF (Condition3) THEN Statements3 ELSE IF (Condition4) THEN Statements4 END IF Note: The nest can be to many levels. The following figure shows the execution of this structure.

Multiple Way Selection (Nested IF) Condition1 Condition2 Statements2 Statements3 Rest of algorithm Statements1 Condition3 Statements4 True False

Multiple Way Selection (Nested IF) Example: Write an algorithm that inputs a student mark and outputs the corresponding grade, where grades are as follows: mark grade A 80-89B 70-79C 60-69D < 60E Example: Write an algorithm that inputs a student mark and outputs the corresponding grade, where grades are as follows: mark grade A 80-89B 70-79C 60-69D < 60E

Multiple Way Selection (Nested IF) Algorithm Design ALGORITHM Grades INPUT mark IF ( mark 100 ) THEN OUTPUT “ Mark out of range” ELSE IF ( mark  90 AND mark  100 ) THEN OUTPUT “A” ELSE IF ( mark  80 ) THEN OUTPUT “B” ELSE IF ( mark  70 ) THEN OUTPUT “C” ELSE IF ( mark  60 ) THEN OUTPUT “D” ELSE OUTPUT “E” END IF END Grades Algorithm Design ALGORITHM Grades INPUT mark IF ( mark 100 ) THEN OUTPUT “ Mark out of range” ELSE IF ( mark  90 AND mark  100 ) THEN OUTPUT “A” ELSE IF ( mark  80 ) THEN OUTPUT “B” ELSE IF ( mark  70 ) THEN OUTPUT “C” ELSE IF ( mark  60 ) THEN OUTPUT “D” ELSE OUTPUT “E” END IF END Grades

Multiple Way Selection (Nested IF) In the previous example, only one statement should be executed for any value of x. In the nested case, only one statement is executed exactly. Therefore, it is better than the sequence case. In the previous example, only one statement should be executed for any value of x. In the nested case, only one statement is executed exactly. Therefore, it is better than the sequence case.

Multiple Way Selection (SWITCH) Note that the nest can be done to many levels. Practically, it is not preferable to have more than 3 nested levels. The SWITCH control structure is the substitute for the nested IF structure of many levels of nesting. The SWITCH control structure is used to select one of several paths. It is especially useful when the selection is based on the value of a single variable or a simple expression (called the case selector). The case selector may be an integer, character, or Boolean variable or expression. Note that the nest can be done to many levels. Practically, it is not preferable to have more than 3 nested levels. The SWITCH control structure is the substitute for the nested IF structure of many levels of nesting. The SWITCH control structure is used to select one of several paths. It is especially useful when the selection is based on the value of a single variable or a simple expression (called the case selector). The case selector may be an integer, character, or Boolean variable or expression.

Multiple Way Selection (SWITCH) Syntax SWITCH (selector) CASE label1: Statements1 BREAK CASE label2: Statements2 BREAK. DEFAULT: Statements_n END SWITCH Syntax SWITCH (selector) CASE label1: Statements1 BREAK CASE label2: Statements2 BREAK. DEFAULT: Statements_n END SWITCH

Multiple Way Selection (SWITCH) The semantics (execution) of this statement: If the value of “selector” equals to one of the labels values, the statements of that case are executed and the execution continues to the statement after END SWITCH. If the value of the “selector” does not match with any of the labels values, the statements in the DEFAULT case are executed and the execution continues to the statement that follows END SWITCH. Note that if you remove BREAK from the cases, the statements of the matched case are executed and the execution proceed to the cases that follow. The following figure shows this execution: The semantics (execution) of this statement: If the value of “selector” equals to one of the labels values, the statements of that case are executed and the execution continues to the statement after END SWITCH. If the value of the “selector” does not match with any of the labels values, the statements in the DEFAULT case are executed and the execution continues to the statement that follows END SWITCH. Note that if you remove BREAK from the cases, the statements of the matched case are executed and the execution proceed to the cases that follow. The following figure shows this execution:

Multiple Way Selection (SWITCH) Statements1 BREAK Statements2 BREAK Statements_n BREAK … Rest of algorithm CASE.. DEFAULT SELECTOR CASE..

Multiple Way Selection (SWITCH) Example: Write an algorithm that inputs a character and displays a suitable musical note (i.e. do, re, mi, etc.) Analysis stage: Problem Input: - a character, musical_note Problem Output: - a message showing the corresponding musical note Example: Write an algorithm that inputs a character and displays a suitable musical note (i.e. do, re, mi, etc.) Analysis stage: Problem Input: - a character, musical_note Problem Output: - a message showing the corresponding musical note

Multiple Way Selection (SWITCH) Algorithm Design ALGORITHM Music INPUT musical_note SWITCH (musical_note) CASE ‘c’ : OUTPUT “ do “ BREAK CASE ‘d’ : OUTPUT “ re “ BREAK CASE ‘e’ : OUTPUT “ mi “ BREAK CASE ‘f’ : OUTPUT “ fa “ BREAK CASE ‘g’ : OUTPUT “ sol “ BREAK CASE ‘a’ : OUTPUT “ la “ BREAK CASE ‘b’ : OUTPUT “ ti “ BREAK DEFAULT: OUTPUT “ Invalid note was read “ END SWITCH END Music Algorithm Design ALGORITHM Music INPUT musical_note SWITCH (musical_note) CASE ‘c’ : OUTPUT “ do “ BREAK CASE ‘d’ : OUTPUT “ re “ BREAK CASE ‘e’ : OUTPUT “ mi “ BREAK CASE ‘f’ : OUTPUT “ fa “ BREAK CASE ‘g’ : OUTPUT “ sol “ BREAK CASE ‘a’ : OUTPUT “ la “ BREAK CASE ‘b’ : OUTPUT “ ti “ BREAK DEFAULT: OUTPUT “ Invalid note was read “ END SWITCH END Music

Multiple Way Selection (SWITCH) Example: Write an algorithm to use SWITCH statement to present menu asking the user to enter a specified letter to perform the corresponding task. The algorithm is to calculate the area of the circle if the letter a is read, and to calculate the circumference if letter c is read. Analysis stage: Problem Input: - radius of the circle - a character to perform a specified task Problem Output: depending on the character read, the output is: - area of the circle or - circumference of the circle Example: Write an algorithm to use SWITCH statement to present menu asking the user to enter a specified letter to perform the corresponding task. The algorithm is to calculate the area of the circle if the letter a is read, and to calculate the circumference if letter c is read. Analysis stage: Problem Input: - radius of the circle - a character to perform a specified task Problem Output: depending on the character read, the output is: - area of the circle or - circumference of the circle

Multiple Way Selection (SWITCH) Algorithm Design ALGORITHM Circle OUTPUT “ Enter the radius of a circle: “ INPUT radius OUTPUT “ Enter a to calculate the area of a circle or c to calculate its circumference:” INPUT ch SWITCH (ch) CASE ‘a’ : area  3.14 * radius * radius OUTPUT “ Area = “, area BREAK CASE ‘c’ : circum  2 * radius * 3.14 OUTPUT “ Circumference = “, circum BREAK DEFAULT: OUTPUT “ Invalid letter was read “ END SWITCH END Circle Algorithm Design ALGORITHM Circle OUTPUT “ Enter the radius of a circle: “ INPUT radius OUTPUT “ Enter a to calculate the area of a circle or c to calculate its circumference:” INPUT ch SWITCH (ch) CASE ‘a’ : area  3.14 * radius * radius OUTPUT “ Area = “, area BREAK CASE ‘c’ : circum  2 * radius * 3.14 OUTPUT “ Circumference = “, circum BREAK DEFAULT: OUTPUT “ Invalid letter was read “ END SWITCH END Circle

Exercise 1 Design an algorithm that will receive two integer items and display to the screen their sum, difference, product and quotient. Note that the quotient calculation (first integer divided by second integer) is only to be performed if the second integer does not equal zero.

Exercise 2 Design an algorithm that will prompt an operator for a student’s serial number and the student’s exam score out of 100. Your program is then to match the exam score to a letter grade and print the grade to the screen. Calculate the letter grade as follows: Exam scoreGrade 90 and aboveA B C D Below 60F

Conclusion This chapter covered the selection control structure in detail Depends on the problem you may use one way, two way and multiple way selection For multiple way selection you can use either IF or SWITCH This chapter covered the selection control structure in detail Depends on the problem you may use one way, two way and multiple way selection For multiple way selection you can use either IF or SWITCH

REFERENCE ures/ch4/index.html ures/ch4/index.html Lesley Anne. Simple Program Design, A Step-by-Step Approach. 4 th Edition. Thomson Course Technology. ures/ch4/index.html ures/ch4/index.html Lesley Anne. Simple Program Design, A Step-by-Step Approach. 4 th Edition. Thomson Course Technology.