CS241 PASCAL I - Control Structures1 PASCAL I - Control Structures Philip Fees CS241.

Slides:



Advertisements
Similar presentations
Statement-Level Control Structures
Advertisements

(8.1) COEN Control Structures  Control structure general issues  Compound statements  Selectors (conditional structures) – single – two-way –
ITEC113 Algorithms and Programming Techniques
Gary MarsdenSlide 1University of Cape Town Statements & Expressions Gary Marsden Semester 2 – 2000.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 4 – Introducing Algorithms, Pseudocode and.
Computer Science 1620 Loops.
ISBN Chapter 8 Statement-Level Control Structures.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
ISBN Chapter 8 Statement-Level Control Structures.
Program Design and Development
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
Statement-Level Control Structures Sections 1-4
VB .NET Programming Fundamentals
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
Imperative Programming
High-Level Programming Languages: C++
CIS Computer Programming Logic
Flow of Control. 2 Control Structures Control structure: An instruction that determines the order in which other instructions in a program are executed.
Lecture Set 5 Control Structures Part D - Repetition with Loops.
Chapter 12: How Long Can This Go On?
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Chapter 8 High-Level Programming Languages. 8-2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,
Flow of Control Part 1: Selection
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
INFORMATION TECHNOLOGY CSEC CXC 10/25/ PASCAL is a programming language named after the 17th century mathematician Blaise Pascal. Pascal provides.
CIS-165 C++ Programming I CIS-165 C++ Programming I Bergen Community College Prof. Faisal Aljamal.
Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using.
CPS120: Introduction to Computer Science Decision Making in Programs.
ISBN Chapter 8 Statement-Level Control Structures.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
Visual Basic Programming
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 6, Lecture 1 (Monday)
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
CONTENTS Processing structures and commands Control structures – Sequence Sequence – Selection Selection – Iteration Iteration Naming conventions – File.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 5, Lecture 1 (Monday)
1 st semester Basic Pascal Elements อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
JavaScript, Fourth Edition
Pascal Programming Today Chapter 11 1 Chapter 11.
Loops cause a section of a program to be repeated a certain number of times. The repetition continues while a condition remains true. When a condition.
CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
Controlling Program Flow with Decision Structures.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 4: Introduction to C: Control Flow.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
1 Fall 2009ACS-1903 Ch 4 Loops and Files while loop do-while loop for loop Other topics later on …
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
Information and Computer Sciences University of Hawaii, Manoa
Definition of the Programming Language CPRL
Fundamentals of PL/SQL part 2 (Basics)
Def: A control structure is a control statement and
8.1 Introduction - Levels of Control Flow: 1. Within expressions
The Selection Structure
Chapter 8: Control Structures
CS1100 Computational Engineering
Control Structures In Text: Chapter 8.
Chapter 6 Control Statements: Part 2
Chapter 6: Repetition Statements
Statement-Level Control Structures
C Programming Getting started Variables Basic C operators Conditionals
Flow of Control.
REPETITION Why Repetition?
Presentation transcript:

CS241 PASCAL I - Control Structures1 PASCAL I - Control Structures Philip Fees CS241

CS241 PASCAL I - Control Structures2 Week 1 Topics l Reserved Words l Syntax Diagrams l PASCAL Program l Basic Data Types l Basic Output

CS241 PASCAL I - Control Structures3 Reserved Words l See page l Identifiers that are part of the syntax of the language l Cannot be used for constant, type, variable, function, procedure identifier names

CS241 PASCAL I - Control Structures4 Syntax Diagrams l See page l Graphic representation of a language’s grammar l Identifier: [A-Za-z][A-Za-z0-9]*

CS241 PASCAL I - Control Structures5 PASCAL Program l See page 32 l What is the minimum PASCAL program?

CS241 PASCAL I - Control Structures6 Basic Data Types l integer (-maxint <= integer <= maxint) l real ( , x 10 3, E3) l char (‘a’, ‘7’, ‘ ‘) l String constant (const name = ‘sally’;)

