Selection Control Structure CSC128: FUNDAMENTALS OF COMPUTER PROBLEM SOLVING

Similar documents
5. Selection: If and Switch Controls

Boolean Data-Type. Boolean Data Type (false, true) i.e. 3/6/2018. The type bool is also described as being an integer: bool bflag; bflag = true;

Chapter 4: Control Structures I (Selection) Objectives. Objectives (cont d.) Control Structures. Control Structures (cont d.

Chapter 5 Conditional and Iterative Statements. Statement are the instructions given to the computer to perform any kind of action.

Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures. Control Structures (cont d.) Relational Operators

STUDENT OUTLINE. Lesson 8: Structured Programming, Control Structures, if-else Statements, Pseudocode

LOGO BASIC ELEMENTS OF A COMPUTER PROGRAM

Lecture 5 Tao Wang 1

Control Structure: Selection

4.1. Chapter 4: Simple Program Scheme. Simple Program Scheme. Relational Operators. So far our programs follow a simple scheme

More Complex Versions of the if Statement. Class 13

Computer Engineering Department CMPE110 Midterm Sample Questions, 2017/ Fall

Recognize the correct ordering of decisions in multiple branches Program simple and complex decision

Chapter 4: Control Structures I (Selection)

Chapter 4 - Notes Control Structures I (Selection)

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Chapter 4: Making Decisions

Chapter 4: Making Decisions

Decision Structures. Lecture 3 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

Repetition Structures

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

Decisions. Arizona State University 1

Week 2. Relational Operators. Block or compound statement. if/else. Branching & Looping. Gaddis: Chapters 4 & 5. CS 5301 Spring 2018.

Chapter Two: Program Design Process and Logic

CHAPTER 2.1 CONTROL STRUCTURES (SELECTION) Dr. Shady Yehia Elmashad

Text Input and Conditionals

C++ Programming: From Problem Analysis to Program Design, Fourth Edition. Chapter 4: Control Structures I (Selection)

LECTURE 04 MAKING DECISIONS

Loops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017

Chapter 3 Problem Solving and the Computer

C++ Programming: From Problem Analysis to Program Design, Third Edition

Boolean Expressions (Conditions)

Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures

LECTURE 5 Control Structures Part 2

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS240 BRANCHING STATEMENTS

REPETITION CONTROL STRUCTURE LOGO

Relational operators (1)

Chapter 3. More Flow of Control. Copyright 2008 Pearson Addison-Wesley. All rights reserved.

Decision Structures. Lesson 03 MIT 11053, Fundamentals of Programming

Introduction to Decision Structures. Boolean & If Statements. Different Types of Decisions. Boolean Logic. Relational Operators

Control Statements. If Statement if statement tests a particular condition

Final Examination Semester 3 / Year 2010

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal


Programming Logic and Design Sixth Edition

Score score < score < score < 65 Score < 50

REVIEW. The C++ Programming Language. CS 151 Review #2

LESSON 3 CONTROL STRUCTURES

BITG 1223: Selection Control Structure by: ZARITA (FTMK) LECTURE 4 (Sem 1, 16/17)

Checking Multiple Conditions

Pseudocode syntax, descriptions and examples

Add Subtract Multiply Divide

C++ Programming: From Problem Analysis to Program Design, Fourth Edition. Chapter 5: Control Structures II (Repetition)

Topics. Chapter 5. Equality Operators

C++ PROGRAMMING SKILLS Part 2 Programming Structures

Chapter 4: Making Decisions. Copyright 2012 Pearson Education, Inc. Sunday, September 7, 14

CHAPTER 1: PROGRAM DEVELOPMENT LIFE CYCLE. Prepared for: CSC 128 Fundamentals of Computer Problem Solving

ECOR Come to the PASS workshop with your mock exam complete. During the workshop you can work with other students to review your work.

CSC128 FUNDAMENTALS OF COMPUTER PROBLEM SOLVING Chapter 4: Repetition Control Structure

Java I/O and Control Structures

CHRIST THE KING BOYS MATRIC HR. SEC. SCHOOL, KUMBAKONAM CHAPTER 9 C++

Repetition Algorithms

Programming Language. Control Structures: Selection (switch) Eng. Anis Nazer First Semester

Superior University. Department of Electrical Engineering CS-115. Computing Fundamentals. Experiment No.5. for loop and do-while loop

Class 2: Variables and Memory. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

Introduction. C provides two styles of flow control:

QUIZ: What value is stored in a after this

Computer Science & Engineering 150A Problem Solving Using Computers

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

Lecture 3 (02/06, 02/08): Condition Statements Decision, Operations & Information Technologies Robert H. Smith School of Business Spring, 2017

C++ Programming: From Problem Analysis to Program Design, Third Edition

Programming for Engineers Iteration

Making Decisions In Python

Repetition CSC 121 Fall 2014 Howard Rosenthal

Lab - 8 Awk Programming

How to code control statements

Chapter 5 Selection Statements. Mr. Dave Clausen La Cañada High School

DECISION STRUCTURES: USING IF STATEMENTS IN JAVA

Chapter 5. Repetition. Contents. Introduction. Three Types of Program Control. Two Types of Repetition. Three Syntax Structures for Looping in C++

In this chapter you will learn:

1) What of the following sets of values for A, B, C, and D would cause the string "one" to be printed?

