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
Selection (decision) control structure Learning objective
Advertisements

Pascal Programming Today Chapter 4 1 »Conditional statements allow the execution of one of a number of possible operations. »Conditional statements include:
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 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 Repetition 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 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.
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.
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
James Tam Getting Started With Pascal Programming What is the basic structure of a Pascal Program Variables in Pascal Performing input and output with.
James Tam Programming: Part I In this section of notes you will learn about how to write simple programs using JES.
J. Michael Moore Structured Programming CSCE 110 Drawn from James Tam's material.
10 November JavaScript. Presentation Hints What do YOU think makes a good presentation Some of my suggestions Don’t write full sentences on slides Talk,
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.
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
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.
C++ for Engineers and Scientists Third Edition
Visual C++ Programming: Concepts and Projects
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.
CONTROL STATEMENTS IF-ELSE, SWITCH- CASE Introduction to Computer Science I - COMP 1005, 1405 Instructor : Behnam Hajian
Chapter 3 Making Decisions
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
CPS120: Introduction to Computer Science Decision Making in Programs.
2 Objectives You should be able to describe: Relational Expressions Relational Expressions The if-else Statement The if-else Statement Nested if Statements.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
1 2. Program Construction in Java. 2.4 Selection (decisions)
Conditional Structures UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
BACS 287 Programming Logic 2. BACS 287 Sequence Construct The sequence construct is the default execution mode for the CPU. The instructions are executed.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 5, Lecture 1 (Monday)
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.
COMPUTER PROGRAMMING I 5.04 Apply Decision Making Structures.
CPS120: Introduction to Computer Science Decision Making in Programs.
An Object-Oriented Approach to Programming Logic and Design Chapter 5 Making Decisions.
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.
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.
Chapter 5 – Decision Making. Structured Programming Sequence Selection Repetition yesno yes no.
Chapter 4 Select … Case Multiple-Selection Statement & Logical Operators 1 © by Pearson Education, Inc. All Rights Reserved. -Edited By Maysoon.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand how decisions are made in a computer ❏ To understand the logical.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
Operators and Expressions
Loops In Pascal In this section of notes you will learn how to rerun parts of your program without having to duplicate the code. Repetition in Pascal:
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.
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Chapter 2 Programming Basics.
Simple Branches and Loops
Computer Programming Basics
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Decision Making Using the IF and EVALUATE Statements
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
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 High Level View Of Decision Making For The Computer Is income below $10,000? True Nominal income deduction Eligible for social assistance False Is income between $10K - $20K? True Income tax = 20% False etc.

James Tam Decision-Making In Pascal Decisions are questions with answers that are either true or false (Boolean) e.g., Is it true that the variable ‘x’ is positive? The program branches one way or another depending upon the answer to the question. Decision making/branching constructs (mechanisms) in Pascal If-then If-then-else If, else-if Case-of

James Tam If-Then Question? Execute a statement or statements True False Remainder of the program

James Tam If-Then Decision-making: checking if a particular condition is true Format: if (operand 1 relational operator 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 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 Body of if-then consists of a single statement Format: if (Boolean expression) then s1; s2; Example: if (x = 1) then writeln('Body of if'); writeln ('After body'); If-Then (Simple Body) Indicates end of decision- making Body

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

James Tam If-Then (Compound Body(2)) Example: taxRate := 0.2; if (income < 10000) then begin writeln(‘Eligable for social assistance’); taxCredit = 100; end; tax = income * taxRate;

James Tam If-Then: Determining What Is The Body Recall: The body of the if-then is what gets executed if the Boolean expression evaluates to true. Single statement body: what follows the ‘then’ and precedes the first semi-colon. Compound body with multiple statements: what is enclosed within the begin-end pair.

James Tam If-Then-Else Question? Execute a statement or statements True False Execute a statement or statements Remainder of the program

James Tam Decision-making with two conditions (true or false) Format: if (operand relational operator operand) then body of 'if' else body of 'else'; additional statements; If-Then-Else No semi-colon (indicates end of decision making!) Semi-colon (decision making is complete)

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

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!) Semi-colon (this is the end of the decision-making process!)

