© Janice Regan, CMPT 128, Jan 2007 0 CMPT 128: Introduction to Computing Science for Engineering Students Introduction to branching.

Slides:



Advertisements
Similar presentations
Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Advertisements

© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Control Structures Nested ifs, switch statements.
Control Structures Control structures are used to manage the order in which statements in computer programs will be executed Three different approaches.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
1 9/24/07CS150 Introduction to Computer Science 1 Relational Operators and the If Statement.
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)
COMP 14 Introduction to Programming Miguel A. Otaduy May 18, 2004.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Conditional Statements Control Structures.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Logical and Relational Expressions Nested if statements.
1 Lecture 7:Control Structures I (Selection) Introduction to Computer Science Spring 2006.
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
1 Selection Structures. 2 Making Decisions Sample assignment statements to figure worker pay with possible overtime PayAmount = Hours * Rate PayAmount.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 30, 2005.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Decision Structures and Boolean Logic.
Decision Structures and Boolean Logic
Computer Science Selection Structures.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
CPS120: Introduction to Computer Science Decision Making in Programs.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
2 Objectives You should be able to describe: Relational Expressions Relational Expressions The if-else Statement The if-else Statement Nested if Statements.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
Rational Expressions and selection structures Relational operators Logical operators Selection structures.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 4 Decision.
Pseudocode When designing an ALGORITHM to solve a problem, Pseudocode, can be used. –Artificial, informal language used to develop algorithms –Similar.
CPS120: Introduction to Computer Science Decision Making in Programs.
VBScript Language. What is VBScript Based on the Visual Basic family of languages Supports object oriented features Interpreted Loosely Typed Implicitly.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 4: Control Structures I (Selection)
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
1 Compound Assignment C++ has a large set of operators for applying an operation to an object and then storing the result back into the object Examples.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 3 (2) & 4 September 8 & 10, 2009.
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.
Lesson - 5. Introduction While programming, we usually need to decide the path of the program flow according to the parameters and conditions. Actually.
Computer Science: A Structured Programming Approach Using C1 5-2 Two-Way Selection The decision is described to the computer as a conditional statement.
Computer Science: A Structured Programming Approach Using C1 5-2 Two-Way Selection The decision is described to the computer as a conditional statement.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
ICT Introduction to Programming Chapter 4 – Control Structures I.
Chapter 5: Control Structures I (Selection). Objectives In this chapter you will: Learn about control structures Examine relational and logical operators.
Computer Programming TCP1224 Chapter 5 The Selection Structure.
CPS120: Introduction to Computer Science Decision Making in Programs.
Decision Statements, Short- Circuit Evaluation, Errors.
CPS120: Introduction to Computer Science Decision Making in Programs.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
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)
CSC113: Computer Programming (Theory = 03, Lab = 01)
Decision Making in Code Logical Tests & Truth Logical Expressions
Topics The if Statement The if-else Statement Comparing Strings
Making Decisions in a Program
Topics The if Statement The if-else Statement Comparing Strings
Control Structures: Selection Statement
Chapter 4: Decision Structures and Boolean Logic
CMPT 102 Introduction to Scientific Computer Programming
Summary Two basic concepts: variables and assignments Basic types:
Computer Science Core Concepts
Chapter 4: Control Structures I (Selection)
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
Understanding Conditions
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Chapter 3: Selection Structures: Making Decisions
Control Structures: Selection Statement
Chapter 4: Decision Structures and Boolean Logic
Control Structures.
Presentation transcript:

© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Introduction to branching

© Janice Regan, CMPT 128, Control Structures  Control structures are used to manage the order in which statements in computer programs will be executed  Three different approaches  In sequence  Branching  Looping

© Janice Regan, CMPT 128, Control Structures  Branch: Altering the flow of program execution by making a selection or choice  Loop: Altering the flow of program execution by repetition of a particular block of statement(s)

Branching  One way selection  Two way selection  Multiple way selection © Janice Regan, CMPT 128,

4 Flowcharts  Flowcharts use some basic symbols  To start or end a function  To contain calculations  To make decisions  To connect different parts of an algorithm

© Janice Regan, CMPT 128, One-way selection  Simplest form of a branch.  Evaluate a condition that can be True or False  If the condition is true a series of actions are executed  If the condition is false that series of actions are not executed  C and C++ implementation:  if statement

© Janice Regan, CMPT 128, Flowchart: one way selection  Write the condition that needs to be satisfied in the decision box (diamond).  Based upon the value of the condition (boolean T or F) choose what to do next  The sequence of statements to be executed if the condition is true is placed in the box condition Statement 1; Statement n; T F ⋮ C and C++ implementation if statement

© Janice Regan, CMPT 128, One-way selection  Example if statement in C and C++: setFlagOrderBoxes = 0; if (numberOfBoxes < minimumBoxInventory) setFlagOrderBoxes = 1; setFlagOrderBags = 2; // always executed  setFlagOrderBags is always set to 2, even if the condition in the if statement is false

