Control Structures: Selection Statement

Slides:



Advertisements
Similar presentations
INTEC CS160 - Jeanine Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 3 Control Structures and Data Files.
Advertisements

1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
1 Engineering Problem Solving With C++ An Object Based Approach Chapter 3 Control Structures.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
Week 4 Selections This week shows how to use selection statements for more flexible programs. It also describes the various integral types that are available.
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Logical and Relational Expressions Nested if statements.
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
C++ for Engineers and Scientists Third Edition
Visual C++ Programming: Concepts and Projects
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 1 st semester King Saud University College of Applied studies and Community Service Csc 1101.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
2 Objectives You should be able to describe: Relational Expressions Relational Expressions The if-else Statement The if-else Statement Nested if Statements.
PAGES:51-59 SECTION: CONTROL1 : DECISIONS Decisions.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Conditional Statement Chapter 8. Conditional Statements Are statements that check an expression then may or may not execute a statement or group of statement.
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 1 st semester King Saud University College of Applied studies and Community Service Csc 1101.
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.
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Choices and Decisions if statement if-else statement Relational.
Copyright 2003 Scott/Jones Publishing Making Decisions.
Control structures Algorithm Development Conditional Expressions Selection Statements 1.
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.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Introduction to branching.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Chapter Making Decisions 4. Relational Operators 4.1.
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
If Statements Programming. COMP104 Lecture 7 / Slide 2 Review: Rules for Division l C++ treats integers different than doubles. 100 is an int. l 100.0,
Control Flow Statements
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.
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 2 nd semester King Saud University College of Applied studies and Community Service Csc 1101.
Chapter 5 – Decision Making. Structured Programming Sequence Selection Repetition yesno yes no.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
A First Book of C++ Chapter 4 Selection.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Branching statements.
If/Else Statements.
Java Programming Fifth Edition
Chapter 4: Making Decisions.
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.
Decisions Chapter 4.
Chapter 4: Making Decisions.
The Selection Structure
Engineering Problem Solving with C++, Etter/Ingber
Chapter 4: Making Decisions.
Control Structures.
IF if (condition) { Process… }
Control Structures: Selection Statement
Pages:51-59 Section: Control1 : decisions
Repetition Control Structure
Chapter 4: Control Structures I (Selection)
Program Flow.
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Java Statements B.Ramamurthy CS114A, CS504 4/23/2019 BR.
Pages:51-59 Section: Control1 : decisions
Presentation transcript:

Control Structures: Selection Statement Chapter 3 B. Ramamurthy 5/3/2019 B.Ramamurthy

Introduction “Two roads divulged in a yellow wood, And sorry I could not travel both….” Robert Frost From “The Road Not Taken” 5/3/2019 B.Ramamurthy

Structured Programming Sequence Selection Repetition no yes yes no 5/3/2019 B.Ramamurthy

Decision Statements (selection) How to compare data values? Relational operators How to alter the sequence of program execution based on the result? if.. else statements How to deal with multiple choices? Switch statement 5/3/2019 B.Ramamurthy

Example We will use “assigning letter grade based on percentage points” as an example to illustrate selection statement. Percent Range >=90 >=80 < 90 >=70 <80 >=60 <70 <50 Letter Grade A B C D F 5/3/2019 B.Ramamurthy

Relational Operators Operator Meaning < Less than ? > Greater than ? >= Greater than or equal to? <= Less than or equal to? == Equal to? != Not Equal to ?

Logical Operators ! not && and || or These operators are used to combine more than one condition forming a complex condition. 5/3/2019 B.Ramamurthy

if Statement An if statement allows a program to choose whether or not to execute a following statement. Syntax: if (condition) statement; Semantics: condition is a Boolean expression: Something that evaluates to True or False. If condition is true then execute the statement is executed. 5/3/2019 B.Ramamurthy

The if statement : syntax if(expression) statement; //single statement executed //if expression is true if(expression) { //statements inside {} are //executed if expression is true statement1; statement2; … statement n; } 5/3/2019 B.Ramamurthy

If -else Statement An if-else statement allows a program to do one thing if a condition is true and a different thing if the condition is false. Syntax: if ( condition ) statement1 else statement2 Statements to be executed for if and else can be a single statement or multiple statements enclosed in { }. 5/3/2019 B.Ramamurthy

The if - else statement: syntax if(expression) statement; else if(expression) { statement block } else 5/3/2019 B.Ramamurthy

The switch statement switch(expression) { case constant: statement(s); break; /* default is optional*/ default: } 5/3/2019 B.Ramamurthy

The switch statement Expression must be of type integer or character The keyword case must be followed by a constant break statement is required unless you want all subsequent statements to be executed. 5/3/2019 B.Ramamurthy

Practice! Convert these nested if/else statements to a switch statement: if (rank==1 || rank==2) cout << "Lower division \n"; else { if (rank==3 || rank==4) cout << "Upper division \n";    { if (rank==5) cout << "Graduate student \n"; cout << "Invalid rank \n"; } 5/3/2019 B.Ramamurthy

Practice Solution! switch(rank) { case 1: case 2: cout << "Lower division \n"; break; case 3: case 4: cout << "Upper division \n"; case 5: cout << "Graduate student \n"; default: cout << "Invalid rank \n"; }//end switch 5/3/2019 B.Ramamurthy

Summary In many applications, choices are to be made depending on some conditions related to the problem. Selection or decision structures are used to model such situations. C++ supports the implementation of “selection” through the “if” and “switch” statements. In this discussion we looked at various forms of selection statements. 5/3/2019 B.Ramamurthy