Presentation is loading. Please wait.

Presentation is loading. Please wait.

15.1 More About High-Level Programming Languages Syntax and Semantics Each programming language contains its own rules for both syntax and semantics. Syntax.

Similar presentations


Presentation on theme: "15.1 More About High-Level Programming Languages Syntax and Semantics Each programming language contains its own rules for both syntax and semantics. Syntax."— Presentation transcript:

1 15.1 More About High-Level Programming Languages Syntax and Semantics Each programming language contains its own rules for both syntax and semantics. Syntax A set of rules that specifies how to write instructions Tells programmers how to combine symbols, numbers and letters that can be used when writing instructions. Semantics Determines the meaning of instructions

2 15.1 More About High-Level Programming Languages Syntax and Semantics Each programming language contains its own rules for both syntax and semantics. Syntax A set of rules that specifies how to write instructions Tells programmers how to combine symbols, numbers and letters that can be used when writing instructions. Semantics Determines the meaning of instructions

3 15.2 Input Statements, Output Statements and Assignment Statements Input Statement A flexible program can handle different sets of data without changing its program code. This requires that the data be separated from the program. Data Set 1 Program to process data Result Set 1 Data Set 2 Program to process data Result Set 2 Input Output Same program

4 15.2 Input Statements, Output Statements and Assignment Statements Input Statement Obtain inputs from users and store data as variables DescriptionPseudocodeCPascal Input an integer A Input A Int A; scanf (“%d”, &A); VAR A: integer; readln(A) Input a character Ch Input Ch Char Ch; scanf (“%c”, &Ch); VAR Ch: Char; readln(Ch) Input a real number Y Input Y float Y; scanf (“%f”, &Y); VAR Y: real; readln(Y) Examples of input statements

5 15.2 Input Statements, Output Statements and Assignment Statements Output Statements used to print the result of processed data The output can be displayed on the screen, saved to a file, or printed on a paper The output is usually formatted DescriptionPseudocodeCPascal Output a string ABC Output “ABC”printf(“ABC”);writeln(‘ABC’); Output an integer 10 Output 10printf(“10”);writeln(‘10’); Output an integer- typed variable X Output Xprintf(“%d”, X);writeln(X); Output a real-typed variable R Output Rprintf(“%f”, R);writeln(R); Examples of output statements

6 15.2 Input Statements, Output Statements and Assignment Statements Variable A kind of memory location The variable value is the content of the memory location Can be used to store data while the program is running

7 15.2 Input Statements, Output Statements and Assignment Statements Assignment statement Used to place a value of an expression into a variable Assignment operator Literally means ‘become’ = in C and := in Pascal Format of an assignment statement variable name = expression

8 15.2 Input Statements, Output Statements and Assignment Statements Assignment Statements StatementEvent X = 100 The value 100 is assigned to X. X = 100 * 2 The expression is an arithmetic expression and X is assigned to be 100 x 2. Therefore, X becomes 200 X = X + 20 The expression contains a variable X and X is assigned to be X + 20. X (its original value is 200) + 20 = 220. Therefore 220 is assigned to X. The value change of X in different assignment statements

9 15.2 Input Statements, Output Statements and Assignment Statements Assignment Statements DescriptionPseudocodeCPascal Assign 9 to X X = 9X = 9;X := 9; Assign a number K to Y Y = KY = K;Y := K; Add A to B B = B + AB = B + A;B := B + A; Subtract A from B B = B - A B := B – A; Increase M by1 M = M + 1M++;M := M + 1; Decrease N by 1 N = N - 1N--;N := N – 1; Examples of assignment statements

10 15.2 Input Statements, Output Statements and Assignment Statements Assignment Statements OperatorDescriptionExample + Add A + B - Subtract A – B * Multiply A * B / Divide A / B Sqr Square Sqr(A) Sqrt Square root Sqrt(A) x^y Power of y A^9 Mod Remainder A Mod B Common operators

11 15.2 Input Statements, Output Statements and Assignment Statements Assignment Statements OperatorDescriptionExample + Add A + B - Subtract A – B * Multiply A * B / Divide A / B Sqr Square Sqr(A) Sqrt Square root Sqrt(A) x^y Power of y A^9 Mod Remainder A Mod B Common operators

12 15.3 Sequence, Selection and Iteration Sequence control structure The simplest type of control structure Contain a series of steps or statements that are executed in the order in which they are written PseudocodeFlowchart statement_1 statement_2 …… statement_n statement_1statement_2statement_n

13 15.3 Sequence, Selection and Iteration Selection control structure Allow the program to choose between two or more alternatives depending on the specific conditions Type of selection control structure PseudocodeFlowchart with ELSE part IF { condition } THEN { THEN part } ELSE { ELSE part } ENDIF ELSE partTHEN part Condition NoYes

14 15.3 Sequence, Selection and Iteration Selection control structure Type of selection control structure PseudocodeFlowchart without ELSE part IF { condition } THEN { THEN part } THEN part Condition Yes No

