Selection Control Structure

Slides:



Advertisements
Similar presentations
Subject: Information Technology Grade: 10
Advertisements

1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 9/24/07CS150 Introduction to Computer Science 1 Relational Operators and the If Statement.
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
The If/Else Statement, Boolean Flags, and Menus Page 180
12-2 Know how if and switch C statements control the sequence of execution of statements. Be able to use relational and logical operators in the conditional.
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
CHAPTER 7 DATA INPUT OUTPUT Prepared by: Lec. Ghader R. Kurdi.
PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
22/11/ Selection If selection construct.
PEG200/Saidatul Rahah 1.  Selection Criteria › if..else statement › relational operators › logical operators  The if-then-else Statement  Nested if.
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
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.
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Session 3 Decision Trees Conditionals.
Programming Fundamentals by Dr. Nadia Y. Yousif1 Control Structures (Selections) Topics to cover here: Selection statements in the algorithmic language:
C++ Programming Lecture 5 Control Structure I (Selection) – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook.
Algorithms JPC and JWD © 2002 McGraw-Hill, Inc. 2 Algorithms 2 An Algorithm is a finite set of precise instructions for performing a computation or for.
Introduction to Programming Python Lab 7: if Statement 19 February PythonLab7 lecture slides.ppt Ping Brennan
CSE202: Lecture 5The Ohio State University1 Selection Structures.
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
GCSE COMPUTER SCIENCE Practical Programming using Python Lesson 4 - Selection.
Decision making If.. else statement.
The Ohio State University
Basic concepts of C++ Presented by Prof. Satyajit De
GCSE COMPUTER SCIENCE Practical Programming using Python
Chapter 4: Control Structures I (Selection)
REPETITION CONTROL STRUCTURE
Input and Output Upsorn Praphamontripong CS 1110
while Repetition Structure
Review Materials I (T) Subject : T0016 – ALGORITHM AND PROGRAMMING
LESSON 4 Decision Control Structure
Chapter 4: Making Decisions.
C++ Basic Syntax – Homework Exercises
Introduction to Programming
Engineering Problem Solving with C++, Etter/Ingber
CSC113: Computer Programming (Theory = 03, Lab = 01)
Chapter 4: Control Structures
If, else, elif.
Chapter 4: Making Decisions.
Control Statement Examples
Imperative Programming
Introduction to Programming
Compound Assignment Operators in C++
Control Structure Senior Lecturer
SELECTION STATEMENTS (2)
Intro to Nested Looping
If selection construct
CSE 1020:Control Structure
Introduction to Programming
Chapter#3 Structured Program Development in C++
CSC128 FUNDAMENTALS OF COMPUTER PROBLEM SOLVING
Decision making If statement.
If selection construct
Selection Control Structure
Chapter 4 Selection.
Chapter 5: Control Structure
If Statements.
Intro to Nested Looping
SELECTIONS STATEMENTS
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
Programming Concepts and Database
Structured Program Development in C++
Chapter 3: Selection Structures: Making Decisions
Introduction to Programming
Welcome back! October 11, 2018.
Selection Control Structure
Imperative Programming
Presentation transcript:

Selection Control Structure CSC128: FUNDAMENTALS OF COMPUTER PROBLEM SOLVING

Multiple selection To solve a problem that has several selection, use either of the following method: Multiple selection multiple path if nested if

Multiple selection – multiple path

Multiple selection – multiple path Problem statement: Get a character from the user, if the users enter the letter ‘M’, display “Male”, else if the user enter “F” display “Female”, else display “invalid code”;

Multiple selection – multiple path Pseudocode if the code equal to ‘M’then print “Male” else if the code equal to ‘F’ then print “Female else print “Invalid code”

Multiple selection – multiple path Flowchart T Code == ‘M’ Male F T Code == ‘F’ Female F Invalid code

