chap05 - QBASIC Chapter 5 QBASIC Chapter 5 Structured...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: QBASIC Chapter 5 QBASIC Chapter 5 Structured Programming and Menu­Driven Programs Created by Richard D. Taylor Illustrated by Saundra J. Taylor Structured Programming Structured Programming ­ Structured Programming - a methodology Structured according to which all program logic can be constructed from a combination of the following three basic control structures: following ­Sequence - the most fundamental of the Sequence structure, it provides for two or more actions to be executed in the order in which they appear be ­If-Then-Else or Selection - provides a choice If-Then-Else between two alternatives between Structured Programming Structured Programming ­Do-While or Repetition - provides for the repeated Do-While execution of a loop execution ­ Extensions: ­Do-Until - an extension of the Do-While structure ­Case - an extension of the If-Then -Else structure in Case which the choice includes more than two alternatives alternatives Structured Programming Structured Programming ­ Structured programming confers the Structured following benefits: following ­Programs are clearer and more readable ­Less time is spent debugging, testing, and Less modifying the program modifying ­The programmer’s productively increases ­The quality, reliability, and efficiency of the The program are improved program Structured Programming Structured Programming ­ Control Structures ­Decision making - a condition that must be Decision either true or false, asks whether two variables are equal or whether an expression is within a certain range certain Structured Programming Structured Programming ­ Combining Conditions ­Logical operators - (AND, OR, and NOT) may be Logical used to combine conditions to reduce the number of statements required to implement certain If-Thenstatements Else structures ­Data Validation - logical operators are often used in Data the conditions of decision statements that verify that the data entered by the user is within a range that will generate valid results when the program is executed executed Structured Programming Structured Programming ­ Top-Down versus Structured ­Top-down solution - use a top-down chart to Top-down determine what must be done determine ­Structured Programming solution for a subtask Structured - use a program flowchart or some other logic tool to resolve the question of how to implement the subtask implement The IF Statement The IF Statement ­ Form: IF condition, THEN clause ELSE IF clause clause If condition, THEN clause ELSE If clause END IF clause ­ Purpose: If the condition is true, the PC Purpose: executes the clause following the keyword THEN, if the condition is false and an ELSE clause is included, the PC executes the ELSE clause The IF Statement The IF Statement ­ Examples: ­IF Age > 65 THEN PRINT Age ELSE Switch$ = IF “Y” “Y” ­IF Tax >= 0 THEN Switchl$ = “Y” PRINT IF Gross.Pay ELSE Switch$ = “N” Gross.Pay ­ Nested IF statement - one in which a second IF Nested statement is found within the THEN or ELSE clause clause The IF Statement The IF Statement ­ Comparing Numeric Expressions ­If the condition in an IF statement includes two If numeric expressions, the comparison is based on the algebraic values of the two expressions algebraic ­ Comparing String Expressions ­IF the conditions in an IF statement includes two IF string expressions, the PC evaluates the two strings from left to right, one character at a time from The IF Statement The IF Statement ­ American Standard Code for Information American Interchange (ASCII) is the code that is used to store data in main memory to ­ Collating Sequence - the position of the Collating character in relation to the other characters character The IF Statement The IF Statement ­ Values of condition ­Conditions are evaluated by the arithmeticlogic unit (ALC) in the PC - IF the condition is logic true, it returns a -1, otherwise if it is false, it returns a 0 returns Accumulators Accumulators ­ - numeric variables that are initialized to a numeric predetermined value, such as zero, in the Initialization module, then incremented within the loop in the Process File Module, and then manipulated or displayed in the Wrap-up module Wrap-up Accumulators Accumulators ­ Counters - an accumulator that is used to count Counters the number of times some action or event is performed performed ­ Emp.count = Emp.count + 1 ­ Running totals - an accumulator that is used to Running sum the different values a variable is assigned during the execution of a program during ­ Total.Gross = Total.Gross + Emp.Gross Implementing the Do­While and Implementing the Do­While and Do­Until Structures ­ In designing and implementing a Do loop, think In carefully about where to place the decision to terminate the loop terminate ­ The WHILE and WEND Statements ­WHILE statement - initiates a Do-While loop ­WEND statement - terminates it Implementing the If­Then­Else Structure Implementing the If­Then­Else Structure ­ Simple Forms of the IF-THEN-ELSE Simple Structure Structure ­Alternative processing for the true and false Alternative cases cases ­Alternative processing for the true case ­Alternative processing for the false case ­With several statements for both the true and With false cases false Implementing the If­Then­Else Structure Implementing the If­Then­Else Structure ­ Nested Forms of the If-Then-Else Structure Nested one in which the action to be taken for the true or false case includes yet another Iftrue Then Else structure is considered to be Then nested, or layered, within the first nested, Logical Operators Logical Operators ­ The NOT Logical Operator ­Relational Expression - a condition made up of Relational two expressions and relational operators two ­Logical Operator - relational expression that is Logical preceded by the NOT forms a condition that is false when the relational expression is true false Logical Operators Logical Operators Condition A=B A<B A>B A <= B A >= B A <> B Method 1 A<>B A >= B A <= B A>B A<B A=B Method 2 NOT A=B NOT A < B NOT A > B NOT A <= B NOT A >= B NOT A <> B Logical Operators Logical Operators ­ The AND Logical Operator ­requires that both conditions be true for the requires compound condition to be true compound ­ The OR Logical Operator ­requires only one of the two conditions be true requires for the compound condition to be true for Logical Operators Logical Operators ­ The XOR, EQV, and IMP Logical Operators ­requires one of the two conditions be true for the requires compound condition to be true compound ­ Combining Logical Operators ­can be combined in a decision statement to form a can compound condition and the formation of compound statements that involve more than one type of logical operator can create problems unless done correctly correctly Logical Operators Logical Operators ­ The Effect of Parentheses in the Evaluation of The Compound Conditions Compound ­Parentheses may be used to change the order of Parentheses procedures - the PC evaluates that part of the compound condition within the parentheses first and then continues to evaluate the remaining compound condition according to the rules of procedures condition Data Validation Techniques Data Validation Techniques ­ Data Validation - the process of ensuring Data that valid data is assigned to a program that ­The reasonableness check ­The range check ­The value or code check ­The digit check Data Validation Techniques Data Validation Techniques ­ The BEEP Statement ­ Form: BEEP ­ Purpose: Causes the PC speaker to beep for Causes a quarter of a second quarter ­ Examples ­BEEP ­BEEP : BEEP: BEEP: BEEP Data Validation Techniques Data Validation Techniques ­ Reasonableness Check - ensures the legitimacy Reasonableness of data items that are entered from an external source. A program may check a string variable to ensure a specific number of characters is assigned to it assigned ­ The Range Check - ensures data items entered The from an external device fall within range of valid values Data Validation Techniques Data Validation Techniques ­ The Value or Code Check ­ensures values or codes entered from an external ensures source are valid. The value of class standing may be F for freshman, S for sophomore, J for junior, and G for senior, with all other entries invalid for ­ The Digit Check ­verifies the assignment of a special digit to a verifies number - a company may use a procedure whereby all part numbers of items sold begin with the digit 2 all The SELECT CASE Statement and The SELECT CASE Statement and Menu­Driven Programs ­ Used to implement the Case structure, the Used case structure is an extension of the Ifcase Then-Else structure The SELECT CASE Statement and The SELECT CASE Statement and Menu­Driven Programs ­ Form: SELECT CASE testexpression CASE matchexpression CASE ELSE END SELECT The SELECT CASE Statement and The SELECT CASE Statement and Menu­Driven Programs ­ Purpose: causes execution of the range of causes statements that follow the CASE clause whose matchexpression matches the testexpression - IF there is no match, then the range of statements in the CASE ELSE clause is executed. Following the execution of a range of statements, control passes to the statement following the END SELECT the The SELECT CASE Statement and The SELECT CASE Statement and Menu­Driven Programs ­ Example: ­SELECT CASE Age CASE IS = 0 CALL B200. Baby CASE ELSE PRINT “Invalid Age” END SELECT The SELECT CASE Statement and The SELECT CASE Statement and Menu­Driven Programs ­ Valid Match Expressions Example Match Expression 1 CASE “F” to “H”, “S”, CASE Emp.Code$ Emp.Code$ 2 CASE IS = Emp.Salary, IS = CASE Max.Salary Max.Salary Summary Slide Summary Slide ­ Structured Programming ­ The IF Statement ­ Accumulators ­ Implementing the Do-While and Do-Until Implementing Structures Structures ­ Logical Operators ­ Data Validation Techniques Summary Slide (cont.) Summary Slide (cont.) ­ The SELECT CASE Statement and Menu- Driven Programs ­ The SELECT CASE Statement and MenuDriven Programs ...
View Full Document

This note was uploaded on 06/06/2011 for the course MIS 105 taught by Professor Grigoletti during the Spring '11 term at Moraine Valley Community College.

Ask a homework question - tutors are online