Presentation is loading. Please wait.

Presentation is loading. Please wait.

Control Structures, Arithmetic and Errors. 1.Flow-chart symbols. 2.Control structures. 3. Arithmetic. 4. Programming Errors.

Similar presentations


Presentation on theme: "Control Structures, Arithmetic and Errors. 1.Flow-chart symbols. 2.Control structures. 3. Arithmetic. 4. Programming Errors."— Presentation transcript:

1 Control Structures, Arithmetic and Errors. 1.Flow-chart symbols. 2.Control structures. 3. Arithmetic. 4. Programming Errors.

2 1. Flowchart symbols. §A flow-chart uses a small number of basic symbols to represent an algorithm. It visually displays the flow of control. Process Decision Connector Start/Stop I/O Pre-defined Process

3 Flowchart symbols (cont). §These symbols are a visual alternative to pseudocode for representing algorithms. The advantage over pseudocode is that they can visually represent branching (“ifs”), repetition (“loops”) and function calls. §We will use them to explain the 4 main control structures of procedural programming.

4 2. Control Structures. §A control structure is a way of controlling the order of execution of program instructions. §A control structure determines 2 things: §(a) which instructions are executed (all or some); and (b) how often they are executed (not at all, just once, or more than once). §There are 4 main control structures: §(1) Sequential (straight-line), (2) Selection (branching), (3) Repetition (looping), (4) Subprogram (function).

5 Control Structures (cont.). §(1) Sequential (straight-line). This is the default, if we do not use any logic tests. §Flow chart: a single straight line from Start to Stop, no branching, no looping. Like Program 1 and 2. §Therefore: §All statements are executed, and all are executed once.

6 Control Structures (cont.). §What is the limitation of the sequential control structure? §It does not allow choices (it is un-American!). Cannot process different data values in different ways, e.g. does not do different processing for different age groups, management levels, genders or depending on whether a student is a freshman, sophomore, junior or senior.

7 Control Structures (cont.). §(2) Selection (branching). §Flow chart: After data is input, there is one or more decision. §The value of the data determine how it is processed. §E.g. males and females processed differently. §Therefore: only the branch matching the data is executed; the other branches are ignored.

8 Control Structures (cont.). §What is the limitation of selection? §Still only executes an instruction once. §But often we have repeated tasks, e.g. input 52 weekly sales figures to calculate the annual total and the average weekly sales. §Of course we could just type in the same instructions 52 times, but more economical to… §Use a loop that repeats the same instructions §52 times.

9 Control Structures (cont.). §(3) Repetition/Looping. §2 Flow-charts: may be a pre-test loop (test the loop condition, if true, execute the loop body, then re-test the loop condition) until…. §The loop condition is false. §a post-test loop (execute the loop body, test the loop condition, if true, re-execute the loop body, otherwise exit the loop).

10 Control Structures (cont.). §In C#, a pre-test loop is executed by a §while (condition) { } §Count = 0; Annual_Sales = 0; §while (Count < 52) { § Weekly_Sales = Console.ReadLine(); § Annual_Sales += Weekly_Sales; § Count++; // increment Count §} // end while

11 Control Structures (cont.). §In C#, a post-test loop is executed by a §do { } while (condition) §Count = 0; Annual_Sales = 0; §do { § Weekly_Sales = Console.ReadLine(); § Annual_Sales += Weekly_Sales; § Count ++; §} while (Count < 52); // end do while

12 Control Structures (cont.). §What is the limitation of looping? §Loops allow you to repeat instructions in the same place (the loop body), but are not helpful if you want to re-use the same code in 25 places in your program. §For that it is nice to pre-define a “service” (a method or function) that can be used (called from) anywhere in the program.

13 Control Structures (cont.). §(4) Subprograms / methods / functions. §Flow-chart: pre-defined process symbol. Instructions for a useful task are put together in a method that can be invoked (called) whenever it is needed. Later we will also look at classes where data and the methods used to manipulate it are bundled together in one package (object oriented programming).

14 3. Arithmetic. §[Hand out Operator.doc and work through.] §Operator: signifies an operation (verb). §Operand: signifies data (nouns) which are operated on. §The basic C# arithmetic operators are: §+, -, *, / and % (modulus)

15 Arithmetic (cont.). §% signifies modulus = the remainder of integer division. §For example 17 % 3 = 2, §because 17/3 = 5 remainder 2. §% requires that BOTH operands are integers.

16 Arithmetic (cont.). §The precedence is: 1) *, /, % 2) +, - §If two operators have the same precedence? §Left before right. §Parentheses can be used to promote precedence §(Num + 5) * 7 // forces addition first even §// though lower precedence than multiplication.

17 Arithmetic (cont.). §Using these rules, what does the following evaluate to? §5 + 13 * 2 % 4 §If in doubt, use parentheses! Code with clarity and modifiability in mind.

18 Arithmetic (cont.). §Some languages (e.g. Pascal) have different symbols for integer arithmetic and real arithmetic. §C# uses the same symbol /, but “overloads” it, giving a different interpretation depending on the operands used.

19 Arithmetic (cont.). §For example: §int Answer1; § Answer1 = 17/3; //Answer1  5 §This is “type coercion” since a real number is forced into integer format. float Answer2; § Answer2 = 17/3; Answer2  5.666…

20 Arithmetic (cont.). §Type coercion is not encouraged because it is unclear whether the programmer meant the decimal expansion to be truncated. A better approach is casting. §int Answer1; § Answer1 = Convert.ToInt32 (17/3); §Or: Answer1 = int (17/3); // old format. §Makes it clear the truncation is intentional.

21 Arithmetic (cont.). §Math functions. §See the methods defined within the Math class, such as: §Math. pow (X, Y) returns X to the Yth power. §Math.sqrt (X) returns the square root of X.

22 Arithmetic (cont.). §See Math_Examples.cs on the CSC250 website for examples of handling conversions between real and integer, rounding etc. §Within Visual Studio, see Help/Index/Math for a list of math functions and code examples.

23 4.Programming errors. §Recall last time, we noted that any programming language has both a syntax and a semantics? §Syntax = ? §Semantics =? §Programmers can (and do) therefore make both syntax and semantic errors.

24 Programming errors (cont.). §1) Syntax errors are found at compile time. §2) Semantic errors are found (if ever!) at run time. §There are 2 types of semantic error: §A) Run-time errors—cause an unhandled exception / abnormal end (e.g. division by zero).

25 Programming errors (cont.). §B) Logic errors: processing continues, but the logic of the algorithm is wrong, e.g. omitted steps, incorrect calculation, infinite loop. §See the hand-out on program errors.


Download ppt "Control Structures, Arithmetic and Errors. 1.Flow-chart symbols. 2.Control structures. 3. Arithmetic. 4. Programming Errors."

Similar presentations


Ads by Google