control structures, arithmetic and errors. 1.flow-chart symbols. 2.control structures. 3....

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

Upload: hubert-evans

Post on 16-Dec-2015

225 views

Category:

Documents


2 download

TRANSCRIPT

Control Structures, Arithmetic and Errors.

1. Flow-chart symbols.

2. Control structures.

3. Arithmetic.

4. Programming Errors.

1. Flowchart symbols.

A flow-chart uses a small number of basic symbols to represent an algorithm. It visually displays the flow of control.

ProcessDecision

Connector

Start/Stop

I/O Pre-definedProcess

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.

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).

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.

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.

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.

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 instructions52 times.

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).

Control Structures (cont.).

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

Control Structures (cont.).

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

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.

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).

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)

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.

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.

Arithmetic (cont.).

Using these rules, what does the following evaluate to?

5 + 13 * 2 % 4If in doubt, use parentheses! Code with

clarity and modifiability in mind.

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.

Arithmetic (cont.).

For example:int Answer1; Answer1 = 17/3; //Answer1 5This is “type coercion” since a real

number is forced into integer format.

float Answer2; Answer2 = 17/3; Answer2 5.666…

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.

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.

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.

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.

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).

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.