Pascal Programming: Week 4 Notes More on Robust Input Allow for varied user input to general questions: Solution to: "Y" or "y" or "Yes" or "yes" or "1" Sets - Using the "IN" operator List of values in a set Expression preceding In must be a String or Integer No Real Boolean makes no sense use of "NOT" with "IN" operator Example: Ans in ['Y', 'y', 'Yes', 'yes'] More on Evaluating Expressions Boolean Truth Tables The Difference between CAND/COR and AND/OR Short-Circuit vs. Complete Evaluation Problems and Reasons (Speed versus Logic) Example: IF (Value <> 0) AND (Answer DIV Value >= 20) THEN Writeln(' Incorrect'); Fix: IF (Value <> 0) THEN IF (Answer DIV Value >= 20) THEN Writeln(' Incorrect'); Compiler Directives Global and Local {$B+} {$B-} The Use of Flags Purpose: to provide a checking mechanism. If some condition is met the flag is set to a particular value. Differing actions are taken depending upon the value of the flag. Real-time Equivlent Branching and Looping: Branching The differences are seen in Assignment #2 IF THEN vs. IF THEN IF THEN Two-way branching and Multi-way Branching Binary Tree vs. Graph Structure IF THEN ELSE IF ELSE IF ELSE … Another Method: CASE Statements Examples: PROGRAM CaseSample (input, output); VAR Grade: char; BEGIN Writeln('What grade did you get?'); Readln(Grade); CASE Grade of 'A', 'B': Writeln('Great'); 'C': Writeln('OK'); 'D', F': Writeln('Give up'); ELSE Writeln('Not a Valid Grade'); END; {Case Statement} Writeln('See ya later!'); Readln; END. Branching and Looping: Looping WHILEs and REPEATs Another Method: FOR Loops Examples: FOR i:= 1 to 20 Do BEGIN {Do Something} i:= i+1; END; Beginning:= 1; Ending:= 20; FOR i:= Beginning to Ending Do BEGIN {Do Something} i:= i+1; END; Often used in indexing through arrays. Functions Pre-defined: SQR(), SQRT(), TRUNC(), ROUND(), and others User-defined: CUBE() example Functions are expressions Written like a function but it returns a value Arrays One-Dimensional Array Declaration Usage of arrays User-Defined Types Overview of Type Declaration Example