James Tam Making Decisions In Pascal In this section of notes you will learn how to have your Pascal programs choose between alternative courses of action.

Slides:



Advertisements
Similar presentations
Pascal Programming Today Chapter 4 1 »Conditional statements allow the execution of one of a number of possible operations. »Conditional statements include:
Advertisements

Control Structures Control structures are used to manage the order in which statements in computer programs will be executed Three different approaches.
James Tam Making Decisions In Pascal In this section of notes you will learn how to have your Pascal programs choose between alternative courses of action.
James Tam Repetition In Pascal In this section of notes you will learn how to have a section of code repeated without duplicating the code.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
James Tam Making Decisions In Python In this section of notes you will learn how to have your Pascal programs choose between alternative courses of action.
James Tam Making Decisions In Pascal In this section of notes you will learn how to have your Pascal programs choose between alternative courses of action.
James Tam Making Decisions In Pascal In this section of notes you will learn how to have your Pascal programs choose between alternative courses of action.
James Tam Making Decisions In Pascal In this section of notes you will learn how to have your Pascal programs choose between alternative courses of action.
James Tam Repetition In Pascal In this section of notes you will learn how to rerun parts of your program without having to duplicate the code.
Val Function A Function performs an action and returns a value The expression to operate upon, known as the argument, (or multiple arguments), must be.
J. Michael Moore Structured Programming CPSC 110 Drawn from James Tam's material.
J. Michael Moore Other Control Structures CSCE 110 Influenced by material developed by James Tam & Jennifer Welch.
Decisions (Conditional Programming) Chapter 5 (Sec. 5.1 & 5.2)
James Tam Loops In Pascal In this section of notes you will learn how to rerun parts of your program without having to duplicate the code.
Basic Elements of Programming A VB program is built from statements, statements from expressions, expressions from operators and operands, and operands.
James Tam Getting Started With Pascal Programming What is the basic structure of a Pascal Program Variables in Pascal Performing input and output with.
2-1 Making Decisions Sample assignment statements PayAmount = Hours * Rate PayAmount = 40*Rate + (Hours – 40)*Rate*1.5.
J. Michael Moore Structured Programming CSCE 110 Drawn from James Tam's material.
James Tam Loops In Pascal In this section of notes you will learn how to rerun parts of your program without having to duplicate the code.
J. Michael Moore Logic and Boolean Expressions CSCE 110.
true (any other value but zero) false (zero) expression Statement 2
James Tam Loops In Pascal In this section of notes you will learn how to rerun parts of your program without having to duplicate your code.
School of Computing Science CMT1000 Ed Currie © Middlesex University 1 CMT1000: Introduction to Programming Ed Currie Lecture 5B: Branch Statements - Making.
Programming: Part II In this section of notes you will learn more advanced programming concepts such as branching and repetition as well as how to work.
We will not cover the following sections in class: –7.2 –7.4 –7.8(we will discuss the design topics) You ARE responsible for knowing the topics covered.
J. Michael Moore Other Control Structures CSCE 110 Influenced by material developed by James Tam & Jennifer Welch.
James Tam Loops In Pascal In this section of notes you will learn how to rerun parts of your program without having to duplicate the code.
James Tam Making Decisions In Pascal In this section of notes you will learn how to have your Pascal programs choose between alternative courses of action.
Making Decisions In Python
A High-Level Model For Software Development
Chapter 4: Control Structures: Selection
1 Selection Structures. 2 Making Decisions Sample assignment statements to figure worker pay with possible overtime PayAmount = Hours * Rate PayAmount.
James Tam Making Decisions In Pascal In this section of notes you will learn how to have your Pascal programs choose between alternative courses of action.
Chapter 3 Making Decisions
CPS120: Introduction to Computer Science Decision Making in Programs.
Chapter 5 – Decisions 5.1 Relational and Logical Operators 5.2 If Blocks 5.3 Select Case Blocks.
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
Programming Logic and Design, Second Edition, Comprehensive
Rational Expressions and selection structures Relational operators Logical operators Selection structures.
Selection Boolean What is Boolean ? Boolean is a set with only two values : –true –false true and false are standard identifiers in Pascal, called Boolean.
Chapter 5 Logic; Got Any?. Flow of Control The order in which the computer executes statements in a program Control Structure A statement used to alter.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
Selection Relational Expressions A condition or logical expression is an expression that can only take the values true or false. A.
Selection-making Decisions Selection allows you to choose between two or more possible program flow --- it lets you make decisions in your program. Examples.
James Tam Making Decisions In Python In this section of notes you will learn how to have your programs choose between alternative courses of action.
Chapter 4 Making Decision Csc 125 C++ programming language Fall 2005.
CPS120: Introduction to Computer Science Decision Making in Programs.
Pascal Programming Making decisions - Selection Statements National Certificate Unit 4 Carl Smith.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 6, Lecture 1 (Monday)
Midterm1 answers: Page 1 1 a6 b6 c0 2 ***. Midterm1 answers: Page 2 A)a. 53b. 44c. 13d. 75e. 77 B)a. 53b. 17c. 33 / 44d. 13e. 75 C)a. ‘5’b. ‘3’c. ‘ ‘d.
James Tam Making Decisions In Python In this section of notes you will learn how to have your programs choose between alternative courses of action.
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.
Pascal Programming George Boole, a 19 th Century mathematician, is created with true, false logic. A Boolean expression in Pascal will be true or false.
Conditional or Decision Logic
Operators and Expressions
Visual Basic 6 (VB6) Data Types, And Operators
Chapter 4: Making Decisions.
Programming Fundamentals
Boolean Expressions and If statements
2-1 Making Decisions Sample assignment statements
Objectives After studying this chapter, you should be able to:
Microsoft Visual Basic 2005: Reloaded Second Edition
SELECTIONS STATEMENTS
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Computer Programming Basics
Decision Making Using the IF and EVALUATE Statements
Control Structures.
Presentation transcript:

James Tam Making Decisions In Pascal In this section of notes you will learn how to have your Pascal programs choose between alternative courses of action

James Tam Decision-Making In Pascal Decisions are questions with answers that are either true or false (Boolean) Decision making constructs in Pascal If-then If-then-else If, else-if Case-of

James Tam If-Then Decision-making with one condition Format: If (operand 1 relational operation operand 1 ) then body; 2 additional statements; Example: if (age >= 18) then writeln('You are an adult'); writeln('Tell me more about yourself'); 1 Operands are referred to as expressions in Leestma and Nyhoff 2 The body of the if-then is referred to as a statement in Leestma and Nyhoff Boolean expression Indicates end of decision-making

James Tam If-Then (Flowchart) Boolean expression Body T F Additional statements

James Tam If-Then (Simple Body) Body of if-then consists of a single statement Format: if (Boolean Expression) then s1; s2; Example ( for full example look under /home/231/examples/decisions/simpleIfThen.p ): if (x = 1) then writeln('Body of if'); writeln ('After body'); Indicates end of decision-making Body

James Tam If-Then (Compound Body) Body of if-then consists of multiple statements Format: if (Boolean Expression) then begin s1; s2; : sn; end; sn+1; Indicates end of decision-making Body

James Tam If-Then (Compound Body(2)) Example ( for full example look under /home/231/examples/decisions/compoundIfThen.p ): if (x = 1) then begin writeln('Body of if 1'); writeln('Body of if 2'); end; writeln('after if');

James Tam If-Then-Else Decision-making with two conditions One (and only one) condition will be true Format: if (operand 1 relational operation operand 1 ) then body of 'if' else body of 'else'; additional statements; No semi-colon (indicates end of decision making!) Boolean expression

James Tam If-Then-Else Example: if (age >= 18) then writeln('You are an adult') else writeln('You are not an adult'); writeln('Tell me more about yourself');

