Control Structures contd… Selection

Slides:



Advertisements
Similar presentations
1 Engineering Problem Solving With C++ An Object Based Approach Chapter 3 Control Structures.
Advertisements

Conditional Operator (?:) Conditional operator (?:) takes three arguments (ternary) Syntax for using the conditional operator:
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.
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 1 st semester King Saud University College of Applied studies and Community Service Csc 1101.
Selection Statements in C++ If Statement in C++ Semantics: These statements have the same meaning as in the algorithmic language. 2- Two way selection:
Computer Science Department Relational Operators And Decisions.
Previously Repetition Structures While, Do-While, For.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
Chapter 05 (Part III) Control Statements: Part II.
1 ELEC 206 Chapter 3 Control Structures 5-Step Problem Solving Methodology 1. State the problem clearly. 2. Describe the input and output. 3. Work a.
C++ Programming Lecture 7 Control Structure I (Selection) – Part II The Hashemite University Computer Engineering Department.
Chapter 4, cont: Control Structures if else nested if switch.
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.
Switch Selection Structure (L14) * General Form of the switch Statement * Details of switch Statement * Flowchart of switch Statement * cin.get * Character.
Lecture 7 Computer Programming -1-. Conditional Statements 1- if Statement. 2- if ….. else Statement. 3- switch.
BY ILTAF MEHDI(MCS,MCSE, CCNA) 1. INSTRUCTOR: ILTAF MEHDI (MCS, MCSE, CCNA, Web Developer) BY ILTAF MEHDI(MCS,MCSE, CCNA) 2 Programming Fundamentals Chapter.
Chapter 3 Control Statements
SETS AND VENN DIAGRAMS.
Sunday Monday Tuesday Wednesday Thursday Friday Saturday
LESSON 4 Decision Control Structure
Introduction to programming in java
Engineering Problem Solving with C++, Etter/Ingber
Days of the week NEXT.
Chapter 6: Conditional Statements and Loops
Days of the Week Les docs d’Estelle –
The C “switch” Statement
DAYS OF THE WEEK.
The C “switch” Statement
Time management School of Rock.
A Class Calendar in PowerPoint
JANUARY 2018 SUNDAY MONDAY TUESDAY WEDNESDAY THURSDAY FRIDAY SATURDAY
Sunday Monday Tuesday Wednesday Sunday Monday Tuesday Wednesday
CS-161 Computer Programming Lecture 14: Arrays I
Enumerations CS Fall 1999 Enumerations.
Selection Control Structure: Switch Case Statement
Control Structures: Selection Statement
The switch Statement Topics Multiple Selection switch Statement
The switch Statement Topics Multiple Selection switch Statement
Topics 4.1 Relational Operators 4.2 The if Statement
MY FUNDAY.
FIZZ FREE FEBRUARY 2019 Your name: Your school:
Chapter 4: Control Structures I (Selection)
Control Structures Part 3
Days of the Week Monday Tuesday Wednesday Friday Thursday Saturday
Unit 1: Quick Quizzes After 5,13
SEPTEMBER 2014 Unit 1 Unit 1 Unit 1 Unit 1 Quick Quiz 9,16, 21 Unit 1
SEPTEMBER ½ Day Unit PLC
JANUARY 2018 SUNDAY MONDAY TUESDAY WEDNESDAY THURSDAY FRIDAY SATURDAY
Extended Christmas Hours Thursday December 8th 9am -6:30pm Friday December 9th 9am -6:30pm Saturday December 10th 9am-6pm Thursday December.
January 2015 Sunday Monday Tuesday Wednesday Thursday Friday Saturday
Control Structures Selection
Control Structures: Selection Statement
| January Sunday Monday Tuesday Wednesday Thursday Friday
Time 1.
Contact
#teacher5aday Stress Awareness Month
NOVEMBER READING LOG Student: When you have read, record your minutes and have your parent initial the proper box (each day). At the end of the month,
The switch Statement Topics Multiple Selection switch Statement
DECEMBER READING LOG Student: When you have read, record your minutes and have your parent initial the proper box (each day). At the end of the month,
WEEK 1 Monday Tuesday Wednesday Thursday Friday Saturday Sunday
Introduction to Programming - 1
Selection Control Structure
2011年 5月 2011年 6月 2011年 7月 2011年 8月 Sunday Monday Tuesday Wednesday
March 2008 Sunday Monday Tuesday Wednesday Thursday Friday Saturday 1

BOOKINGS – Monday, 2nd July BAYWAVE
C++ Programming Lecture 7 Control Structure I (Selection) – Part II
Mr. Butler 6th Grade Earth Science
January Monday Tuesday Wednesday Thursday Friday Saturday Sunday 30 31
Presentation transcript:

Control Structures contd… Selection Dr. Khizar Hayat Associate Prof. of Computer Science

Multiway if…else Example int main() { int day; cout<<"Enter the value for day"<<endl; cin>>day; if(day==1) cout << "Monday"; } else if(day==2) cout << “Tuesday"; if(day==3) cout << “Wednesday"; : if(day==7) cout << “Sunday"; cout << “Invalid input"; }….}} return(0); Example A program that inputs the day number and display the day name’ i.e. 1 for Monday 2 for Tuesday 3 for Wednesday … 7 for Sunday

Multiway if…else Example int main() { int day; cout<<"Enter the value for day"<<endl; cin>>day; if(day==1) cout << “Monday"; } else if(day==2) cout << “Tuesday"; else if(day==3) cout << “Wednesday"; else if(day==4) cout << “Thursday"; : else if(day==7) cout << “Sunday"; else cout<<“Invalid Input"; return(0); Example A program that inputs the day number and display the day name’ i.e. 1 for Monday 2 for Tuesday 3 for Wednesday … 7 for Sunday

Multiway if…else Example int main() { int day; cout<<"Enter the value for day"<<endl; cin>>day; if(day==1) cout << “Monday"; else if(day==2) cout << “Tuesday"; else if(day==3) cout << “Wednesday"; else if(day==4) cout << “Thursday"; else if(day==5) cout << “Friday"; else if(day==6) cout << “Satursday"; else if(day==7) cout << “Sunday"; else cout<<“Invalid Input"; return(0); } Example A program that inputs the day number and display the day name’ i.e. 1 for Monday 2 for Tuesday 3 for Wednesday … 7 for Sunday

The switch structure switch(expression) //The term in ()is called the selector { case constant1: //constant1 also called label Statement(s); break; //needed to skip the rest of the cases case constant2: break; . default //optional part }

The switch int main(void) { int day; cout << "Enter the day of the week between 1 and 7"; cin >> day; switch(day) case 1: cout << "Monday"; break; case 2: cout << "Tuesday"; case 3: cout << "Wednesday"; case 4: cout << "Thursday"; case 5: cout << "Friday"; case 6: cout << "Saturday"; case 7: cout << "Sunday"; default: //optional cout << “Invalid Input"; } return(0); Example A program that inputs the day number and display the day name’ i.e. 1 for Monday 2 for Tuesday 3 for Wednesday … 7 for Sunday

The switch structure The switch keyword. Same as if…else if….else but limited; Can only be used to compare an expression against constants The case labels must have the same type as the selector The case labels must all be different You can associate more than one case labels with the same statement. No need to use the delimiters ‘{}’ in case statement(s); Omitting or forgetting break in a case is not a syntax error but a logical error. It would take you to the subsequent case part No need to write break in the default case but good for readability.

Problems Write a program that takes a character input and tells you whether it is a vowel or not. We may introduce getche() from conio.h displaying a menu in a program.