CS241 PASCAL I - Control Structures7 Basic Output l write - output without newline terminator l writeln - output with newline terminator l writeln(expr1[:n[:n]], expr2[:n[:n]], …, exprN[:n[:n]]);

CS241 PASCAL I - Control Structures8 Exercises l Exercise #39, page 52

CS241 PASCAL I - Control Structures9 Week 2 Topics l Arithmetic Operators l Basic Debugging (tracing and print) l Basic Input l Constants l Standard Functions

CS241 PASCAL I - Control Structures10 Arithmetic Operators l ( ), *, MOD, DIV, /, +, - l What is an operator? l Precedence - evaluation sequence of multiple operator expressions l Typically left to right, but can be inside to outside or right to left.

CS241 PASCAL I - Control Structures11 Basic Input l Variables - name vs. memory location l Data pointer –location of next data item to be read from input stream l read - input next data item l readln - input next data item, skip to next line

CS241 PASCAL I - Control Structures12 Constants l Maintenance - descriptive name, one location updates CONST pi = 3.14; VAR area, radius : real;... area := 3.14 * radius * radius; vs. area := pi * radius * radius;

CS241 PASCAL I - Control Structures13 Standard Functions l pp. 92 & 96 l what is a function –perform some operation on argument(s) –returns value(s) as determined by operation l Example area := pi * sqr(radius);

CS241 PASCAL I - Control Structures14 Week 3 Topics l Boolean Logic –Relational Operators –Boolean Expressions l IF THEN ELSE Statements l CASE Statements

CS241 PASCAL I - Control Structures15 Boolean Expression l Relational Operators (=,, =, <>) used to build Boolean Expressions l Logical Operators (AND, OR, NOT) used to build Compound Boolean Expressions l Exercise: pp #16-22, #28-33

CS241 PASCAL I - Control Structures16 IF THEN Statements l A decision-making statement l Syntax: IF THEN l Review flow diagram, figure 5.1 pg. 179

CS241 PASCAL I - Control Structures17 Compound Statements l Treats a set of statements as one l Good programming practice even for single statement. Why? l Syntax: IF THEN BEGIN ;... ; END;

CS241 PASCAL I - Control Structures18 IF THEN ELSE Statements l Action to occur only if boolean statement is false l Review flow diagram, figure 5.2 pg. 187

CS241 PASCAL I - Control Structures19 Nested IF Statements l When the part of IF or ELSE is an IF THEN ELSE statement l Nested IF ELSE is considered a single statement and doesn’t require BEGIN END l Review Example 5.13 on page 197 l WARNING: ensure ELSE matches to correct IF

CS241 PASCAL I - Control Structures20 CASE Statement l Abbreviated IF THEN, ELSE IF THEN, etc statement l Review flow diagram, figure 5.4 pg. 209 and CASE rules on pg. 210 l protect case with IF THEN ELSE or use of OTHERWISE

CS241 PASCAL I - Control Structures21 Exercises l Modify LAB #2 to improve the display l New features –If the coefficient is 1 don’t display the 1: 1x + 2y = 5 becomes x + 2y = 5 –if the coefficient is 0 don’t display: 0x + 5y = 5 becomes 5y = 5 –display subtraction 2x + -1y becomes 2x - y = 0 –give error if division by 0 would occur

CS241 PASCAL I - Control Structures22 Week 4 Topics Nested Selection No additional information available

CS241 PASCAL I - Control Structures23 Week 5 Topics l Repetition Problem l Conditional Branch l Pre-testing (Top-testing) Loops l Post-testing (Bottom-testing) Loops l Comparing Loops

CS241 PASCAL I - Control Structures24 Repetition Problem l A class of problems that can be solved by repeatedly performing some task until some condition is no longer valid. l Example 1: Monopoly, “Go to Jail” until you roll doubles, 3 rolls, or pay $50. l Example 2: N ! = 1 * 2 * 3 *... (N-1) * N l Iterations could be written as sequence of steps - # of iterations may vary.

