Selection Control Structures 2 (L09) * Nested if Statements * The if-else Chain * Exercise: if, if-else, nested if, and if-else chain Selection Control.

Slides:



Advertisements
Similar presentations
Chapter 4: Making Decisions.
Advertisements

LECTURE 1 CMSC 201. Overview Goal: Problem solving and algorithm development. Learn to program in Python. Algorithm - a set of unambiguous and ordered.
1 EMT 101 – Engineering Programming Dr. Farzad Ismail School of Aerospace Engineering Universiti Sains Malaysia Nibong Tebal Pulau Pinang Week 3.
1 CSC103: Introduction to Computer and Programming Lecture No 8.
CMPUT 101 Lab # 5 October 22, :00 – 17:00.
Do/while Structure (L15) * do/while structure * break Statement * continue Statement * Loop Programming Techniques - Interactive input within a loop -
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
1 10/11/06CS150 Introduction to Computer Science 1 do/while and Nested Loops.
Function (L16) * Mathematical Library Functions * Program Components in C++ * Motivations for Functionalizing a Program * Function Prototype * Function.
1 9/25/06CS150 Introduction to Computer Science 1 Nested Ifs, Logical Operators, exit() Page 194.
Function 2 (L17) * Function Prototype * Promotion Rules * Data Type * Library Header File * Customer Header File * Case Study * Exercise/Home Work Dr.
The If/Else Statement, Boolean Flags, and Menus Page 180
Computer Science 1620 Selection Structures. Write a program that accepts the speed of a vehicle, and determines whether that person is speeding assume.
Programming Fundamentals1 Chapter 4 SELECTION STRUCTURES.
 Write a program that uses a one dimension to do a table look-up  Learn about parallel arrays.