© Janice Regan, CMPT 128, C++ Compound/Block Statement  Only one statement following the if statement is part of the if control structure  What if we need more than one statement done if the condition is true?  Must use a block of statements (also called a compound statement)  C++ uses { }, to contain all the statements in a block of statements

© Janice Regan, CMPT 128, One way selection: sample  Do a series of actions only if a given condition holds  If the condition does not hold skip the actions if (myScore > yourScore) { cout << “My score was lower than yours “; difference = yourScore – myScore; cout << “I only got “ << difference << “ less points than you did” ; } cout << “Thanks for studying with me”;

© Janice Regan, CMPT 128, Two-way selection  Evaluate a condition that can be True or False  One “Set of things to do” if the condition is true,  A different “Set of things to do” if the condition is false  C and C++ implementation:  if-else statement

© Janice Regan, CMPT 128, Flowchart: two way selection  Based upon the value of the condition (boolean T or F) choose what to do next  The sequence of statements to be executed if the condition is true is placed in the box at the right  The sequence of statements to be executed if the condition is false is placed in the box below the condition condition Statement 1; Statement n; T F Statement 1; Statement n; ⋮ ⋮ Implemented in C and C++ as an if-else statement

© Janice Regan, CMPT 128, Example of two-way selection  Example if-else statement in C and C++: if (examScore > 50) myCourseGrade = “PASS”; else myCourseGrade = “FAIL”;  NOTE: Only one statement follows the if, and one statement follows the else

© Janice Regan, CMPT 128, Two way selection in C++  Complete one of two possible series of actions  First series of actions is complete if condition is true  Second series of actions is completed if condition is false if (condition) {//Series of actions to be taken when the condition is true action 1; ⋮ action n; } else { // Series of actions to be taken when the condition is false action 1; ⋮ action n; }

© Janice Regan, CMPT 128, Two way selection in C++  CODING STANDARD: always use {} to make blocks if (X<Y && Y<Z) //Never X<Y<Z { //Series of actions to be taken when the condition is true cout < < “Y is between X and Z” ; } else { // Series of actions to be taken when the condition is false cout<<“ Y is not between X and Z”; } cout << endl << “Y is “ << Y; cout << “ X is “ << X << “ Z Is “ << Z;

© Janice Regan, CMPT 128, Truth Table && The && (And) operator EXPRESSION1 EXPRESSION2 EXPRESSION1 && EXPRESSION2 T T T T F F F T F F F F

© Janice Regan, CMPT 128, Multiple-way selection  If condition1 is true the 1 st series of actions is completed  If condition1 is false and condition2 is true the 2 nd series of actions is completed  If condition1 and condition2 are false and condition3 is true the 3 rd series of actions is completed  …  If all conditions are false the final series of actions is completed  Implemented in C++ as an if-elseif-else statement

© Janice Regan, CMPT 128, Flowchart for multiple selection condition2 Statement 1; Statement n; T F Statement 1; Statement n; condition F Statement 1; Statement n; T ⋮ ⋮ ⋮

© Janice Regan, CMPT 128, Example of Multiple selection  Example: if (examScore > 80) myCourseGrade = “A”; else if (examScore > 70) myCourseGrade = “B ”; else myCourseGrade = “FAIL”;

© Janice Regan, CMPT 128, Example of Multiple selection  Example: always use { } to create blocks if ( X > 80 || Y < 100) { cout 80, Y any value) OR (X any value, Y<100)” ; } else if (X < 80) { cout = 100” ; } else { cout = 100”; }

© Janice Regan, CMPT 128, Truth Tables || The || (Inclusive Or) operator EXPRESSION1 EXPRESSION2 EXPRESSION1 || EXPRESSION2 T TT T FT F TT F F F

© Janice Regan, CMPT 128, Selection: decision statements Each decision statement contains a condition  The condition is an expression with a logical value (true or false)  The condition is a Boolean expression  A relational expression (a type of logical expression)  Another type of logical expression  A Boolean variable or constant

© Janice Regan, CMPT 128, Binary Relational Operators C, C++  <less than  <=less than or equal to  > greater than  >= greater than or equal to  == equal to  !=not equal to  Evaluated left to right Binary Equality Operators in C, C++

© Janice Regan, CMPT 128, Relational Expressions  A type of logical expression  Combines two numbers, strings, characters to give a value of true or false  A simple relational expression is  Two numerical values combined using a binary relational operator  A more complex relational expression is  A combination of simple relational expressions

© Janice Regan, CMPT 128, Other logical expressions Other types of logical expression include  Two logical variables / constants combined with a binary logical operator  One logical variable or constant  One logical variable or constant operated on by a unary logical operator  Two relational expressions combined with a binary logical operator  …  Any more complex logical expression

© Janice Regan, CMPT 128, Binary Logical Operators C, C++  &&Logical AND  ||Logical OR  !Not  Evaluated left to right  Arguments of logical operators have values of true or false Unary Logical Operators C, C++