chap04 - QBASIC Chapter 4 QBASIC Chapter 4 Looping and...

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 4 QBASIC Chapter 4 Looping and Input/Output Created by Richard D. Taylor Illustrated by Saundra J. Taylor Introduction ­ Straight-Line Program ­ Read and Data Statements - integrating data into the program ­ Tabular formatted Reports PRINT USING LOCATE ­ DO LOOP - begins with a DO statement and ends with a LOOP Statement Introduction ­ DO WHILE - a condition that can be true or false PASS - one execution of a DO LOOP ­ Range of statements - the lines from the DO until the LOOP commands ­ Primary Read or Lead Read - is the READ Statement Introduction Introduction ­ Testing for the End-of-File ­Sentinel Value - value of EOF clearly distinguishable from all the rest of the data ­End-of-File Routine - causes the PC to terminate the execution of the program Introduction Introduction ­ Testing for the End-of-File ­It is important that the trailer record contain enough values for all the variables in the READ Statement ­The DO LOOP requires the use of two READ Statements ­A program can process any number of Products by simply placing each in a DATA statement prior to the trailer record The DO and LOOP Statements The DO and LOOP Statements ­ FORM: DO DO DO WHILE DO UNTIL ­ Purpose: causes the statements between DO and LOOP to be executed repeatedly ­With the first general form, DO, the loop is controlled by a condition in the corresponding LOOP statement The DO and LOOP Statements The DO and LOOP Statements ­DO WHILE causes the loop to be executed while the condition is true ­DO UNTIL causes the loop to be executed until the condition becomes true ­ Examples: DO WHILE emp.name$ <> “EOF” DO UNTIL Discount >= 500 The DO and LOOP Statements The DO and LOOP Statements ­ Form: LOOP LOOP WHILE LOOP UNTIL ­ Purpose: identifies the end of a DO LOOP The three general forms work in the following way: The DO and LOOP Statements The DO and LOOP Statements ­If the condition is in the corresponding DO statement, then the LOOP statement automatically returns control to the DO statement ­LOOP WHILE causes the loop to be executed while the condition is true ­LOOP UNTIL causes the loop to be executed until the condition becomes true The DO and LOOP Statements The DO and LOOP Statements ­ Examples: LOOP LOOP WHILE CONTROL$ =“Y” LOOP UNTIL CONTROL$ = “Y” The DO and LOOP Statements The DO and LOOP Statements ­ Selecting the Proper DO Loop for a Program ­If the decision to terminate is at the top of the loop, use DO WHILE or DO UNTIL - if the decision to terminate is at the bottom of the loop use LOOP WHILE or LOOP UNTIL ­Use the keyword WHILE if you want to continue execution of the loop while the condition is true Use the keyword UNTIL if you want to continue execution of the loop until the The DO and LOOP Statements The DO and LOOP Statements ­ Conditions Relations Equal to Less than Greater than Less than or equal to Greater than or equal to Not equal to Math QBASIC Symbols Symbols = < > = < > Examples Code$ = “I” Gross < 1000 Rate > 0.05 <= or =< Tax <= 250 >= or => Count >= 10 <> or >< Same$ <> “End” The READ, DATA, and RESTORE The READ, DATA, and RESTORE Statements ­ The DATA Statement - provides for the creation of a sequence of data items for use by the READ statement ­FORM: DATA data item ­Purpose: Provides for the creation of a sequence of data items for use by the READ statement ­Examples: DATA Feb, Mar, Apr DATA 12.00, 13.50, 15.76 The READ, DATA, and RESTORE The READ, DATA, and RESTORE Statements ­ The READ Statements - provides for the assignment of values to variables from a sequence of data items created from DATA statements ­Form: READ variables ­Purpose: Provides for the assignment of values to variables from a sequence of data items created from DATA statements ­Examples: ­ ­ READ Month$, Discount, Commission, Returns READ Dis, Fac, Adjust(J), X#, Y, Z% The READ, DATA, and RESTORE The READ, DATA, and RESTORE Statements ­ The RESTORE Statement - usually data items from a DATA statement are processed by a READ statement only once IF you want the PC to read all or some of the same data items later, you must use the RESTORE statement to restore the data ­ Form: RESTORE Label The READ, DATA, and RESTORE The READ, DATA, and RESTORE Statements ­ Purpose: Allows the data in the program to be reread, If no label follows the keyword, RESTORE, then the next READ statement accesses the first data item in the first DATA statement. If a a label follows RESTORE, the next READ statement accesses the first data item in the first DATA statement that immediately follows the specified label The READ, DATA, and RESTORE The READ, DATA, and RESTORE Statements ­ Examples: RESTORE RESTORE Record The PRINT Statement The PRINT Statement ­ The PRINT Statements - used The to write information to the screen ­ Form: PRINT item prn item ­ Purpose: writes information to the screen ­ Examples: PRINT PRINT Emp.Name$ PRINT Count; Discount, Employee$; Number The PRINT Statement The PRINT Statement ­ Print Zones and Print Position ­Print Zones has 14 positions for a total of 70 positions per line ­Print Positions numbered consecutively from the left, starting with position one ­ Representation of Numeric Output ­ Representation of String Output The PRINT Statement The PRINT Statement ­ Use of the Comma Separator , ­Comma separator - allows you to display values that are automatically positioned in tabular format ­Each PRINT statement executed display one line unless one of the following conditions are true: The number of print zones required by the PRINT statement exceeds five ­ The PRINT statement ends with a comma or semicolon ­ The PRINT Statement The PRINT Statement ­ Use of the Semicolon Separator ; ­Semicolon - causes the display of the value immediately to the right of the previous one ­ Creating Blank Lines ­If a PRINT statement contains a null list, then a blank line results ­Example: PRINT The PRINT Statement The PRINT Statement ­ Use of the TAB FUNCTION ­to specify the exact print positions for the various output results on a given line ­ Example: PRINT TAB (10); “PUC Company” ­ Displaying Spaces-the SPC Function - works in a fashion similar to the Spacebar on a typewriter, may be used to insert spaces between print items ­Example: PRINT “Column1”; SPC(3); “Column2” The PRINT Statement The PRINT Statement ­ Calculations with the PRINT Statement - permits calculations to be made within the PRINT statement ­PRINT 4+2; SPC(4); 3 * 2 ­ Using the Immediate Window - narrow window at the bottom of the screen called the immediate window, which permits your PC to act as a powerful desk calculator ­ The LPRINT Statement - prints the results to the The PRINT USING Statement for The PRINT USING Statement for Formatted Output ­ You can do the following with PRINT You USING: USING: ­specify the exact image of a line of output ­force decimal-point alignment when displaying force numeric tables in columnar form numeric ­control the number of digits displayed for a control numeric results numeric ­specify that commas be inserted into a number The PRINT USING Statement for The PRINT USING Statement for Formatted Output ­ You can do the following with PRINT You USING: USING: ­specify that the sign status of the number be specify displayed along with the number displayed ­assign a fixed or floating dollar sign to the assign number display number ­force a numeric result to be displayed in a force exponential form exponential The PRINT USING Statement for The PRINT USING Statement for Formatted Output ­ You can do the following with PRINT You USING: USING: ­left-justify or right-justify string values in a left-justify formatted field formatted ­round a value automatically to a specified number round of decimal digits of The PRINT USING Statement for The PRINT USING Statement for Formatted Output ­ Form: PRINT USING string expression; list ­ Purpose: Provides for controlling the format of a Provides program’s output by specifying an image to which that output must conform which ­ Examples: ­PRINT USING “The answer is #,###.##”; Cost ­PRINT USING Format$; Total ­PRINT USING “\ \”; Cust.Name# The PRINT USING Statement for The PRINT USING Statement for Formatted Output ­ Declaring the Format of the Output ­Method 1 format Specified as a String in the PRINT USING format Statement Statement ­ PRINT USING “employee ### has earned $$,###.##”; PRINT Clock; Salary Clock; ­ ­Method 2 Format Specified Earlier and Assigned to a String Format Variable Variable ­ T1$ = “Employee ### has earned $$,###.##” ­ The PRINT USING Statement for The PRINT USING Statement for Formatted Output ­ Format Symbols ­Descriptor field or format field - one or more Descriptor consecutive format symbols appearing in a string expression expression ­ The Number Sign Symbol ­The number sign (#) is the format symbol used to The define a numeric descriptor field define # indicates one position in a numeric result ­ ####.## indicates six positions, two of which are decimal ­ The PRINT USING Statement for The PRINT USING Statement for Formatted Output ­ The Decimal Point (Period) Symbol ­The period (.) in a numeric descriptor field places a decimal The point in the output record at that character position in which it appears, and the format of the numeric result is aligned with the position of the decimal point with ­ The Comma Symbol ­A comma (,) to the left of the decimal point in a numeric comma descriptor field places a comma in front every third digit to the left of the decimal point the The PRINT USING Statement for The PRINT USING Statement for Formatted Output ­ The Plus and Minus Sign Symbols The ± ­A plus or a negative sign as either the first or last plus character in a numeric descriptor field ­Floating sign - if the plus sign is the first character Floating in the descriptor field in ­Fixed sign - if the plus sign is the last character in Fixed the descriptor field the The PRINT USING Statement for The PRINT USING Statement for Formatted Output ­ The Dollar Sign Symbol The $ ­Fixed dollar - a single dollar sign ($) appearing to Fixed the left of a numeric descriptor field causes a $ to be displayed in the position of the output record displayed ­Floating dollar sign - the dollar sign will display to Floating the left of the first significant digit. Two leading dollar signs appearing together: one position is filled by the dollar sign and the second sign reserves a digit position digit The PRINT USING Statement for The PRINT USING Statement for Formatted Output ­ The Asterisk Symbol The * ­Two asterisks (**) starting at the left side of a Two numeric descriptor field cause the value to be displayed in asterisk-filled format displayed ­Check Protection Asterisks - leading asterisks often Check are used when monetary checks are being printed or when the result must be protected when The PRINT USING Statement for The PRINT USING Statement for Formatted Output ­ Formatted Character String Output ­Descriptor fields for string values are defined in Descriptor terms of terms ampersand (&) - represents a variable-length string field ­ two backslashes (\\) - indicate how many positions ­ the exclamation point (!) - string constant to be displayed ­ underscore ( _ ) - precede the exclamation point ­ ­rather than the number sign (#) The PRINT USING Statement for The PRINT USING Statement for Formatted Output ­ The LPRINT USING Statement ­Prints the results on the printer ­Printer Spacing Chart - define the report and column Printer headings headings ­Heading Lines - what is printed on the top of each Heading page page ­Detail Lines - is displayed for each record processed The LOCATE Statement The LOCATE Statement ­ LOCATE statement may be used to position the LOCATE cursor precisely at any position on the screen cursor ­ Form: LOCATE row, column, cursor ­ Purpose: To position the cursor precisely on the To screen and to make the cursor visible or invisible invisible The LOCATE Statement The LOCATE Statement ­ Examples ­LOCATE 5, 10 ­LOCATE 1, 1 Summary Slide Summary Slide ­ Introduction ­ The DO and LOOP Statements ­ The READ, DATA, and RESTORE Statements ­ The PRINT Statement ­ The PRINT USING Statement for Formatted The Output Output ­ The LOCATE Statement ...
View Full Document

Ask a homework question - tutors are online