CUW CSC 250 - Control Structures, Arithmetic and Errors

Unformatted text preview:

Control Structures, Arithmetic and Errors.1. Flowchart symbols.Flowchart symbols (cont).2. Control Structures.Control Structures (cont.).Slide 6Slide 7Slide 8Slide 9Slide 10Slide 11Slide 12Slide 133. Arithmetic.Arithmetic (cont.).Slide 16Slide 17Slide 18Slide 19Slide 20Slide 21Slide 224. Programming errors.Programming errors (cont.).Slide 25Control 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.ProcessDecisionConnectorStart/StopI/OPre-definedProcessFlowchart 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 instructions52 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 awhile (condition) { }Count = 0; Annual_Sales = 0;while (Count < 52) { Weekly_Sales = Console.ReadLine(); Annual_Sales += Weekly_Sales; Count++; // increment Count} // end whileControl 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 whileControl 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 % 4If 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  5This 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


View Full Document

CUW CSC 250 - Control Structures, Arithmetic and Errors

Download Control Structures, Arithmetic and Errors
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Control Structures, Arithmetic and Errors and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Control Structures, Arithmetic and Errors 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?