CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees.

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 –
CS0004: Introduction to Programming Repetition – Do Loops.
Computer Science 1620 Loops.
ISBN Chapter 8 Statement-Level Control Structures.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 3, Lecture 2.
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.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
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 (!)
James Tam Loops In Pascal In this section of notes you will learn how to rerun parts of your program without having to duplicate the code.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
CS241 PASCAL I - Control Structures1 PASCAL I - Control Structures Philip Fees CS241.
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.
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.
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,
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)
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.
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.
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)
Midterm Exam Topics (Prof. Chang's section) CMSC 201.
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.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
Literals A literal (sometimes called a constant) is a symbol which evaluates to itself, i.e., it is what it appears to be. Examples: 5 int literal
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.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
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.
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
Outline Altering flow of control Boolean expressions
CS1100 Computational Engineering
Control Structures In Text: Chapter 8.
CSCI 3328 Object Oriented Programming in C# Chapter 4: C# Control Statement – Part I – Exercises UTPA – Fall 2012 This set of slides is revised from lecture.
3 Control Statements:.
CS 1428 Final Exam Review.
Chapter 6: Repetition Statements
C Programming Getting started Variables Basic C operators Conditionals
Flow of Control.
The structure of programming
REPETITION Why Repetition?
Presentation transcript:

CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees

CS241 PASCAL I - Control Structures2 Reserved Words Cannot be used for identifiers (names) of constant, type, variable, function, procedure

CS241 PASCAL I - Control Structures3 Basic Data Types l integer (-maxint <= integer <= maxint) l real ( , x 10 3, E3) l char (‘a’, ‘7’, ‘W‘,...) l string constant (const name = ‘Cyprus’;)

CS241 PASCAL I - Control Structures4 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 Structures5 Topics l Arithmetic Operators l Basic Debugging (tracing and print) l Basic Input l Constants l Standard Functions

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

CS241 PASCAL I - Control Structures7 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 Structures8 Constants and variables CONST pi = 3.14; VAR area, radius : real;... area := 3.14 * radius * radius;... area := pi * radius * radius;

CS241 PASCAL I - Control Structures9 Standard Functions 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 Structures10 Topics l Boolean Logic –Relational Operators –Boolean Expressions l IF... THEN... ELSE Statements l CASE Statements

CS241 PASCAL I - Control Structures11 Boolean Expression Relational Operators (=,, =, <>) used to build Boolean Expressions Logical Operators (AND, OR, NOT) used to build Compound Boolean Expressions

CS241 PASCAL I - Control Structures12 IF... THEN Statements l A decision-making statement l Syntax: IF THEN

CS241 PASCAL I - Control Structures13 Compound Statements l Treats a set of statements as one l Syntax: IF THEN BEGIN ;... ; END;

CS241 PASCAL I - Control Structures14 IF... THEN... ELSE Statements l Action to occur only if a boolean statement is false

CS241 PASCAL I - Control Structures15 Nested IF Statements l When the part of IF or ELSE is an IF THEN ELSE statement l Nested IF ELSE is considered as a single statement and doesn’t require BEGIN END

CS241 PASCAL I - Control Structures16 CASE Statement l Abbreviated IF THEN, ELSE IF THEN, etc statement l Protect case with IF THEN ELSE or use of OTHERWISE

CS241 PASCAL I - Control Structures17 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 Structures18 Topics Nested Selection No additional information available

CS241 PASCAL I - Control Structures19 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 Structures20 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 Structures21 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 Structures22 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 Structures23 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 Structures24 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 Structures25 While Loop l Syntax: WHILE DO l Exercise: pg. 264 #3, 4, 5

CS241 PASCAL I - Control Structures26 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 Structures27 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 Structures28 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 Structures29 Week 6 Topics l Nested Loops

CS241 PASCAL I - Control Structures30 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 Structures31 Week 7 & 8 Topics l Subprogramming l Procedures l Parameters l Side Effects

CS241 PASCAL I - Control Structures32 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 Structures33 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 Structures34 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 Structures35 Parameters l PROCEDURE ( ); l parameter list format: similar to VAR format l Discuss parameter notes slide

CS241 PASCAL I - Control Structures36 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 Structures37 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 Structures38 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.