James Tam If-Then-Else (Simple Body(2)) Example: 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 (not the end of decision-making process!) Semi-colon (this is the end of the decision-making process!)

James Tam If-Then-Else (Compound Body(2)) Example: if (income < 10000) then begin writeln(‘Eligible for social assistance’); taxRate = 0.1; end else begin writeln(‘Not eligible for social assistance’); taxRate = 0.2; end; tax := income * taxRate;

James Tam Quick Summary: If Vs. If-Else If: Evaluate a Boolean expression (ask a question) If the expression evaluates to true then execute the ‘body’ of the if. No additional action is taken when the expression evaluates to false. Use when your program evaluates a Boolean expression and code will be executed only when the expression evaluates to true. If-else: Evaluate a Boolean expression (ask a question) If the expression evaluates to true then execute the ‘body’ of the if. If the expression evaluates to false then execute the ‘body’ of the else. Use when your program evaluates a Boolean expression and different code will execute if the expression evaluates to true than if the expression evaluates to false.

James Tam Decision-Making With Multiple Expressions Format: if (Boolean expression) logical operator (Boolean expression) then 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 logical operators in Pascal OR AND XOR NOT (NAND and NOR can be constructed by combining NOT with AND & NOT with OR)

James Tam Forming Compound Boolean Expressions With The “OR” Operator Format: if (Boolean expression) OR (Boolean expression) then body; Example: if (gpa > 3.7) OR (yearsJobExperience > 5) then writeln(‘You are hired’);

James Tam Forming Compound Boolean Expressions With The “AND” Operator Format: if (Boolean expression) AND (Boolean expression) then body; Example: if (yearsOnJob <= 2) AND (isGoofOff = true) then writeln(‘You are fired’);

James Tam Forming Compound Boolean Expressions With The “XOR” Operator Format: if (Boolean expression) XOR (Boolean expression) then body; Example: if (takesFirstJob = true) XOR (takesSecondJob = true) then isEmployed := true;

James Tam Forming Compound Boolean Expressions With The “NOT” Operator Format: if NOT (Boolean expression) then body; Examples: if NOT (x AND y) then writeln(‘NAND’); if NOT (x OR y) then writeln(‘NOR’);

James Tam Order Of The Operations 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 then

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

James Tam Quick Summary: Using Multiple Expressions Use multiple expressions when multiple questions must be asked and the result of each expression may have an effect on the other expressions: AND: All Boolean expressions must evaluate to true before the entire expression is true. If any expression is false then whole expression evaluates to false OR: If any Boolean expression evaluates to true then the entire expression evaluates to true. All Boolean expressions must evaluate to false before the entire expression is false.

James Tam Nested Decision Making One decision is inside another. Decision making is dependent. The first decision must evaluate to true before the successive decisions are even considered for evaluation. Questio n 1? True Questio n 2? True Statement or statements Remainder of the program False

James Tam Format: if (Boolean expression) then inner body Example: if (income < 10000) then if (citizen = true) then writeln(‘Eligable for social assistance'); tax = income * TAX_RATE; Outer body Nested Decision Making 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 a condition and executes the body of code if the condition is true if-then-else Checks a condition and executes one body of code if the condition is true and another body if the condition is false Approaches for multiple (two or more) alternatives Multiple if's Multiple else-if's

James Tam Decision Making With Multiple If’s Question ? True Statement or statements True Statement or statements Question ? Remainder of the program False

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

James Tam Multiple If's: Non-Exclusive Conditions (Example) 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’);

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 Decision Making With If, Else-If Question ? True Statement or statements False Question ? Remainder of the program Statement or statements False True Statement or statements

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: Mutually Exclusive Conditions (Example) Example: 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; else body; end; (* case *) The 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) else 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; else body; end; (* case *) The 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) else writeln('Letter grade must be one of an "A", "B", "C", "D" or "F"'); end; (* case *)

