J. Michael MooreStructured ProgrammingCPSC 110Drawn from James Tam's materialJ. Michael MooreProgramming Structure• ___________________• ___________________• ___________________J. Michael MooreSequence• Programs are executed at a time.• A compound statement a sequence of statements as a __________statement.J. Michael MooreCompound Statementbeginstatement1;statement2;..end;J. Michael MooreHigh Level View Of Decision Making For The ComputerIs income below $10,000?FalseIs income between $10K - $20K?TrueIncome tax = 20%Falseetc.True•Nominal income deduction•Eligible for social assistanceJ. Michael MooreDecision-Making In Pascal• Decisions are questions with answers that are either _____or ______ (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.J. Michael MooreIf-ThenQuestion?Execute a statementTrueFalseRemainder of the programJ. Michael MooreBoolean expression• Decision-making: checking if a particular condition is trueFormat:if (boolean-expression) thenstatement;additional statements;• Example:if (age >= 18) thenwriteln('You are an adult');writeln('Tell me more about yourself');If-ThenIndicates ________ ________________ _________ _________J. Michael MooreAllowable Operands For Boolean ExpressionsCan also stand alone as a Boolean expression (it is a boolean value)•integer•real•boolean•char•constOperands:Boolean-expression:operand relational-operator operandORboolean-valueJ. Michael MooreRelational Operators For Boolean ExpressionsIf (operand relational-operator operand) thenPascal Mathematical operator equivalent Meaning< < Less than> > Greater than= = Equal to<= ≤ Less than or equal to>= ≥ Greater than or equal to<> ≠ Not equal toJ. Michael MooreStatement / Body• Body of if-then consists of a single statement• Format:if (Boolean expression) thens1;s2;• Example:if (x = 1) thenwriteln('Body of if');writeln ('After body');If-Then (Simple Statement)Indicates _______________________________________________________. (Note: _________________________ ____________________________.)J. Michael MooreBody / Compound Statement• Body of if-then consists of multiple statements• Format:if (Boolean expression) thenbegins1;s2;:sn;end;sn+1;If-Then (Compound Statement)Indicates _________________________________________________________________________________J. Michael MooreIf-Then (Compound Statement)• Example:taxRate := 0.2;if (income < 10000) thenbeginwriteln('Eligable for social assistance');taxCredit = 100;end;tax = income * taxRate;J. Michael MooreIf-Then: What Gets Executed• When true, the if executes ____________which is a _________ statement.• ________statement: the body follows the 'then' and precedes the first semi-colon.• _______________statement: the body is enclosed within the begin-end pair as a compound statement.J. Michael MooreIf-Then-ElseQuestion?Execute a statementTrueFalseExecute a statementRemainder of the programJ. Michael Moore• Decision-making with two conditions (true or false)• Format:if (boolean-expression) thenbody of 'if'elsebody of 'else';additional statements;If-Then-ElseNo semi-colon ___________________________________________________________!!!Semi-colon _______________ __________________________ _________________________.Note: ____________________ _________________________ _______________.J. Michael MooreIf-Then-Else• Example:if (age >= 18) thenwriteln('Adult')elsewriteln('Not an adult');writeln('Tell me more about yourself');J. Michael MooreIf-Then-Else (Simple Statement)• Body of if-then-else consists of a _______statement• Format:if (Boolean expression) thens1elses2;s3;____________________________________________________________________________!!!_____________________________________________________________________________.J. Michael MooreIf-Then-Else (Simple Statement)• Example:if (x = 1) thenwriteln('body of if')elsewriteln('body of else');writeln('after if-then-else');J. Michael MooreIf-Then-Else (Compound Statement)• if (boolean-expression) thenbegins1;:sn;endelsebeginsn+1;:sn + m;end;sn + m + 1;No semi-colon (__________________________________!)Semi-colon (_____________________________________________________________________________!– _______________________________________)J. Michael MooreIf-Then(Compound Statement)• Example:if (income < 10000) thenbeginwriteln('Eligible for social assistance');taxRate := 0.1;endelsebeginwriteln('Not eligible for social assistance');taxRate := 0.2;end;tax = income * taxRate;J. Michael MooreQuick Summary: If Vs. If-Else•If:– Evaluates a __________________ (asks a question)– If the expression evaluates to _____ then execute the '______' of the _____.– If the expression evaluates to _______ then _________________________________________.– Use when your program evaluates a ____________ __________ and code will be ____________ only when the expression evaluates to ________.J. Michael MooreQuick Summary: If Vs. If-Else• If-else:– Evaluates a __________________ (asks a question)– If the expression evaluates to _____ then execute the '_______' of the ______.– If the expression evaluates to _____ then execute the '_______' of the ______.– Use when your program evaluates a _____________ ___________ and ________ code will ____________if the expression evaluates to _____ than if the expression evaluates to _____.J. Michael MooreNested Decision Making• Decision making is dependent.• The first decision must evaluate to ________before the ____________decisions are even ____________for evaluation.Question 1?TrueQuestion 2?TrueStatement Remainder of the programFalseFalseJ. Michael MooreOuter bodyInner bodyNested Decision Making• One decision is made ___________________• ______ decisions must evaluate to ______ before ______ decisions are even ___________ for evaluation.• Format:if (Boolean expression) thenif (Boolean expression) theninner body• Example: if (income < 10000) thenif (citizen = true) thenwriteln('Eligable for social assistance');tax := income * TAX_RATE;J. Michael MooreIs this for x or y????if (x > 0) thenif (y > 0) thenwriteln('x and y greater than
View Full Document