James Tam If-Then-Else (Flowchart) Boolean expression Body of 'else' T F Additional statements Body of 'if'

James Tam If-Then-Else (Simple Body) Body of if-then-else consists of a single statement Format: if (Boolean Expression) then s1 else s2; s3; No semi-colon (indicates end of decision- making!)

James Tam If-Then-Else (Simple Body(2)) Example ( for full example look under /home/231/examples/decisions/simpleIfThenElse.p ): if (x = 1) then writeln('body of if') else writeln('body of else'); writeln('after if-then-else');

James Tam If-Then-Else (Compound Body) Body of if-then-else consists of multiple statements Format: if (Boolean Expression) then begin s1; : sn; end else begin sn+1; : sn + m; end; Sn + m + 1; No semi-colon (marks end of decision-making!)

James Tam If-Then (Compound Body(2)) Example ( for full example look under /home/231/examples/decisions/compoundIfThenElse.p ): if (x = 1) then begin writeln('Body of if 1'); writeln('Body of if 2'); end else begin writeln('Body of else 1'); writeln('Body of else 2'); end; writeln('after if-then-else');

James Tam Allowable Operands For Boolean Expressions If (operand relational operator operand) then Operands integer real boolean char const

James Tam Allowable Relational Operators For Boolean Expressions If (operand relational operator operand) then Pascal Mathematical operator equivalent Meaning < < Less than > > Greater than = = Equal to <= ≤ Less than or equal to >= ≥ Greater than or equal to <> ≠ Not equal to

James Tam Decision-Making With Multiple Expressions Typical format: if (Boolean expression) Boolean operator (Boolean expression) body Example: if (x > 0) AND (y > 0) then writeln ('X is positive, Y is positive');

James Tam Decision-Making With Multiple Expressions (2) Built-in Boolean operators in Pascal NOT AND OR XOR (NAND and NOR can be constructed via NOT, AND & OR)

James Tam Forming Compound Boolean Expressions With NOT Format if NOT (Boolean Expressions) body; Example if NOT (x AND y) if NOT (x OR y) For a complete example program look in Unix under /home/231/examples/decisions/compoundNOT.p

James Tam Forming Compound Boolean Expressions With OR Format if (Boolean Expression) OR (Boolean Expression) body; Example if (gpa > 3.7) OR (yearsJobExperience > 5) writeln(‘You are hired’); For a complete example program look in Unix under /home/231/examples/decisions/compoundOR.p

James Tam Forming Compound Boolean Expressions With AND Format if (Boolean Expression) AND (Boolean Expression) body; Example if (yearsOnJob <= 2) AND (isGoofOff = True) writeln(‘You are fired’); For a complete example program look in Unix under /home/231/examples/decisions/compoundAND.p

James Tam Forming Compound Boolean Expressions With XOR Format if (Boolean Expression) XOR (Boolean Expression) body; Example if (takesFirstJob = True) XOR (takesSecondJob = True) isEmployed := true;

James Tam Order Of Operation Order Operator 1 NOT 2 * / DIV MOD AND OR 4 = = <>

James Tam Why Bracket Boolean Expressions Compound Boolean Expressions e.g., if x > 0 AND y > 0

James Tam Why Bracket Boolean Expressions Compound Boolean Expressions e.g., if x > 0 AND y > 0 AND has highest priority so the 0 and y becomes operands for this operation

James Tam Nested Decision Making One decision is made inside another Outer decisions must evaluate to true before inner decisions are even considered Format if (Boolean expression) then inner body Example (For complete example look in Unix under /home/231/examples/decisions/nesting.p) if (num1 > 0) then if (num2 > 0) then writeln('Both numbers are positive'); Outer body Inner body

James Tam Nested Decision Making: The Dangling Else if (x > 0) then if (y >0) then writeln(‘x is greater than zero, y is greater than zero’) else writeln(‘x is greater than zero’);

James Tam The Dangling Else Reformatted if (x > 0) then if (y > 0) then writeln('x and y greater than zero') else writeln('x greater than zero');