Chapter 05 (Part V) Control Statements: Part II. Nested For-Structures Consider the following codes: for (int i=0; i
 Review structures  Program to demonstrate a structure containing a pointer.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 4: Continuing with C++ I/O Basics.
1 EMT 101 – Engineering Programming Dr. Farzad Ismail School of Aerospace Engineering Universiti Sains Malaysia Nibong Tebal Pulau Pinang Week 4.
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.
Selection Structures (if & switch statements) (CS1123)
1 Value Returning Functions // Function prototype int Largest(int num1, int num2, int num3); Function Name Type Parameters Type of parameters Formal parameters.
1 Lecture 5: Part 1 Searching Arrays Searching Arrays: Linear Search and Binary Search Search array for a key value Linear search  Compare each.
Completing the Basic (L06)
Current Assignments Homework 3 is due tonight. Iteration and basic functions. Exam 1 on Monday.
Nested Control Structures * Conditional Operator (?:) * Preincrement and Postincrement * Predecrement and Postdecrement * Counter-Controlled Repetition.
CSE1222: Lecture 6The Ohio State University1. Common Mistakes with Conditions (1)  Consider the following code: int age(26); if (age = 18) { cout
Previously Repetition Structures While, Do-While, For.
Review the following: if-else One branch if Conditional operators Logical operators Switch statement Conditional expression operator Nested ifs if –else.
Lecture 8: Making Decisions Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
1 EMT 101 – Engineering Programming Dr. Farzad Ismail School of Aerospace Engineering Universiti Sains Malaysia Nibong Tebal Pulau Pinang Week 2.
Review the following : Flowcharting Variable declarations Output Input Arithmetic Calculations Conditional Statements Loops.
Chapter 4 Selection: the if-else and switch-case instructions.
Lecture 7: Making Decisions Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Class 3 (L33) u Client of a Class u Purpose of Public Members u Private Class Members u Controlling Access u Access Functions u Predicate Functions u Utility.
PEG200/Saidatul Rahah 1.  Selection Criteria › if..else statement › relational operators › logical operators  The if-then-else Statement  Nested if.
Selection Structures: if and switch Statements. 2 Selection Statements –In this chapter we study statements that allow alternatives to straight sequential.
Input a number #include using namespace std; int main() { int num; cout num; return 0; }
C++ Programming Lecture 7 Control Structure I (Selection) – Part II The Hashemite University Computer Engineering Department.
Selection Control Structures (L08) * Selection Criteria * if Statements * The if-else Statement Selection Control Structure Dr. Ming Zhang.
 for loop  while loop  do-while loop for (begin point; end point ; incrementation ) { //statements to be repeated }
Programming Fundamentals1 Chapter 4 SELECTION STRUCTURES.
Review 1 Computers and Programming I Dr. Ming Zhang Tel: (757) Fax: (757) Office: Gosnold 217b Subject Overview.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
Hello World Using C++ (L03) * Introduction C++ * Programming Style * Hello World Using C++ Hollow World Using C/C++ Dr. Ming Zhang.
April 11, 2005 More about Functions. 1.Is the following a function call or a function header? calcTotal(); 2.Is the following a function call or a function.
A First Book of C++ Chapter 4 Selection. Objectives In this chapter, you will learn about: –Relational Expressions –The if-else Statement –Nested if Statements.
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 2 September 3, 2009.
 Memory setup  Pointer declaration  Address operator  Indirection  Printing addresses or pointers.
Lecture 7 Computer Programming -1-. Conditional Statements 1- if Statement. 2- if ….. else Statement. 3- switch.
CSE202: Lecture 5The Ohio State University1 Selection Structures.
Lesson xx Why use functions Program that needs a function Function header Function body Program rewritten using a function.
A First Book of C++ Chapter 4 Selection.
1 Chapter 4 - Control Statements: Part 1 Outline 4.1 Introduction 4.4 Control Structures 4.5 if Selection Structure 4.6 if/else Selection Structure 4.7.
生查子 ~ 歐陽修 去年元夜時,花市燈如晝, 月上柳梢頭,人約黃昏後; 今年元夜時,月與燈依舊, 不見去年人,淚濕春衫袖。
Two-Dimensional Arrays Lesson xx
Relational Operators A relational operator compares two values. The values can be any built-in C++ data type, such as Character Integer Floating point.
CSC113: Computer Programming (Theory = 03, Lab = 01)
Arrays Part-1 Armen Keshishian.
Repetition Statements
Chapter 2 Elementary Programming
Value returning Functions
Counting Loops.
Self study.
Statements and flow control
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)
C++ Programming Lecture 7 Control Structure I (Selection) – Part II
Presentation transcript:

Selection Control Structures 2 (L09) * Nested if Statements * The if-else Chain * Exercise: if, if-else, nested if, and if-else chain Selection Control Structure Dr. Ming Zhang

Nested if Statement * The inclusion of one or more if statements within an existing if statement is called a nested if statement. * if ( expression1) { if (expression2) statement 1; } else Statement3; Selection Control Structure Dr. Ming Zhang

Example 1 of Nested if Statement if (hours < 9) { if (distance > 500) cout << “snap”<< endl; } else cout << “pop” << endl; Selection Control Structure Dr. Ming Zhang

if-else Nested within the if part if ( expression1) { if (expression2) statement 1; else statement 2; } else Statement3; Selection Control Structure Dr. Ming Zhang

Example 2 of Nested if Statement if (hours < 9) { if (distance > 500) cout << “snap”<< endl; else cout << “ drink” << endl; } else cout << “pop” << endl; Selection Control Structure Dr. Ming Zhang

if-else Nested within the else part if ( expression1) statement 1; else if (expression2) statement2; else statement3; Selection Control Structure Dr. Ming Zhang

Example 3 of Nested if Statement if (hours > 9) cout << “pop” << endl; else { if (distance > 500) cout << “snap”<< endl; else cout << “ drink” << endl; } Selection Control Structure Dr. Ming Zhang

Exercise: if-else Chain (C ++) #include using std::cout; using std::endl; using std::cin; int main( ) {char marcode; cout << “Enter a marital code:”; cin >> marcode; if (marcode == ‘M’) cout << “Individual is married.”<< endl; else if (marcode == ‘S’) cout << “Individual is single.” << endl; else if (marcode == ‘D’) cout << “Individual is divorced.” << endl; return (0);} Selection Control Structure Dr. Ming Zhang

Monthly Income of a Salesperson Monthly Sales (ms) Monthly Income ms >= $50000 $375+16%of sales $50000> ms >=$40000 $ % of sales $40000 > ms $ % of sales Selection Control Structure Dr. Ming Zhang

Exercise:Monthly Income (C++) #include using std::cout; using std::cin; using std::endl int main( ) { int ms; float income; cout << “Enter the value of monthly sales:”; cin >> ms; if(ms >= 50000) income= *ms; else if(ms >=40000) income = *ms; else if(ms <40000) income = *ms; cout << “The income is:” << income << endl; return(0);} Selection Control Structure Dr. Ming Zhang

Home Work 1: Monthly Income Programming and Running Monthly Sales (ms) Monthly Income ms >= $50000 $375+16%of sales $50000 >ms >=$40000 $ % of sales $40000 >ms >=$30000 $ % of sales $30000 >ms >=$20000 $300+9% of sales $20000 >ms >=$10000 $250+5% of sales $10000 >ms $200+3% of sales Selection Control Structure Dr. Ming Zhang

Homework 2: C++ for Gross Payment Develop a C++ program that will determine the gross pay for each of several employees. The company pays "straight-time" for the first 40 hours worked by each employee and pays "time-and-a- half" for all hours in excess of 40 hours. You are given a list of the employee of the company, the number of hours each employee worked last week, and the hourly rate of each employee. Your program should input this information for only one employee, and should determine and display the employee's gross pay. Selection Control Structure Dr. Ming Zhang