DOC PREVIEW
UNC-Chapel Hill COMP 004 - LECTURE NOTES

This preview shows page 1-2 out of 7 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 7 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 7 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 7 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

COMP 4, Summer 2005 Lec 13 HANDOUT ASelection structureCOMP 4, Summer 2005 Lec 13 HANDOUT ASyntax for Assignment statement: := is the assignment operator[varname]:=[constant, var, or arith. expression]Examples:taxRate := .32message := "Go Heels!"average := (score1+score2+score3)/totNum How it works: Acts like two statements:1st Turing evaluates everything on RHS of := 2nd Turing stores resulting value to varname on LHS of := Note: := is read as: "is assigned to" ("RHS is assigned to LHS")Specification conditions:1)varname must be declared proper type; 2)string values must be in quotesType hours [Enter] and rate [Enter]% DECLARE VARIABLES:var hours, rate, grosspay, netpay : realput "Type hours [Enter] and rate [Enter]”get hours, rate% PROCESSING & STORE % Calculate Gross Pay:% Deductions are 40% of grosspay grosspay := hours * rate% Calculate Net Pay: netpay := grosspay - (grosspay*.40)cls% OUTPUT put "Gross pay is: ", grosspayput "Net pay is: $", netpayStep Through (desk check; program trace …) RAM RUN WindowHours rate 2Program Development Life Cycle (some in text reading)Analyze the problem (we will do this for you) What do I want the program to do for the user? Desired outputs (in general terms)?Design an algorithm to solve the problem (you do) What are the specific desired outputs? (start here) What will be the inputs? How can inputs be transformed into outputs? Write Code (you do) Express the algorithm in a high-level computer language (Turing 4.0.2) Control structures: (flow of execution) Simple sequence Selection (decision/branch) Repetition (loop: statements ‘inside loop’ repeated)Run & Debug (you do) Find and fix syntax errorsTest Output (you do) Find and fix logic errorsDocumentation (you do‘internal’ documentation only: part of your code) done throughout life cycle for programmers: (internal & external) for users (screen prompts, user manuals)Maintenance (maintenance programmers do) fix errors (bugs) not found before enhancements; more bells & whistles….("Upgrade")3ANALYZE PROBLEM #1:Write a program that calculates average grade based on 6 scores (of equal weight); decides ifstudent passed based on average grade; displays PASS or FAIL message; and displays average grade.DESIGN AN ALGORITHMInput/store > Process/store > OutputDocumentation should occur throughout the process.*Declare variables (4th)Tell User what Program Does (5th)Inputs (from user) (2 nd )Processing (3 rd )Outputs (1 st )*Go back and add more internal documentation, as needed (6 th ) !4Selection structure Syntax: (simplest form)if [condition] then[statement(s) if condition true]else {optional}[statement(s) if condition false {opt'l} end ifExample:if hours <= 40 thengrosspay := hours*rateelseovertime := (hours-40) * (rate*1.5)grosspay := (hours*rate)+ overtimeend if BACK to PROBLEMS: See Handout A: Problem #1 (demo)5ANALYZE PROBLEM #2:Want program to assign letter gradeA, B, C, D, or Fbased on average score. What's the fundamental difference between this problem and Problem 1?Syntax: (expanded form)if [condition] then[statement(s) if condition true]elsif [condition] then[statement(s) if condition true]else {optional}[statement(s) if condition false {opt'l} end if1) Can put as many ‘elsif’ clauses as you like (or none).2) As SOON as 1st condition is TRUE, immediately execute statement(s) following its ‘then’; and test no more! Next, control goes directly to statement following ‘end if’, if there are statements (or program just ends).3) Note: ‘else’ clause is optional; omit it if you want nothing to occur when all conditions are false.(…for the same reason that the "false" action can be left out of an If function argument in Excel.)6Example:if age < 16 thenput "You’re too young to drive”elsif age < 18 thenput "You’re too young to vote”elsif age < 21 thenput "You’re too young to drink”elsif age < 30 thenput "You’re too young to retire”elseput "You’re over the hill, bucko!”end ifStep Through (Desk check; Program trace …)RAM RUN WindowBACK to PROBLEMS: See Handout C: Problem #2


View Full Document

UNC-Chapel Hill COMP 004 - LECTURE NOTES

Download LECTURE NOTES
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view LECTURE NOTES and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view LECTURE NOTES 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?