James Tam Decision-Making With Multiple Alternatives if-then Checks one condition if-then-else Checks for one of two mutually exclusive conditions Approaches for multiple alternatives Multiple if's Multiple else-if's

James Tam Multiple If's: Non-Exclusive Conditions Any, all or none of the conditions may be true Format: if (Boolean expression 1) then body 1; if (Boolean expression 2) then body 2; : statements after the conditions;

James Tam Multiple If's:Flowchart Expression 1 Expression 2 Body 1 T F Expression n : Body 2 T F Body n T F Statements after conditions

James Tam Multiple If's: Non-Exclusive Conditions (Example) if (x > 0) then writeln('X is positive); if (y > 0) then writeln('Y is positive'); If (z > 0) then writeln('Z is positive's);

James Tam Multiple If's: Mutually Exclusive Conditions At most only one of many conditions can be true Can be implemented through multiple if's Example (for full example look in Unix under /home/231/examples/decisions/inefficientDecisionMaking.p) if (gpa = 4) then letter := 'A'; if (gpa = 3) then letter := 'B'; if (gpa = 2) then letter := 'C'; if (gpa = 1) then letter := 'D'; if (gpa = 0) then letter := 'F'; Inefficient combination!

James Tam Multiple If, Else-If's: Mutually Exclusive Conditions Format: if (Boolean expression 1) then body 1 else if (Boolean expression 2) then body 2 : else body n; statements after the conditions;

James Tam Multiple If, Else-If's: Flowchart Expression 1 Expression 2 Body 1 T F Expression n : Body 2 T F Body n T F Statements after conditions

James Tam Multiple If, Else-If's: Mutually Exclusive Conditions (Example) Example (for full example look in Unix under /home/231/examples/decisions/ifElseIf.p) if (gpa = 4) then letter := 'A' else if (gpa = 3) then letter := 'B' else if (gpa = 2) then letter := 'C' else if (gpa = 1) then letter := 'D' else if (gpa = 0) then letter := 'F' else writeln('GPA must be one of 4, 3, 2, 1 or 0'); Watch your semi-colons!

James Tam Case Statements An alternative to the if, else-if (at most only one of many conditions can be true) Format (integer): Case (expression) of i 1 : body; i 2 : body; : i n : body; otherwise: body; end; (* case *) Expression (variable, constant, arithmetic) must evaluate to an integer

James Tam Case Statements: Integer Example Example (look for complete example in Unix under /home/231/examples/decisions/caseOf1.p): case (gpa) of 4: writeln(‘You got an A’); 3: writeln(‘You got a ‘B’); 2: writeln(‘You got a C’); 1: writeln(‘You got a D’); 0: writeln(‘You got an F’);

James Tam Case Statements: Integer Example (2) otherwise: writeln('GPA must be one of 4, 3, 2, 1 or 0'); end; (* case *)

James Tam Case Statements: Characters Format (char): Case (expression) of ‘c 1 ’: body; ‘c 2 ’: body; : ‘c n ’: body; otherwise body; end; (* case *) Expression (variable, constant, arithmetic) must evaluate to a character

James Tam Case Statements: Character Example Example (look for complete example in Unix under /home/231/examples/decisions/caseOf2.p): case (letter) of ‘A’: writeln(‘GPA = 4’); ‘B’: writeln(‘GPA = 3’); ‘C’: writeln(‘GPA = 2’); ‘D’: writeln(‘GPA = 1’); ‘F’: writeln(‘GPA = 0’);

James Tam Case Statements: Character Example (2) otherwise: writeln('Letter grade must be one of an "A", "B", "C", "D" or "F"'); end; (* case *)

James Tam Summary How is decision making implemented via Pascal constructs: If-then If-then-else Case-of What are Boolean expressions and what are valid operators and operands? How to handle simple vs. multiple statements in the body of a decision-making statement. What are compound Boolean expressions? How does nested decision making work? Exclusive vs. non-exclusive alternatives when making decisions.