James Tam Recap: What Decision Making Constructs Are Available In Pascal/When To Use Them ConstructWhen To Use If-thenEvaluate a Boolean expression and execute some code (body) if it’s true If-then-elseEvaluate a Boolean expression and execute some code (first body) if it’s true, execute alternate code (second body) if it’s false Multiple if’sMultiple Boolean expressions need to be evaluated with the answer for each expression being independent of the answers for the others (non-exclusive). Separate code (bodies) can be executed for each expression. If, else-ifMultiple Boolean expressions need to be evaluated but zero or at most only one of them can be true (mutually exclusive). Zero bodies or exactly one body will execute. Case-ofSimilar to the ‘if, else-if’ but results in smaller (cleaner) programs but only works for specific situations (Boolean expressions that involve characters or integer values only).

James Tam Recap: When To Use Compound And Nested Decision Making Constructs ConstructWhen To Use Compound decision making More than one Boolean expression must be evaluated before some code (body) can execute. Nested decision making The outer Boolean expression must be true before the inner expression will even be evaluated.

James Tam Documenting Decision Making Constructs Each branch should be documented (or for simple programs the whole construct as a whole needs to be documented): (* Documentation for first branch *) if (BE1) then begin body 1 end (* Documentation for second branch *) else if (BE2) then begin body 2 end : else begin body n end;

James Tam Documenting Decision Making Constructs (2) Examples of things to document for branches: Under which conditions does each branch execute Conditions not handled Preconditions assumed for the branching construct.

James Tam Testing Decision Making Constructs Make sure that the body of each decision making construct executes when it should. Test: 1)Obvious true cases 2)Obvious false cases 3)Boundary cases

James Tam Testing Decisions: An Example program testDecisions (input, output); begin var num : integer; write('Enter a value for num: '); readln(num); if (num >= 0) then writeln('Num is non-negative: ', num) else writeln('Num is negative: ', num); end.

James Tam Avoid Using Real Values When An Integer Will Do program testExample; begin var num : real; num := ; if (num = 0.61) then writeln('Sixty one cents') else writeln('Not sixty one cents'); end.

James Tam Decision Making: A More Complex Problem Write a decision making construct that will determine the level of schooling for grade school child: Age = 5, level = kindergarten Age = 6 – 11, level = elementary school Age = 12 – 14, level = junior high school Age = 15 – 17, level = senior high school For ages lower than these values the program should indicate that the child is not yet of school age. For ages higher than these values the program should indicate that the person is no longer a child (of grade school age).

James Tam Solution Note: Don’t look at this solution ahead of time! program school (input, output); const KINDERGARTEN= 5; ELEMENTARY= 6; JUNIOR_HIGH= 12; SENIOR_HIGH= 15; ADULT= 18; begin var age : integer; write ('Enter the age of the child: '); readln (age);

James Tam Solution (2) if (age < KINDERGARTEN) then writeln ('Child is not of school age yet.') else if (age = KINDERGARTEN) then writeln ('Child should be in Kindergarten.') else if (age >= ELEMENTARY) AND (age < JUNIOR_HIGH) then writeln ('Child should be in Elementary School.') else if (age >= JUNIOR_HIGH) AND (age < SENIOR_HIGH) then writeln ('Child should be in Junior High School.') else if (age >= SENIOR_HIGH) AND (age < adult) then writeln ('Child should be in High School.') else if (age >= ADULT) then writeln ('This person is no longer a child.'); end.

James Tam You Should Now Know What are the four decision making constructs available in Pascal: If-then If-then-else If, else-if Case-of How does each one work When should each one be used How to evaluate and use decision making constructs: Tracing the execution of simple decision making constructs Where are semi-colons needed in decision making constructs and why are they needed How to evaluate nested and compound decision making constructs and when to use them

James Tam You Should Now Know (2) How the bodies of the decision making construct are defined: What is the body of decision making construct What is the difference between decision making constructs with simple bodies and those with compound bodies What is an operand What is a relational operator What is a Boolean expression How multiple expressions are evaluated and how the different logical operators work How to test decision making constructs