CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 6, Lecture 1 (Monday)

Slides:



Advertisements
Similar presentations
Introduction to Computing Science and Programming I
Advertisements

Introduction to C Programming
Computer programming Lecture 3. Lecture 3: Outline Program Looping [Kochan – chap.5] –The for Statement –Relational Operators –Nested for Loops –Increment.
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
CMPUT101 Introduction to Computing(c) Yngvi Bjornsson & Jia You1 Algorithm Discovery and Design Chapter 2 Topics: Representing Algorithms Algorithmic Problem.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
J. Michael Moore Structured Programming CPSC 110 Drawn from James Tam's material.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 3, Lecture 2.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
J. Michael Moore Structured Programming CSCE 110 Drawn from James Tam's material.
Introduction to Computers and Programming Lecture 8: More Loops New York University.
Chapter 5: Loops and Files.
Loops – While, Do, For Repetition Statements Introduction to Arrays
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
CS 1 Lesson 5 Loops and Files CS 1 -- John Cole.
CS241 PASCAL I - Control Structures1 PASCAL I - Control Structures Philip Fees CS241.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 6: Repetition  Some additional operators increment and decrement.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Repetition Statements.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
The switch Statement, DecimalFormat, and Introduction to Looping
Introduction to Computer Programming in c
Chapter 6 – Repetition Statements : Objectives After you have read and studied this chapter, you should be able to Implement repetition control in a program.
INTRODUCTION TO ALGORITHMS PROGRAMMING. Objectives Give a definition of the term algorithm Describe the various parts of the pseudocode algorithm or algorithm.
Chapter 2 - Algorithms and Design
Lecture 10: Reviews. Control Structures All C programs written in term of 3 control structures Sequence structures Programs executed sequentially by default.
CPS120 Introduction to Computer Science Iteration (Looping)
Programming Logic and Design Fifth Edition, Comprehensive
Programming Logic and Design Sixth Edition Chapter 5 Looping.
Chapter 2 - Algorithms and Design print Statement input Statement and Variables Assignment Statement if Statement Flowcharts Flow of Control Looping with.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 6, Lecture 1 (Monday)
1 Chapter 2 - Algorithms and Design print Statement input Statement and Variables Assignment Statement if Statement Flowcharts Flow of Control Looping.
PROBLEM SOLVING WITH LOOPS Chapter 7. Concept of Repetition Structure Logic It is a computer task, that is used for Repeating a series of instructions.
Logic Our programs will have to make decisions in terms of what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 8 - JavaScript: Control Structures I Outline 8.1 Introduction 8.2 Algorithms 8.3 Pseudocode 8.4.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 5, Lecture 1 (Monday)
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 3, Lecture 1.
Chapter 5: Control Structures: Iteration Visual Basic.NET Programming: From Problem Analysis to Program Design.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 10.
Introduction to Loops Iteration Repetition Counting Loops Also known as.
CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees.
Before we get started…. First, a few things… Weighted Grading System Programming Style Submitting your assignments… The char and string variable types.
Loops and Files. 5.1 The Increment and Decrement Operators.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
CSC 1010 Programming for All Lecture 4 Loops Some material based on material from Marty Stepp, Instructor, University of Washington.
Chapter 7 Problem Solving with Loops
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Count Controlled Loops (Nested) Ain’t no sunshine when she’s gone …
Iteration & Loop Statements 1 Iteration or Loop Statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
COMP Loop Statements Yi Hong May 21, 2015.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
PROGRAM CONTROL STRUCTURE
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
Lecture 07 More Repetition Richard Gesick.
Lecture 4B More Repetition Richard Gesick
CSS161: Fundamentals of Computing
Outline Altering flow of control Boolean expressions
MSIS 655 Advanced Business Applications Programming
Algorithm Discovery and Design
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Control Structures.
Presentation transcript:

CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 6, Lecture 1 (Monday)

TODAY Midterm exam will be Monday April 23 Still waiting for programs from homework #2 Go over remainder homework #2, as part of review for midterm exam –Formatting with write, writeln New material for today/this week/next week: –Selection. Making decisions. –Different types of choices: Yes/No choice. Either/or choice. Multiple choices. –Boolean expressions –Conditional statements in some detail –Nesting conditional statements

Homework #2 Review

Exercises 1.4, # 2 – 12 points Valid: –b) Payroll –c) Room222 –e) A –f) A1 –k) ListOfEmployees Invalid: –a) 7up –d) Name List –g) 1A –h) Time&Place –i) CONST –j) X*Y –l) Lima, Ohio

