Making Decisions In Pascal

Similar documents
Making Decisions In Pascal

Making Decisions In Pascal In this section of notes you will learn how to have your Pascal programs to execute alternatives

Making Decisions In Python

Programming: Part I. In this section of notes you will learn about how to write simple programs using JES. Translators

Getting Started With Pascal Programming

Making Decisions In Python

Getting Started With Pascal Programming

Decision Structures. Selection. Selection options (in Java) Plain if s (3 variations) Each action could be any of: if else (3 variations)

In Delphi script, when values are assigned to variables, the colon-equal operator is used; :=

Introduction To Files In Pascal

Introduction To Files In Pascal

JavaScript. Backup Your Work Frequently!

Decision Making in C

Lecture 5 Tao Wang 1

Ch. 7: Control Structures

Flow of Control. Flow of control The order in which statements are executed. Transfer of control

V2 2/4/ Ch Programming in C. Flow of Control. Flow of Control. Flow of control The order in which statements are executed

Prof. Navrati Saxena TA: Rochak Sachan

A/L 2011_revision. PASCAL PROGRAMMING

DECISION STRUCTURES: USING IF STATEMENTS IN JAVA

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

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;

Microsoft Visual Basic 2005: Reloaded

5. Selection: If and Switch Controls

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

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

Pointers. In this section of notes you will learn about another type of variable that stores addresses rather than data. Memory: What You Know

Relational and Logical Statements

The PCAT Programming Language Reference Manual

Getting Started With Pascal Programming

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

1 Programming. 1.1 Analyzing a problem

CT 229 Java Syntax Continued

A control expression must evaluate to a value that can be interpreted as true or false.

Syntax and Variables

Chapter 8 Statement-Level Control Structure

QUIZ: What value is stored in a after this

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

5. Control Statements

Multi-Dimensional Arrays In Pascal

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

The Arithmetic Operators

Control Structure: Selection

Following is the general form of a typical decision making structure found in most of the programming languages:

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

Arithmetic and Bitwise Operations on Binary Data

BASIC ELEMENTS OF A COMPUTER PROGRAM

STUDENT LESSON A12 Iterations

C Program. Output. Hi everyone. #include <stdio.h> main () { printf ( Hi everyone\n ); }

Computational Physics - Fortran February 1997

Topics. Chapter 5. Equality Operators

520 Principles of Programming Languages. Arithmetic. Variable Declarations. 19: Pascal

Operators. Java operators are classified into three categories:

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

9691 COMPUTING. 9691/23 Paper 2 (Written Paper), maximum raw mark 75

ESCI 386 IDL Programming for Advanced Earth Science Applications Lesson 1 IDL Operators

Nested Selections. Chapter 8

JAVA OPERATORS GENERAL

Quick Reference Guide

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

WEEK 4 OPERATORS, EXPRESSIONS AND STATEMENTS

SML A F unctional Functional Language Language Lecture 19

Chapter 4: Making Decisions

Chapter 2: Overview of C++

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

PDS Lab Section 16 Autumn Tutorial 3. C Programming Constructs

Decision Making -Branching. Class Incharge: S. Sasirekha

Annex A (Informative) Collected syntax The nonterminal symbols pointer-type, program, signed-number, simple-type, special-symbol, and structured-type

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

arrays Simple Types (Atomic) In this section of notes you will be introduced to a homogeneous composite type, onedimensional 1) Integer 2) Real

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

SECTION II: LANGUAGE BASICS

Chapter 4: Making Decisions

CP FAQS Q-1) Define flowchart and explain Various symbols of flowchart Q-2) Explain basic structure of c language Documentation section :

Computer Science & Engineering 150A Problem Solving Using Computers

Fundamental of Programming (C)

Object Oriented Programming with Java

Case by Case. Chapter 3

Introduction to Computer Science Midterm 3 Fall, Points

Object Oriented Programming Lecture (2.1) Supervisor Ebtsam AbdelHakam Department of Computer Science Najran University

Constants and Variables

Informatics Ingeniería en Electrónica y Automática Industrial. Control flow

Concepts Review. 2. A program is the implementation of an algorithm in a particular computer language, like C and C++.

Control Structures: if and while A C S L E C T U R E 4

Input parameters. Function. An ideal black-box representation of a function

conditional statements

Test #2 October 8, 2015

Boolean Expressions (Conditions)

Exercise 1 Using Boolean variables, incorporating JavaScript code into your HTML webpage and using the document object

Language Reference Manual

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

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

EDIABAS BEST/2 LANGUAGE DESCRIPTION. VERSION 6b. Electronic Diagnostic Basic System EDIABAS - BEST/2 LANGUAGE DESCRIPTION

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.

GO - OPERATORS. This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by one.

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

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

Pseudo-code vs. Assembly. Introduction to High-Level Language Programming. Disadvantages of Assembly. Pseudo-code vs. High-level Programs

Transcription:

Making Decisions In Pascal In this section of notes you will learn how to have your Pascal programs choose between alternative courses of action High Level View Of Decision Making For The Computer??? True False You win! Game over man

Decision-Making In Pascal Decisions are questions with answers that are either true or false (Boolean) Decision making constructs (mechanisms) in Pascal If-then If-then- If, -if Case-of If-Then Decision-making: checking if a particular condition is true If (operand 1 relational operator operand 1 ) then 2 additional statements; Example: Boolean expression Indicates end of decisionmaking 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

Allowable Operands For Boolean Expressions If (operand relational operator operand) then Operands integer real boolean char const 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