15 15.3 Sequence, Selection and Iteration Selection control structure Relational operator ExampleExplanation =X = 100 X is equal to 100 <>X <> 100 X is NOT equal to 100 >X > 100 X is greater than 100 <X < 100 X is less than 100 >=X >= 100 X is greater than or equal to 100 <= X <= 100 X is less than or equal to 100 Examples of relational operators

16 15.3 Sequence, Selection and Iteration Selection Logical operator ExampleExplanation AND(X = 1) AND (Y = 5) X is equal to 1 and Y is equal to 5. OR(X = 1) OR (Y = 5) X is equal to 1 or Y is equal to 5. NOTNOT(X = 1) X is NOT equal to 1. Examples of logical operators

17 15.3 Sequence, Selection and Iteration Iteration The same set of statements (the body) will be repeated as long as a certain condition is met Pretest loop The condition is tested before the body of the loop Posttest loop The condition is tested after the body of the loop

18 15.3 Sequence, Selection and Iteration Iteration Loop-body Conditio n Yes No Loop-body Conditio n Yes No Pretest loop Posttest loop

19 15.3 Sequence, Selection and Iteration Iteration FOR-loop Repeat the statements in the loop-body a specific number of times Suggested format of a FOR-loop: FOR { variable } = { initial value } TO { final value } { loop-body } NEXT

20 15.3 Sequence, Selection and Iteration Iteration WHILE-loop A pretest loop The body is executed repeatedly while the specified condition is met. WHILE { condition } { loop-body } Loop-body Conditio n Yes No

21 15.3 Sequence, Selection and Iteration Iteration DO…WHILE-loop A posttest loop The program will run the loop-body at least once and then check whether the condition is met DO { loop-body } WHILE { condition } Loop-body Conditio n No Yes

22 15.3 Sequence, Selection and Iteration Iteration REPEAT…UNTIL-loop A posttest loop The loop-body will run repeatedly until the ‘exit condition’ is met REPEAT { loop-body } UNTIL { condition } Loop-body Conditio n Yes No

23 15.3 Sequence, Selection and Iteration Iteration REPEAT…UNTIL-loop A posttest loop The loop-body will run repeatedly until the ‘exit condition’ is met REPEAT { loop-body } UNTIL { condition } Loop-body Conditio n Yes No

24 15.4 Tracing Program Flow Dry Run Used to examine the logic and code of a program Steps of dry run Prepare a set of test data Create a trace table Input the test data into the program Let the test data run through the program one line at a time Write down the values of the program variables as they change

25 15.4 Tracing Program Flow Dry Run Example PROGRAM DVT 10Input Num1 ; allow users to enter a number 20Num2 = Num1 + 2 30Num1 = Num2 - 1 40Num2 = Num1 × 5 50Num2 = Num2 / 2 60Output Num1, Num2

26 15.4 Tracing Program Flow Dry Run Example StatementValue of Num1Value of Num2 Input Num1 3 (test data)Undefined Num2 = Num1 + 2 35 Num1 = Num2 - 1 45 Num2 = Num1 × 5 420 Num2 = Num2 / 2 410 Suppose the value of the test data is 3. therefore Num1 = 3. At the end of PROGRAM DVT, Num1 = 4 and Num2 = 10.

27 15.4 Tracing Program Flow Echo Checking To insert output statements at strategic locations in the program Aim: The intermediate values can be printed at each stage of the calculation or logic.

28 15.4 Tracing Program Flow Echo Checking C programPascal program /* Program DVT */ #include Void main() { int Num1, Num2; printf (“Enter a number: “); scanf (“%d”, &Num1); printf (“%d and %d\n”, Num1, Num2); Num2 = Num1 + 2; printf (“%d and %d\n”, Num1, Num2); Num2 = Num1 – 1; printf (“%d and %d\n”, Num1, Num2); Num2 = Num1 * 5; printf (“%d and %d\n”, Num1, Num2); Num2 = Num1 / 2; printf (“%d and %d\n”, Num1, Num2); printf (“The results: “); printf (“Num1 = %d and Num2 = %d\n”, Num1, Num2); } { Program DVT } PROGRAM DVT; VAR Num1, Num2 : integer; BEGIN WRITE (‘Enter a number: ‘); readln (Num1); writeln (Num1, ‘ and ‘, Num2); Num2 := Num1 + 2; writeln (Num1, ‘ and ‘, Num2); Num2 = Num1 – 1; writeln (Num1, ‘ and ‘, Num2); Num2 := Num1 * 5; writeln (Num1, ‘ and ‘, Num2); Num2 = Num1 DIV 2; writeln (Num1, ‘ and ‘, Num2); write (‘The results: ‘); writeln (‘Num1=’, Num1, ‘ and Num2=’, Num2) END Perform echo checking to print intermediate values

29 15.4 Tracing Program Flow Echo Checking Enter a number: 3undefined 35 45 420 The results: Num1 = 4 and Num2 = 20 The output of the above program when the value of test data is 3

30 15.4 Tracing Program Flow Echo Checking Enter a number: 3undefined 35 45 420 The results: Num1 = 4 and Num2 = 20 The output of the above program when the value of test data is 3


Download ppt "15.1 More About High-Level Programming Languages Syntax and Semantics Each programming language contains its own rules for both syntax and semantics. Syntax."

Similar presentations


Ads by Google