Grade 10-12 NSC IT Cram Notes
Total Page:16
File Type:pdf, Size:1020Kb
Grade 10-12 NSC IT Cram Notes Key Grade 10 – Blue Grade 11 – Green Grade 12 – Red INPUTBOX MATHEMATICAL FUNCTIONS & PROCEDURES Allows user to enter data StringResult := InputBox StrToInt DELPHI BASICS CONCEPTS AND TOOLS (‘Windows caption’, ‘Prompt’, Default value’); Converts string value to integer iN := StrToInt(sNumber); Procedure TForm1.Button1Click(Sender: TObject); DELPHI FILES Var IntToStr Eight files when you save and run the program, main files; sName : string; Converts integer value to string begin sNumber := IntToStr(iN); Project file (.dpr / .dproj) sName := InputBox (‘Name’, ‘Enter your name’, ‘’); Form file (.dfm) lblName.Caption := sName; StrToFloat Unit file (.pas) end; Converts string value to real rN := StrToFloat(sNumber); BASIC RULES Assignment operator := CALCULATIONS USING VARIABLES FloatToStr left side gets, right side gives Converts real value to string A := B sNumber := FloatToStr(rN); VARIABLES String Text must be place between single quotes ‘ ‘ FloatToStrF Values that are text (Numbers and letters) sText := ‘Hello World’ Converts real value to string with formatting (1 number Abbreviation: s (sName) after the decimal for example) Strings are joined by a + sign sN := FloatToStrF(rN, ffFixed, 15, 1); Integer sText := 'Hello' + 'World' Whole numbers (Numbers with no digits after the decimal Trunc point Input, Processing, Output (IPO) Truncates (cuts) the decimal point Abbreviation: i (iNumber) Trunc(4.5); Real Val Whole and fractions (Numbers with digits after the decimal Converts string value to integer or real. Also checks for point) errors. Abbreviation: r (rAmount) Val(sOriginal, iNum, iError); ERRORS Val(sOriginal, rNum, iError); Syntax Errors: Common mistakes: no semi-colon at the end Char of a line; : = instead of := (no space between : and =) spelling Any single characters (Text, Symbol or Number) Round errors Abbreviation: c (cInitial) Rounds real number to the nearest integer Round(4.8); Logical Errors: Errors created by the programmer in Boolean specifying the wrong steps in the program. These errors True or False Roundto cause the program to produce incorrect output. Abbreviation: b (bValid) Rounds to a set power of 10 iH := Roundto(2745,3); Run-Time Errors: Errors that cause the program to abort or Take note; crash prior to its normal expected termination. These - Variables is a place keeper used to store values in Inc include the program not finding a data file it expected or - Variables are declared after the abbreviation “var” Increases value of variable dividing by zero. - Global Variables are declared above implementation at the (You can also do this as: top of the programming code. These variables are valid iNumber := iNumber + 1;) throughout the program. Inc(iNumber) NAMING CONVENTIONS - Local Variables are declared inside the event handler (the Components must be renamed with an abbreviation procedure you create yourself by double clicking on a button Dec indicating the type of component, followed by a descriptive in design mode). You must type in the var as well as the Decreases value of variable name. variables themselves. These variables are only valid within (You can also do this as: Form (frm) this specific event handler or procedure. iNumber := iNumber - 1;) Shape (shp) - Variables with the same data types can be listed after each Dec(iNumber); Button (btn) other separated by a comma: Panel (pnl) Frac Label (lbl) var Provides decimal part of real number Edit (edt) sName, sSurname : String; rK := Frac(3.54); RichEdit (red) iAge, iNumb1, iNumb2 : Integer; Image (img) rAmount : Real; Sqr Groupbox (gbx) Gives square of number typed in Bitmap button (bmb) MATHEMATICAL OPERATORS rK := Sqr(16); Memo (mem) Addition + Radiogroup (rgp) Integer / Real Sqrt Radiobutton (rad) iX := iA + iB; Gives square root of a number Timer (tmr) rK := Sqrt(16); Checkbox (cbx) Subtraction - MaskEdit (med) Integer / Real Power SpinEdit (sed) rX := rA – RB Raises first number to the power of the second number Progress Bar (prb) (134) Page Control (pgc) Multiplication x Power(13,4) StatusBar (stb) Integer / Real ListBox (lst) iX := iA * iB; Pi Provides the value of pi (π) Examples Division ÷ rW := rK * Pi; Editbox receiving name – edtName Integer / Real Label displaying a final mark – lblFinal iX := iD / iS; Random Provides a random number within a range of 0 and limit-1. DIV Randomize; RICHEDIT COMPONENT Integer Division iX := Random(100); The RichEdit component can be used for both input as well iX := iA DIV iB; (iX = any number between 0 and 99) as output. MOD Remember to add the Math unit to the uses section of your Add text to RicheEdit, #9 refers to a Tab (ASCII) Remainder after division / Integer program when using the RoundTo and Power functions. RichEdit1.Lines.Add (‘Text’ + #9 + ‘Text’); iX := iA MOD iB; Clear RichEdit RichEdit.Clear Files can also be opened using the LoadFromFile procedure RichEdit1.Lines.LoadFromFile (‘test.rtf’); RichEdit text can be saved directly into a RTF (RichText Format) using the SaveToFile procedure. RichEdit1.Lines.SaveToFile (‘test.rtf’); SHOWMESSAGE COMPONENT Small popup window with text ShowMessage (‘This is a showMessage!’); REPEAT LOOP - Conditional Loop MAKING DECISIONS - Post-test Loop – condition will be tested after the loop! HUMAN COMPUTER INTERACTION - Will loop at least once! - Repeats depending on a certain condition(s). IF & CASE STATEMENTS DETERMINING THE FOCUS - No begin.. end needed Basic Structure of an if Statements edtName.Setfocus; - The condition(s) indicate when to stop; not when to go on if <conditions(s)> then as with the WHILE. begin - ITC DEFENSIVE PROGRAMMING MESSAGE DIALOG BOX <statements> - Variables to be tested must be initialised before Provide the user with certain options or warnings and let the end; Execution. user then accept the message or decision. - Variable must be changed at the end of loop to avoid an Basic Structure of an if..then..Else statement: infinite loop (loop will never stop repeating Message types: if <condition(s)> then - Variable is tested at the end of the loop. If the statement - mtWarning begin Is FALSE then the loop will be repeated again. - mtError <statements> - mtInformation end Basic structure of the Repeat loop: - mtConfirmation else {If the top condition is not met then…} repeat - mtCustom begin <statement(s)> <statements> <change condition to stop loop> Button types: end; until <Conditions to stop>; - mbYes - mbNo Basic Structure of a Nested If statement: - mbCancel If <condition(s)> then - mbHelp begin - mbAbort <statements> SIMPLE STRING HANDLING - mbRetry end - mbIgnore else STRING HANDLING - mbAll if <condition(s)> then begin sSourceText := ‘The man walks’ There are also groupings such as <statements> - mbYesNoCancel end Text variables are usually declared as Strings. A Number of - mbOKCancel else {If the top conditions are not met then…} component properties are also string (text) values. For - mbAbortRetryIgnore begin example Button1.Caption or Edit1.Text <statements> Examples end; For example sSourceText can be declared as String under var MessageDlg (‘Invalid Input!’, mtWarning, [mbOK], 0); Additional logical operators can also be added: AND, OR, Take note that each character in a String variable can be if MessageDlg (‘This is an error’, mtError, [mbOK, mbAbort], NOT (use brackets around conditions) referred to by using an index. The character sSourceText[2] 0) = mrOK then would for instance be ‘h’. lblOutput.Caption := ‘User pressed OK’ Example else if (inumber >= 5) and (inumb2 >= 5) then lblOutput.Caption := User pressed Abort; lbloutput.caption := ‘Both numbers are greater than five’ else if MessageDlg (‘Do you want to exit’, mtConfirmation, Lbloutput.caption := ‘None of the numbers are greater than [mbYes, mbNo], 0) = mrYes then Application.Terminate; five’; Even the spaces therefore has index values (see characters 4 {Close form with Form1.Close} and 8 above). Basic Structure of the case statement: - Determine the position of a piece of a text within a string case <selection statement> of IntegerVariable := Pos(StrToBeFound, SourceText); <value1> := <statement(s)>; For example: iX := Pos (‘m’, sSourceText); <value2> := <statement(s)>; {The value of iX is now: 5} <value3> := <statement(s)>; else - Display a certain character within a string using square <statement(s)> brackets at the end of a variable. end; StringVariable := SourceText[CharacterPosition]; For example: sNewText := sSourceText[2]; {The value of sNewText is now: ‘h’} - Display a certain section of text within a string. LOOPS (ITERATION) For example: StringVariable := Copy(SourceText, BeginPosition, Length); FOR LOOP sNewText := Copy(sSourceText, 5, 3); - Unconditional Loop – no condition that must be met {The value of sNewText is now: ‘man’} - Repeat instructions for a set number of times. - Uses a counter (must be ordinal: integer or char) - Insert a certain section of text within a string. - Users a start and ending value that determines repetitions For example: Insert(InsertText, SourceText, Position); - Can count/move up (e.g. 1 TO 10) or down (10 DOWNTO 1) Insert(‘big ’, sSourceText, 5); {The value of sSourceText is now: ‘The big man walks’} Basic Structure of a for loop: for <counter> := <start value> to <ending value> do - Remove a certain section of text within a string. begin For example: Delete(SourceText, Position, Length); <statements;> Delete(sSourceText, 5, 4); end; {The value of sSourceText is now: ‘The walks’} WHILE LOOP - Determine the length of a string. - Conditional Loop For example: IntegerVariable