CS241 PASCAL I - Control Structures25 Conditional Branch l Predates looping constructs label: statement1; statement2;... statementN; if ( boolean expression is true) goto label; l Exercise: Write N! using conditional branch logic

CS241 PASCAL I - Control Structures26 Loop Terminology l initialization - assign values before evaluation l evaluate - determine if loop should continue l increment - modify or increase loop controls l iteration - one pass over loop (evaluate, body, increment) l loop body - syntax that is executed with each iteration of loop

CS241 PASCAL I - Control Structures27 Pre-testing (Top-testing) Loops l Evaluates looping condition before executing body of loop l Body of loop executes a minimum of 0 times l Pascal has FOR and WHILE loops l FOR loop (pg. 239) –based on FORTRAN FOR loop –used for fixed increment looping l WHILE loop (pg. 252) –General purpose top-testing loop

CS241 PASCAL I - Control Structures28 For Loop l Syntax: FOR := [TO | DOWNTO] DO l Good practice to have maximum iteration value defined as a CONST or VAR (i.e., don’t hard code). l Loop control variable may or may not be defined as VAR l Exercise: pg

CS241 PASCAL I - Control Structures29 While Loop l Syntax: WHILE DO l Exercise: pg. 264 #3, 4, 5

CS241 PASCAL I - Control Structures30 Post-testing (Bottom-testing) Loops l Evaluates looping condition after executing body of loop l Body of loop executes a minimum of 1 times l Syntax: REPEAT UNTIL l Exercise: pg. 272 #3, 4, 5.

CS241 PASCAL I - Control Structures31 Comparing Loops l Each loop can be rewritten in another loop’s syntax l Easier to use similar testing loops (i.e. for and while) l Review Example 6.22, and 6.23 on pg 275

CS241 PASCAL I - Control Structures32 Exercises l Write one of the following: –pg. 312 # 12 (easier) NOTE: read term from keyboard instead of file –pg. 310 # 6a (moderate) –pg. 311 #8 (harder)

CS241 PASCAL I - Control Structures33 Week 6 Topics l Nested Loops

CS241 PASCAL I - Control Structures34 Examples l pg. 285 l Example 6.26 pg. 285 l Example 6.30 pg. 291 l Program Problem 6 b pg. 310

CS241 PASCAL I - Control Structures35 Week 7 & 8 Topics l Subprogramming l Procedures l Parameters l Side Effects

CS241 PASCAL I - Control Structures36 Subprogramming l Modular - readability, exchange (swap), and fix parts l Subtasks - repetitive execution of sub functionality l Structured - receive values, perform task, return result l Black Box design - Lego building blocks

CS241 PASCAL I - Control Structures37 Procedures l Design to perform small discreet task l Thought of as a small program - test as such l Program is a collection/series of procedure (function) calls l Discuss procedure format slide (pg. 321)

CS241 PASCAL I - Control Structures38 Procedures (cont.) l Placed in declaration section of program/procedure/function l use { } to denote procedure boundary l procedure name unique to program (scope)

CS241 PASCAL I - Control Structures39 Procedure Execution l a procedure is called or invoked by name l flow of control jumps to first line in procedure l on completing procedure, flow of control returns to first line after procedure call l walk through exercise 11 pg. 335

CS241 PASCAL I - Control Structures40 Parameters l PROCEDURE ( ); l parameter list format: similar to VAR format l Discuss parameter notes slide

CS241 PASCAL I - Control Structures41 Parameter Types l formal - variables in procedure heading (parameters) l actual - values in procedure call (arguments) l call by value (value parameters) arguments are copies to parameters l call by reference (variable parameters) parameters refer to arguments

CS241 PASCAL I - Control Structures42 Side Effects l Unintended change in a variable l Typically associated with call by reference parameter passing l Considered “bad” programming. Why? l Sometimes desirable. When/Example?

CS241 PASCAL I - Control Structures43 Exercise l Walk through program on pg. 349 l Do prime program in class #2, pg. 372 l Lab: #3 pg. 372, #8 pg. 373, prime program above.