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.

Slides:



Advertisements
Similar presentations
Chapter 4 - Control Statements
Advertisements

Logic & program control part 3: Compound selection structures.
Pascal Programming Today Chapter 4 1 »Conditional statements allow the execution of one of a number of possible operations. »Conditional statements include:
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
1 10/16/06CS150 Introduction to Computer Science 1 switch Selection Structure.
1 9/26/08CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
1 9/28/07CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Control Statements.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
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.
Unit 3 Lesson 8 Selection Structures Mr. Dave Clausen La Cañada High School.
CONTROL STATEMENTS IF-ELSE, SWITCH- CASE Introduction to Computer Science I - COMP 1005, 1405 Instructor : Behnam Hajian
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)
Selection Statements in C++ If Statement in C++ Semantics: These statements have the same meaning as in the algorithmic language. 2- Two way selection:
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Switch Statement in C++. Syntax switch (selector) { case L1: statements1; break; case L2: statements2; break; … default: statements_n; } Semantics: This.
Decision II. CSCE 1062 Outline  Boolean expressions  switch statement (section 4.8)
Chapter 3 Selections Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 9 – Income Tax Calculator Application: Introducing.
Flow of Control Part 1: Selection
© 2000 Scott S Albert Selection Structures Structured Programming 256 Chapter 4.
Decisions and Debugging Part06dbg --- if/else, switch, validating data, and enhanced MessageBoxes.
PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION.
Control Structures (A) Topics to cover here: Introduction to Control Structures in the algorithmic language Sequencing.
Previously Repetition Structures While, Do-While, For.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
 Learn about control structures  Examine relational and logical operators  Explore how to form and evaluate logical (Boolean) expressions  Learn how.
Chapter 05 (Part III) Control Statements: Part II.
Lecture 3 – Selection. Outline Recall selection control structure Types of selection One-way selection Two-way selection Multi-selection Compound statement.
Selection Relational Expressions A condition or logical expression is an expression that can only take the values true or false. A.
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 1 st semester King Saud University College of Applied studies and Community Service Csc 1101.
1 09/15/04CS150 Introduction to Computer Science 1 Life is Full of Alternatives Part 2.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
111/18/2015CS150 Introduction to Computer Science 1 Announcements  I have not graded your exam yet.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
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.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
Algorithm Discovery and Design Objectives: Interpret pseudocode Write pseudocode, using the three types of operations: * sequential (steps in order written)
Week 4 Program Control Structure
Chapter 3 Decisions Three control structures Algorithms Pseudocode Flowcharts If…then …else Nested if statements Code blocks { } multi statement blocks.
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:
1 9/26/05CS150 Introduction to Computer Science 1 Life is Full of Alternatives.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 11 So Many Paths … So Little Time.
Chapter 10 So Many Paths … So Little Time (Multiple-Alternative Selection Structures) Clearly Visual Basic: Programming with Visual Basic nd Edition.
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.
 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.
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Decisions Chapter 4.
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
Chapter 3 Selections Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Chapter 6: Conditional Statements and Loops
Control Structure Senior Lecturer
Compound Assignment Operators in C++
SELECTION STATEMENTS (2)
Control Structures: Selection Statement
CSE 1020:Control Structure
Chapter 5: Control Structure
switch Selection Structure
SELECTIONS STATEMENTS
Week 3 – Program Control Structure
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Control Structures: Selection Statement
Selection Control Structure
Life is Full of Alternatives Part 3
Presentation transcript:

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 (b)Multi way selection by SWITCH structure

Multi Way Selection by Nested IF Structure 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.

Multi Way Selection by Nested If Structure.. Cont. 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.

Multi Way Selection by Nested If Structure.. Cont. Condition1 Condition2 Statements2 Statements3 Rest of algorithm Statements1 Condition3 Statements4 True False

Multi Way Selection by Nested If Structure.. Examples Example 4: 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 4.. Cont. 1- Analysis stage: Problem Input: - student’s mark, mark Problem Output: - grade Criteria - according to the previous grade table

Example 4.. Cont. 2- 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

Comparison of Nested IF Structure and a Sequence of IF Structures Some times the nested IF structure is a good solution for a given problem than a sequence of IF structures. Consider the following example: A) Sequence case: IF ( x > 0 ) THEN pos_count  pos_count + 1 END IF IF ( x < 0 ) THEN neg_count  neg_count + 1 END IF IF ( x = 0 ) THEN zero_count  zero_count + 1 END IF

( B) Nested case: IF ( x > 0 ) THEN pos_count  pos_count + 1 ELSE IF ( x < 0 ) THEN neg_count  neg_count + 1 ELSE zero_count  zero_count + 1 END IF

In the previous example, only one statement should be executed for any value of x. In the sequence case, the sequence does not show clearly that exactly one of the three statements is executed for any value of x. In the nested case, only one statement is executed exactly. Therefore, it is better than the sequence case.

Multi Way Selection by SWITCH Structure 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.

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

Multi Way Selection by SWITCH Structure.. Cont. 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:

Multi Way Selection by SWITCH Structure.. Cont. selector Statements1 BREAK Statements2 BREAK Statements_n BREAK … Rest of algorithm label1 label2 DEFUALT

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

Multi Way Selection by SWITCH Structure Examples.. Cont. 2- 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

Multi Way Selection by SWITCH Structure Examples.. Cont. Example 6 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. 1- 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

Multi Way Selection by SWITCH Structure Examples.. Cont. 2- 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