Control Structures, Arithmetic and Errors.

Slides:



Advertisements
Similar presentations
CS 101 Introductory Programming - Lecture 7: Loops In C & Good Coding Practices Presenter: Ankur Chattopadhyay.
Advertisements

Programming with Microsoft Visual Basic th Edition
CMSC 104, Version 9/011 Arithmetic Operators Topics Arithmetic Operators Operator Precedence Evaluating Arithmetic Expressions In-class Project Incremental.
Types and Arithmetic Operators
CS0004: Introduction to Programming Repetition – Do Loops.
Programming in Visual Basic
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
ITEC113 Algorithms and Programming Techniques
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Chapter 2: Algorithm Discovery and Design
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Fundamentals of Python: From First Programs Through Data Structures
Adapted from slides by Marie desJardins
Computer Science 101 Introduction to Programming.
CIS Computer Programming Logic
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Chapter 12: How Long Can This Go On?
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Jaeki Song ISQS6337 JAVA Lecture 04 Control Structure - Selection, and Repetition -
Input, Output, and Processing
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic.
Chapter 3 Assignment, Formatting, and Interactive Input C++ for Engineers and Scientists Third Edition.
Property of Jack Wilson, Cerritos College1 CIS Computer Programming Logic Programming Concepts Overview prepared by Jack Wilson Cerritos College.
CPS120: Introduction to Computer Science Operations Lecture 9.
Chapter 7 Additional Control Structures. Chapter 7 Topics l Switch Statement for Multi-Way Branching l Do-While Statement for Looping l For Statement.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
ITEC113 Algorithms and Programming Techniques
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Chapter 3: Assignment, Formatting, and Interactive Input.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
COIT29222 Structured Programming 1 COIT29222-Structured Programming Lecture Week 02  Reading: Textbook(4 th Ed.), Chapter 2 Textbook (6 th Ed.), Chapters.
CMSC 104, Version 8/061L10ArithmeticOps.ppt Arithmetic Operators Topics Arithmetic Operators Operator Precedence Evaluating Arithmetic Expressions In-class.
PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Chapter 3 Assignment, Formatting, and Interactive Input C++ for Engineers and Scientists Third Edition.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
Flow Charts And Pseudo Codes Grade 12. An algorithm is a complete step-by- step procedure for solving a problem or accomplishing a task.
Programming Logic and Design Fourth Edition, Introductory Chapter 2 Understanding Structure.
An Introduction to Programming with C++ Sixth Edition Chapter 8 More on the Repetition Structure.
Java Programming: Guided Learning with Early Objects Chapter 1 Basic Elements of Java.
C++ LANGUAGE MULTIPLE CHOICE QUESTION
CMPT 120 Topic: Python’s building blocks -> More Statements
Topics Designing a Program Input, Processing, and Output
Chapter 7: Expressions and Assignment Statements
Component 1.6.
REPETITION CONTROL STRUCTURE
Chapter 7: Expressions and Assignment Statements
Revision Lecture
Control Structures: Part 2
The Selection Structure
CSC113: Computer Programming (Theory = 03, Lab = 01)
JavaScript: Control Statements I
Arithmetic operations, decisions and looping
Chapter 7 Additional Control Structures
Faculty of Computer Science & Information System
Data Types and Expressions
Topics Designing a Program Input, Processing, and Output
ICT Gaming Lesson 2.
Topics Designing a Program Input, Processing, and Output
Data Types and Expressions
REPETITION Why Repetition?
Data Types and Expressions
Presentation 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. Decision Start/Stop Process Pre-defined Process I/O Connector

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 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 while

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

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