Introduction to Programming

Chapter 8. How to code control statements. 2010, Mike Murach & Associates, Inc. Murach's PHP and MySQL, C8

Course Outline. Introduction to java

Homework 2 Solutions Group B 1- Write a C++ program to read a students score in Statistics and print if he is successful or failing.

Looping Subtasks. We will examine some basic algorithms that use the while and if constructs. These subtasks include

Week 4 Selection Structures. UniMAP Sem II-11/12 DKT121 Basic Computer Programming 1

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

IT 1003 Introduction to Programming (New)

The while Loop 4/6/16 4

Information Science 1

Programming: Java. Chapter Objectives. Control Structures. Chapter 4: Control Structures I. Program Design Including Data Structures

Lecture 5: Making Decisions

Chapter Overview. More Flow of Control. Flow Of Control. Using Boolean Expressions. Using Boolean Expressions. Evaluating Boolean Expressions

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

Lecture 7 Tao Wang 1

Java I/O and Control Structures Algorithms in everyday life

PDS Lab Section 16 Autumn Tutorial 3. C Programming Constructs

Programming Basics and Practice GEDB029 Decision Making, Branching and Looping. Prof. Dr. Mannan Saeed Muhammad bit.ly/gedb029

C++ Program Flow Control: Selection

Transcription:

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 nested if multiple path 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 Code == M F Code == F F T T Male Female Invalid code

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

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) else cout<< teenager ; 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 ;

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 C A E Program Computer Science Accounting Engineering

Exercise Problem statement: Write a C++ program to determine the grade of a student based on the marks. Marks Program 0-49 F 50-64 C 65 74 B 75-100 A

Multiple selection nested if

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 Person younger than 18 years old Person is at least 18 years old and is registered to vote Person is at least 18 years old but not registering to vote Message you are too young to vote you can 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 else display you are too young to vote end if End

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

Multiple selection nested if C++ syntax cin>>age; if (age < 18) else { cout<< you are too young to vote ; cin>>status; if (status == y ) cout<< you can vote ; else 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. Code C Program Computer Science C++ Program segment: if( code == C ) cout<<. ; else if ( code == E ) cout<<. ;... A E Accounting 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 C A E Program Computer Science Accounting Engineering

Characters ASCII codes

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 ;

switch Structures switch structure: alternate to if-else switch (integral) expression is evaluated first value of the expression determines which corresponding action is taken expression is sometimes called the selector C++ Programming: From Problem Analysis to Program Design, Fourth Edition 22

switch Structures (continued) one or more statements may follow a case label braces are not needed to turn multiple statements into a single compound statement the break statement may or may not appear after each statement switch, case, break, and default are reserved words switch statements case labels identify specific code segments can use a variety of data types as case labels break statement used to exit switch statements default label contains statements that execute when the condition expression doesn t match any of the case labels C++ Programming: From Problem Analysis to Program Design, Fourth Edition 24

Exercise Write a program in switch statements that accepts a code of type integer and determines the name of the program for that code based on the following table. Code Program 1 Diploma in Engineering 2 Diploma in Computer Science Sample Output 1 Enter Code [1, 2, 3 or 4] : 4 Diploma in Banking End of Program. Thank you. Sample Output 2 Enter Code [1, 2, 3 or 4] : 6 Invalid Code End of Program. Thank you. 3 Diploma in Business Study 4 Diploma in Banking

switch Exercise Convert following C++ nested if statements into switch statements

switch Exercise ANSWERS

switch Exercise Write a program fragment that accepts a grade of type characternand determines the range of score for that grade as shown below. Grade Score Range A 90 100 B 80 89 C 70 79 D 60 69 E 0-59 If the grade entered is other than the above, an error message will be displayed.

switch Exercise Using a switch statement, write a program that reads two integers and a character. The operation to be performed is based on the character input, as indicated in the table below:- Character Input Operation + Display the sum of two numbers - Display the difference / Display the quotient * Display the product % Display the remainder