Exercises 1.4, # 4 – 3 points The three main sections of a Pascal program are: –Program heading –Declaration section –Executable section / program body You tell me what is in each part.

Exercises 1.4, # 6 – 6 points a.CONST Company : ‘General Motors’; VAR Salary : real; b.VAR Age = 25; c.VAR Days : integer; Ch : char; CONST Name = ‘John Smith’; Swap order

d.CONST Car : ‘Cadillac’; e.CONST Score : integer; f.VAR X, Y, Z : real; Score, Num : integer; This is okay!

Exercises 1.4, # 7 – 2 points Semicolon is a statement separator Semicolon is not needed before an END because END is not a statement A semicolon is also not needed before an ELSE (we’ll see this later) Stmt ; stmt ; stmt  separator (correct) Stmt ; stmt ; stmt ;  terminator (accepted)

Exercises 1.5, # 16 – 6 points What type of data is appropriate for each of the following: a.Your age – integer b.Your grade point average (GPA) – real c.Your name – string (not char) d.A test score – integer or real e.The average test score – real f.Your grade – char, integer, real, string

Part c) Following the example of calculating the mean that we used in class during Week 3 of the course, develop the problem statement, analysis, and design for a program that calculates the maximum of a set of numbers. You don't know how many numbers you will be given. You can assume that all numbers will be greater than 0. Use example from Week 3.1, Slide 29.

Maximum of N PROCESSING: Initialize Old to 0 P rompt for New number Read New while New is not equal to do: IF New > Old THEN Old := New Prompt for New Read New Write the value of New {New stores the maximum value} This is also an example of indefinite looping : You DO NOT know how many times you go around the loop until you finish. called a sentinel value because it guards the loop

Minimum of N (very similar to Maximum) PROCESSING: Initialize Old to MaxInt P rompt for New number Read New while New is not equal to do: IF New < Old THEN Old := New Prompt for New Read New Write the value of New {New stores the minimum value} This is also an example of indefinite looping : You DO NOT know how many times you go around the loop until you finish. called a sentinel value because it guards the loop

New Material

Types of Statements Input/Output : for user-program communication Assignment : to store (intermediate) results Control Structures: to determine which other statements are executed and when –conditional or selection statements : content is executed once, if at all –looping, repetition, or iteration statements : content is executed several times

What about computations? Computations are performed through expressions. Examples: –3 + X –a < b If you don’t use them in other statements you might as well not perform them –Assign them to variables with an assignment statement –Ouput them via output statements –Use them in control structures to determine what statements are executed.

Looping/Iteration/Repetition Statements Three terms for the same idea: Performing an action or a set of actions a number of times Fixed or definite repetition : –You know how many repetitions before you enter the loop –Also counter-controlled repetition –Pascal uses FOR loop –Example : printing out a rectangle Variable or indefinite repetition : –You don’t know how many repetitions you will do before you enter the loop –Sentinel-controlled loops –Pascal uses two types of loops: WHILE loop –Pretest loop : condition is tested at the top of the loop and therefore before ever entering the loop –Body of loop executed 0 or more times REPEAT loop –Postest loop : condition is tested at the top of the loop –Body of loop executed at least one time in order to reach the condition Examples of all of these in slides from Week 3.1. We will look at these in more detail later

Conditional / Selection Statements The basic idea, at the level of the algorithm, is that there is a choice to be made Three types of choices: 1.Yes/no choice. If yes, do something, otherwise do nothing 2.Either/or choice. A 2-way choice. Do something different in each case. 3.Multiple choice. An N-way chice. Do something different in each case. It can be expressed as a series of 2-way choices. Like most programming languages, Pascal provides a way of expressing these choices.

Combining Statements The various types of statements can be combined in many ways. In particular, control statements can be nested inside each other. Nesting is a visual/physical relationship but also a control relationship. –One loop can be performed inside another –A conditional statement can control whether a loop is executed –A conditional statement can be executed several times inside a loop When several statements are controlled by another statement we often use BEGIN and END to create compound statements

Example: Drawing Rectangles FOR I := 1 to Height BEGIN FOR J := 1 TO Width write(‘*’); writeln; END This FOR loop controls only the input/output statement “write(‘*’)”. This FOR loop controls a compound statement enclosed by the BEGIN and END. The inner FOR statement is nested inside the outer FOR statement.