Multiple selection – multiple path if (code == ‘M’) cout<<“Male”; else if (code == ‘F’) cout<<“Female”; cout<<“Invalid code”; Both type of indention is acceptable if (code == ‘M’) cout<<“Male”; else if (code == ‘F’) cout<<“Female”; else cout<<“Invalid code”;

Exercise Write a pseudocode and flowchart for the following C++ program segment. cin>>age; if (age <=10) cout<<“child”; else if (age >10)&&(age<21) cout<<“teenager”; else cout<<“adult”;

Exercise Can this be done??. cin>>age; if (age <=10) cout<<“child”; if (age >10)&&(age<21) cout<<“teenager”; if (age>=21) cout<<“adult”; A group of one-way selection

Exercise Problem statement: Write a C++ program to display the program of the student based on the code the he or she inputs in the computer. Display “Invalid code” if the student enter wrong code. Code Program C Computer Science A Accounting E Engineering

Exercise Problem statement: Write a C++ program to determine the grade of a student based on the marks. ((mark>=0)&&(mark<=49)) ((mark>=50)&&(mark<=64)) Atau (mark>=75) (mark>=65) Marks Program 0 -49 F 50 -64 C 65 – 74 B 75 - 100 A

Multiple selection – nested if Three shell necklaces nested together

Multiple selection – nested if Problem statement: You want to create voter eligibility program that display that displays one of three messages. The messages and the criteria for displaying each message are shown here: Criteria Message Person younger than 18 years old “you are too young to vote” Person is at least 18 years old and is registered to vote “you can vote” Person is at least 18 years old but not registering to vote “you need to register before you can vote”

Multiple selection – nested if Pseudocode: Begin Enter age if ( the age greater than or equal to 18)then enter registration status if (registration status equal to y)then display “you can vote” else display “ you need to register before voting” end if display “you are too young to vote” End

Multiple selection – nested if Flowchart Enter age age >=18 You are too young to vote Enter registration status status == y You can vote You need to register false true false true

Multiple selection – nested if C++ syntax cin>>age; if (age < 18) cout<<“you are too young to vote”; else { cin>>status; if (status == ‘y’) cout<<“you can vote”; cout<<“you need to register”; //end if }//end if

Predefined function - strcmp The strcmp are in the library with the header file string. #include<string>

Predefined function - strcmp Problem statement: Write a C++ program to display the program of the student based on the code the he or she inputs in the computer. Display “Invalid code” if the student enter wrong code. C++ Program segment: if( code == ‘C’) cout<<“ ….”; else if ( code == ‘E’) . Code Program C Computer Science A Accounting E Engineering

Predefined function - strcmp Write a C++ program to display the code of the student based on the program the he or she inputs in the computer. Display “Invalid program” if the student enter wrong program. Code Program C Computer Science A Accounting E Engineering

Predefined function - strcmp cout<< "please enter program:"; cin.getline(program, 20); if(program == “computer science”) cout<<“C”; else if (program == “Accounting”) cout<<“A”; else if (program == “Engineering”) cout<<“E”; else cout<<“invalid”; Is this the correct way???

Predefined function - strcmp strcmp will accept two sequence of characters. It will return an integer. The integer will be: Negative if s1 is less than s2 Zero is s1 and s2 are equal Positive if s1 is greater than s2 int y = strcmp(s1, s2);

Predefined function - strcmp How to use it??? The output: cout<< "please enter program:"; cin.getline(program, 20); int y = strcmp(program, “computer science”); if(y == 0) cout<<“yes”; else cout<<“no”;

Predefined function - strcmp How to use it?? cout<< "please enter program:"; cin.getline(program, 20); if(strcmp(program, “computer science”) == 0) cout<<“yes”; else cout<<“no”;

Predefined function - strcmp cout<< "please enter program:"; cin.getline(program, 20); if(strcmp(program, “computer science”) == 0) cout<<“C”; else if (strcmp(program, “Accounting”) == 0) cout<<“A”; else if (strcmp(program, “Engineering”) == 0) cout<<“E”; else cout<<“invalid”;

Thank You !