CONTROL STRUCTURES (MULTI-WAY SELECTION). MULTI-WAY SELECTION  EXTENDED IF-ELSE  Used to select exactly one task out of multiple tasks (or possibly.

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

Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
1 CIS Jan Overview Selection Statements –If Statement –Else –Nested If-Else –Switch Repetition Statements –While statement –For Statement.
1 Lecture 8:Control Structures I (Selection) (cont.) Introduction to Computer Science Spring 2006.
Programming Switch command. COMP102 Prog. Fundamentals: Switch command / Slide 2 Multiple Selection: The switch Statement value1 action 1 value2 action.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Boolean Expressions Conditional Statements & Expressions CSC 1401: Introduction to Programming with Java Lecture 4 – Part 2 Wanda M. Kunkle.
true (any other value but zero) false (zero) expression Statement 2
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
Chapter 5: Control Structures II (Repetition)
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
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.
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
Week 3 – Selection Structures UniMAP SemPGT C PROGRAMMING1.
Chapter 4 Program Control Statements
True or False: Boolean Expression Boolean expression are expressions which evaluate to "true" or "false“ Primary operators to combine expressions: && (and),
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Lecture 5 Selection Control Structures Selection Control Structures Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
COIT29222 Structured Programming Slide 1 COIT29222-Structured Programming Lecture Week 06  Reading: Study Guide Book 2, Modules 9 & 10 Textbook (4 th.
UniMAP Sem II-09/10EKT120: Computer Programming1 Week 3 – Selection Structures.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
Selection Structures (if & switch statements) (CS1123)
Chapter 4 Selection Structures: Making Decisions.
Decision II. CSCE 1062 Outline  Boolean expressions  switch statement (section 4.8)
COSC175-Selection1 Decisions Given hours worked and pay rate, calculate total pay What if you work overtime? How do you indicate if your work overtime?
1 Chapter 9 Additional Control Structures Dale/Weems.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
Control Structures 1. Control Structures Java Programming: From Problem Analysis to Program Design, D.S. Malik 2.
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
1 09/15/04CS150 Introduction to Computer Science 1 Life is Full of Alternatives Part 2.
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.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
PEG200/Saidatul Rahah 1.  Selection Criteria › if..else statement › relational operators › logical operators  The if-then-else Statement  Nested if.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
Chapter 5: Control Structures I (Selection). Objectives In this chapter you will: Learn about control structures Examine relational and logical operators.
1 CS161 Introduction to Computer Science Topic #8.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
CONTROL STRUCTURE. 2 CHAPTER OBJECTIVES  Learn about control structures.  Examine relational and logical operators.  Explore how to form and evaluate.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
Program Flow Control Addis Ababa Institute of Technology Yared Semu April 2012.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Condition – any expression that evaluates to true/false value Relational operators are BINARY.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
Chapter 3 Selection Statements
Chapter 3 Control Statements
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.
Chapter 4: Making Decisions.
EGR 2261 Unit 4 Control Structures I: Selection
DKT121: Fundamental of Computer Programming
לולאות קרן כליף.
Decisions Given hours worked and pay rate, calculate total pay
Selection Control Structure: Switch Case Statement
Chapter#3 Structured Program Development in C++
Flow Control Statements
Chapter 4: Control Structures I (Selection)
Control Structure Chapter 3.
Structured Program Development in C++
The switch Statement When we want to compare a variable against several values to see which one it has, we can use the switch statement: switch (status)
Chapter 3: Selection Structures: Making Decisions
Programming Fundamental
Presentation transcript:

CONTROL STRUCTURES (MULTI-WAY SELECTION)

MULTI-WAY SELECTION  EXTENDED IF-ELSE  Used to select exactly one task out of multiple tasks (or possibly none)  Unlike a series of “ifs” evaluation stops once one relational expression evaluates as true (others are skipped)  Always does on less evaluation than a series of “ifs”  Well planned implementation provides for efficiency – most common choice is listed first

MULTIPLE SELECTION  EXTENDED IF-ELSE - SIMPLE  if ( relational expression(s) ) statement; else if ( relational expression(s) ) statement; else if ( relational expression(s) ) statement; else statement;

MULTI-WAY SELECTION  EXTENED IF-ELSE - COMPOUND  if ( relational expression(s) ) { statement; statement(s); } else if ( relational expression(s) ) { statement; statement(s); } else if ( relational expression(s) ) { statement; statement(s); }

MULTI-WAY SELECTION  RULES 1.First statement(s) will be executed if and only if the evaluation of the relational expression(s) is/are true. 2.Second statement(s) will be executed if and only if the evaluation of the first relational expression(s) is/are false and if and only if the evaluation of the second relational expression(s) is/are true. 3.Same progression as 1 and 2 for all levels that exist. Note: This process does not have to end with two, three or four selections, but can continue for as many selections as needed.

MULTIPLE SELECTION  EXAMPLES: if (numStudents< 6) salary = hours * CLUSTER_RATE; else if (numStudents < 12) salary = hours * LIMITED_RATE; else salary = hours * FULL_RATE

MULTIPLE SELECTION  EXAMPLES: if (temperature <= 10) cout << “Go Bowling” << endl; else if (temperature <= 32) cout << “Go Skiing” << endl; else if (temperature <= 70) cout << “Play Football” << endl; else if (temperature <= 85) { cout << “Play Tennis” << endl; cout << “Wear white!” << endl; } else cout << “Go Swimming” << endl; if (temperature > 85) cout << “Go Swimming” << endl; else if (temperature > 70) { cout << “Play Tennis” << endl; cout << “Wear white!” << endl; } else if (temperature > 32) cout << “Play Football” << end; else if (temperature > 10) cout << “Go Skiing” << endl; else cout << “Go Bowling” << endl;

MULTI-WAY SELECTION  NESTED IF-ELSE  Situations where action is dependent upon two or more different conditions  Examples: if (gender == ‘M’) if (age < MAX_AGE) cout << “Male and under 67.” << endl; else cout << “Male and 67 or over.” << endl; else if (age < MAX_AGE) cout << “Female and under 67.” << endl; else cout << “Female and 67 or over.” << endl; if (savings >= DOLLAR_LIMIT) if (vacation >= VACATION_LIMIT) cout << “Trip to Hawaii” << endl; else cout << “Vacation at local ski resort.” << endl; else cout << “Go camping in mountains.” << endl;

MULTI-WAY SELECTION  SWITCH  Alternate form of multi-way selection  Decision can only be made on integral or enumerated data types (bool, char, int)  Does not work with strings or floating point numbers  Only works when testing for specific values  Does not work with ranges

MULTI-WAY SELECTION  SWITCH - SIMPLE  switch (selector) { case choice1:statement; break; case choice2:statement; break; case choice3:statement; break;“ case choice(n):statement; }

MULTI-WAY SELECTION  SWITCH – WITH DEFAULT  switch (selector) { case choice1:statement; break; case choice2:statement; break;“ case choice(n):statement; break; default:statement; statement(s); }

MULTIPLE SELECTION  SWITCH – COMPOUND  switch (selector) { case choice1:statement; statement(s); break; case choice2:statement; statemant(s); break;“ case choice(n):statement; statement(s); break; default:statement; statement(s); }

MULTIPLE SELECTION  RULES 1.Selector may only be an integral data type (int, char, bool, or enumerated). 2.Corresponding statement(s) is/are executed if choice is equal to the selector. 3.After corresponding statement(s) are executed if break is not present additional case statements are executed until a break is found or end of structure is found. 4.If no choice is equal to the selector, the switch is exited unless a default is available.

MULTIPLE SELECTION  EXAMPLES: switch(category) { case 1: cout << “Category One” << endl; break; case 2: cout << “Category Two” << endl; break; : case n: cout << “Category n” << endl; break; }

MULTI-WAY SELECTION  EXAMPLE: int num1, num2, answer; char choice; cout << "Enter two numbers, separated by a space: "; cin >> num1 >> num2; cout << endl << endl; cout << "Perform which operation on the Numbers?" << endl; cout << " a - Add" << endl; cout << " s - Subtract" << endl; cout << " m - Mulitply" << endl; cout << " d - Divide" << endl; cout << "Enter your choice from above: " << endl; cin >> choice; choice = tolower (choice); switch (choice) { case 'a‘:answer = num1 + num2; cout << "The sum is " << answer << endl; break; case 's‘:answer = num1 - num2; cout << "The difference is " << answer << endl; break; case 'm‘:answer = num1 * num2; cout << "The product is " << answer << endl; break; case 'd‘:answer = num1 / num2; cout << "The quotient is " << answer << endl; break; default : cout << "Invalid choice entered!" << endl; } // end switch

MULTI-WAY SELECTION  EXAMPLE: char answer; cout << "Would you like to continue [Y] or [N]?" << endl; cin >> answer; switch (answer) { case 'Y': case 'y‘:cout << "You chose yes" << endl; cout << "Way to go." << endl; break; case 'N': case 'n‘:cout << "You chose no" << endl; cout << "Not very positive." << endl; break; default :cout << "You were suppose to choose Y or N." << endl; cout << "What happened?" << endl; } // end switch