Multi-alternative Selection Both the if clause and the else clause of an if...else statement can contain any kind of statement, including another selection.

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

CSE 1301 Lecture 5B Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
Logic & program control part 3: Compound selection structures.
Chapter 4: Making Decisions.
Programming Switch command. COMP102 Prog. Fundamentals: Switch command / Slide 2 Multiple Selection: The switch Statement value1 action 1 value2 action.
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Chapter 5 Conditions, Logical Expressions, and Selection Control Structures Dale/Weems/Headington.
Slides prepared by Rose Williams, Binghamton University Chapter 3 Flow of Control if-else and switch 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.
Introduction to Java and Software Design Dale Weems Headington Chapter 10 Additional Control Structures and Exceptions Slides by Sylvia Sorkin, The Community.
Control Structures I (Selection)
Conditions, Logical Expressions, and Selection Control Structures Sumber dari :
Chapter 4 Logical Expressions & If-Else. 2 Overview  More on Data Type bool u Using Relational & Logical Operators to Construct & Evaluate Logical Expressions.
Conditional Statements While writing a program, there may be a situation when you need to adopt one path out of the given two paths. So you need to make.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Lecture 5 Selection Control Structures Selection Control Structures Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
1 Chapter 9 Additional Control Structures Dale/Weems/Headington.
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.
CPS120: Introduction to Computer Science Decision Making in Programs.
1 Advanced Programming IF. 2 Control Structures Program of control –Program performs one statement then goes to next line Sequential execution –Different.
Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using.
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?
Functions Modules in C++ are called functions and classes Functions are block of code separated from main() which do a certain task every C++ program must.
1 Chapter 9 Additional Control Structures Dale/Weems.
1 Additional Control Structures. 2 Chapter 9 Topics  Switch Statement for Multi-way Branching  Do-While Statement for Looping  For Statement for Looping.
Switch Statement Is a selection control structure for multi-way branching. SYNTAX switch ( IntegralExpression ) { case Constant1 : Statement(s); // optional.
1 Do-While Statement Is a looping control structure in which the loop condition is tested after each iteration of the loop. SYNTAX do { Statement } while.
Chapter 7 Additional Control Structures. Chapter 7 Topics l Switch Statement for Multi-Way Branching l Do-While Statement for Looping l For Statement.
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
Chapter 05 (Part III) Control Statements: Part II.
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. CIS_IS20_CSLO 1. Explain computer programming concepts CSLO1.6. Explain the purpose of general.
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 1 st semester King Saud University College of Applied studies and Community Service Csc 1101.
CSE 1301 Lecture 8 Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
1 Compound Assignment C++ has a large set of operators for applying an operation to an object and then storing the result back into the object Examples.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
COS120 Software Development Using C++ AUBG Fall semester 2010 Ref book: Problem Solving, Abstraction and Design Using C++ Authors: Frank Friedman, Elliot.
Chapter 5 Conditions, Logical Expressions, and Selection Control Structures Dale/Weems.
PEG200/Saidatul Rahah 1.  Selection Criteria › if..else statement › relational operators › logical operators  The if-then-else Statement  Nested if.
Chapter 5: Control Structures I (Selection). Objectives In this chapter you will: Learn about control structures Examine relational and logical operators.
Programming Fundamentals1 Chapter 4 SELECTION STRUCTURES.
1 CS161 Introduction to Computer Science Topic #8.
1 Conditions, Logical Expressions, and Selection Control Structures.
Control statements Mostafa Abdallah
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
Control Flow Statements
LESSON 4 Decision Control Structure. Decision Control Structures 1. if statement 2. if-else statement 3. If-else-if statement 4. nested if statement 5.
CPS120: Introduction to Computer Science Decision Making in Programs.
1 Advanced Programming IF. 2 Control Structures Program of control –Program performs one statement then goes to next line Sequential execution –Different.
Program Flow Control Addis Ababa Institute of Technology Yared Semu April 2012.
C++ Programming Control Structures I (Selection).
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
LECTURE # 7 : STRUCTURED PROGRAMMING Selection Statements Tr.Hadeel.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
Switch-Case Statement. What is a switch-case?  The switch-case statement is really nothing more than a glorified.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
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.
A mechanism for deciding whether an action should be taken
Decisions Given hours worked and pay rate, calculate total pay
Conditionals & Boolean Expressions
Conditionals & Boolean Expressions
Decisions Given hours worked and pay rate, calculate total pay
Additional Control Structures
Chapter 7 Additional Control Structures
Chapter 4: Control Structures I (Selection)
Structured Program Development in C++
Presentation transcript:

Multi-alternative Selection Both the if clause and the else clause of an if...else statement can contain any kind of statement, including another selection statement. Multi-alternative Selection is also called multi-way branching, and can be accomplished by using NESTED if statements.

if ( Expression1 ) Statement1 else if ( Expression2 ) Statement2. else if ( ExpressionN ) StatementN else Statement N+1 EXACTLY 1 of these statements will be executed. Nested if Statements

Each Expression is evaluated in sequence, until some Expression is found that is true. Only the specific Statement following that particular true Expression is executed. If no Expression is true, the Statement following the final else is executed. Actually, the final else and final Statement are optional. If omitted, and no Expression is true, then no Statement is executed. AN EXAMPLE...

if ( creditsEarned >= 90 ) cout << “SENIOR STATUS “; else if ( creditsEarned >= 60 ) cout << “JUNIOR STATUS “; else if ( creditsEarned >= 30 ) cout << “SOPHOMORE STATUS “; else cout << “FRESHMAN STATUS “; Multi-way Branching

Writing Nested if Statements Display one word to describe the int value of number as “Positive”, “Negative”, or “Zero” Your city classifies a pollution index less than 35 as “Pleasant”, 35 through 60 as “Unpleasant”, and above 60 as “Health Hazard.” Display the correct description of the pollution index value.

One Answer if (number > 0) cout << “Positive”; else if (number < 0) cout << “Negative”; else cout << “Zero”;

Other Answer if ( index < 35 ) cout << “Pleasant”; else if ( index <= 60 ) cout << “Unpleasant”; else cout << “Health Hazard”;

Switch Statement Is a selection control structure for multi-way branching. SYNTAX switch ( IntegralExpression ) { case Constant1 : Statement(s); // optional case Constant2 : Statement(s); // optional. default :// optional Statement(s); // optional }

float weightInPounds = ; char weightUnit ;... // user enters letter for desired weightUnit switch ( weightUnit ) { case ‘P’ : case ‘p’ : cout << weightInPounds << “ pounds “ << endl ; break ; case ‘O’ : case ‘o’ : cout << 16.0 * weightInPounds << “ ounces “ << endl ; break ; case ‘K’ : case ‘k’ : cout << weightInPounds / 2.2 << “ kilos “ << endl ; break ; case ‘G’ : case ‘g’ : cout << * weightInPounds << “ grams “ << endl ; break ; default : cout << “That unit is not handled! “ << endl ; break ; } 9

Switch Statement the value of IntegralExpression (of char, short, int, long or enum type ) determines which branch is executed case labels are constant ( possibly named ) integral expressions. Several case labels can precede a statement

Control in Switch Statement control branches to the statement following the case label that matches the value of IntegralExpression. Control proceeds through all remaining statements, including the default, unless redirected with break if no case label matches the value of IntegralExpression, control branches to the default label, if present--otherwise control passes to the statement following the entire switch statement forgetting to use break can cause logical errors because after a branch is taken, control proceeds sequentially until either break or the end of the switch statement occurs