If-Then (Simple Body) Body of if-then consists of a single statement if (Boolean expression) then s1; Body s2; Example: if (x = 1) then Indicates end of decisionmaking writeln('body of if'); writeln ('After body'); If-Then (Compound Body) Body of if-then consists of multiple statements if (Boolean expression) then begin s1; s2; : Body sn; end; sn+1; Indicates end of decisionmaking

If-Then (Compound Body(2)) Example: if (x = 1) then begin writeln('body of if 1'); writeln('body of if 2'); end; writeln('after if'); If-Then-Else Decision-making with two conditions (true or false) if (operand relational operator operand) then body of 'if' body of ''; additional statements; No semi-colon (indicates end of decision making!) Semi-colon (decision making is complete)

If-Then-Else Example: if (age >= 18) then writeln( Adult') writeln( Not an adult'); writeln('tell me more about yourself'); If-Then-Else (Simple Body) Body of if-then- consists of a single statement if (Boolean expression) then s1 s2; s3; No semi-colon (indicates end of decisionmaking!) Semi-colon (this is the end of the decision-making process!)

If-Then-Else (Simple Body(2)) Example: if (x = 1) then writeln('body of if') writeln('body of '); writeln('after if-then-'); If-Then-Else (Compound Body) Body of if-then- consists of multiple statements if (Boolean expression) then begin s1; : sn; end begin sn+1; : sn + m; end; sn + m + 1; No semi-colon (marks end of decision-making!) Semi-colon (this is the end of the decision-making process!)

If-Then (Compound Body(2)) Example: if (x = 1) then begin writeln('body of if 1'); writeln('body of if 2'); end begin writeln('body of 1'); writeln('body of 2'); end; writeln('after if-then-'); Decision-Making With Multiple Expressions if (Boolean expression) logical operator (Boolean expression) then Example: if (x > 0) AND (y > 0) then writeln ('X is positive, Y is positive');

Decision-Making With Multiple Expressions (2) Built-in logical operators in Pascal AND OR XOR NOT (NAND and NOR can be constructed by combining NOT with AND & NOT with OR) Forming Compound Boolean Expressions With The OR Operator if (Boolean expression) OR (Boolean expression) then Example: if (gpa > 3.7) OR (yearsjobexperience > 5) then writeln( You are hired );

Forming Compound Boolean Expressions With The AND Operator if (Boolean expression) AND (Boolean expression) then Example: if (yearsonjob <= 2) AND (isgoofoff = True) then writeln( You are fired ); Forming Compound Boolean Expressions With The XOR Operator if (Boolean expression) XOR (Boolean expression) then Example: if (takesfirstjob = true) XOR (takessecondjob = true) then isemployed := true;

Forming Compound Boolean Expressions With The NOT Operator if NOT (Boolean expression) then Examples: if NOT (x AND y) then writeln( NAND ); if NOT (x OR y) then writeln( NOR ); Order Of The Operations Order Operator 1 NOT 2 * / DIV MOD AND 3 + - OR 4 < > = <= >= <>

Why Bracket Boolean Expressions Compound Boolean expressions e.g., if x > 0 AND y > 0 then 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 becomes operands for this operation

One decision is made inside another Nested Decision Making Outer decisions must evaluate to true before inner decisions are even considered if (Boolean expression) then if (Boolean expression) then inner body Outer body Inner body Example: if (num1 > 0) then if (num2 > 0) then writeln('both numbers are positive'); 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 ) writeln( x is greater than zero );

The Dangling Else Reformatted if (x > 0) then if (y > 0) then writeln('x and y greater than zero') writeln('x greater than zero'); if-then Decision-Making With Multiple Alternatives Checks a condition and executes the body of code if the condition is true if-then- 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 alternatives Multiple if's Multiple -if's

Multiple If's: Non-Exclusive Conditions Any, all or none of the conditions may be true (independent) if (Boolean expression 1) then body 1; if (Boolean expression 2) then body 2; : statements after the conditions; 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 );

Multiple If's: Mutually Exclusive Conditions At most only one of many conditions can be true Can be implemented through multiple if's Inefficient combination! 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'; Multiple If, Else-If's: Mutually Exclusive Conditions if (Boolean expression 1) then body 1 if (Boolean expression 2) then body 2 : body n; statements after the conditions;

Multiple If, Else-If's: Mutually Exclusive Conditions (Example) 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' writeln('gpa must be one of 4, 3, 2, 1 or 0'); Watch your semi-colons! Case Statements An alternative to the if, -if (at most only one of many conditions can be true) Format (integer): case (expression) of i 1 : i 2 : : i n : end; (* case *) The expression (variable, constant, arithmetic) must evaluate to an integer

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 ); Case Statements: Integer Example (2) writeln('gpa must be one of 4, 3, 2, 1 or 0'); end; (* case *)

Format (char): Case (expression) of c 1 : c 2 : : c n : end; (* case *) Case Statements: Characters The expression (variable, constant, arithmetic) must evaluate to a character 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 );

Case Statements: Character Example (2) writeln('letter grade must be one of an "A", "B", "C", "D" or "F"'); end; (* case *) 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

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) writeln('num is negative: ', num); end. Avoid Using Real Values When An Integer Will Do program testexample; begin var num : real; num := 1.03-0.42; if (num = 0.61) then writeln('sixty one cents') writeln('not sixty one cents'); end.

You Should Now Know What are the four decision making constructs available in Pascal: If-then If-then- If, -if Case-of How does each one work When should each one be used How to evaluate 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 decision making constructs 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