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

Slides:



Advertisements
Similar presentations
Chapter 3 Program Design And Branching Structures.
Advertisements

3. S/E with Control Structures 3.1 Relational Operators and Expressions 3.2 If and if-else Statements 3.3 The Type Double 3.4 Program Design with the While.
Selection (decision) control structure Learning objective
Pascal Programming Today Chapter 4 1 »Conditional statements allow the execution of one of a number of possible operations. »Conditional statements include:
Control Structures Control structures are used to manage the order in which statements in computer programs will be executed Three different approaches.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 3, Lecture 2.
1 9/24/07CS150 Introduction to Computer Science 1 Relational Operators and the If Statement.
Introduction to Computers and Programming Lecture 5 New York University.
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
Program Design and Development
1 9/20/06CS150 Introduction to Computer Science 1 Review: Exam 1.
COMP 14 Introduction to Programming Miguel A. Otaduy May 18, 2004.
Introduction to Computers and Programming Lecture 5 Boolean type; if statement Professor: Evan Korth New York University.
Control Structures: Part 2. Introduction Essentials of Counter-Controlled Repetition For / Next Repetition Structure Examples Using the For / Next Structure.
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
CS241 PASCAL I - Control Structures1 PASCAL I - Control Structures Philip Fees CS241.
Visual C++ Programming: Concepts and Projects
Programming Logic and Design, Introductory, Fourth Edition1 Understanding Computer Components and Operations (continued) A program must be free of syntax.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 30, 2005.
Basic Elements of C++ Chapter 2.
Fundamentals of Python: From First Programs Through Data Structures
19/5/2015CS150 Introduction to Computer Science 1 Announcements  1st Assignment due next Monday, Sep 15, 2003  1st Exam next Friday, Sep 19, 2003  1st.
Fundamentals of Python: First Programs
The Data Element. 2 Data type: A description of the set of values and the basic set of operations that can be applied to values of the type. Strong typing:
CIS Computer Programming Logic
1 The CONST definition CONST Pi = , City = ‘New York’; Constant identifiers are used when you do not want the value of an identifier to change why.
2440: 211 Interactive Web Programming Expressions & Operators.
Chapter 2 - Algorithms and Design
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
1 Conditional statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
2 Objectives You should be able to describe: Relational Expressions Relational Expressions The if-else Statement The if-else Statement Nested if Statements.
Chapter 8 High-Level Programming Languages. 8-2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,
UniMAP Sem1-07/08EKT120: Computer Programming1 Week2.
Pascal language Slides of Omar Al-Nahal. Components of Pascal Language Components of Pascal Language 1. Pascal Character set: - English Letters. - Decimal.
Selection Boolean What is Boolean ? Boolean is a set with only two values : –true –false true and false are standard identifiers in Pascal, called Boolean.
INFORMATION TECHNOLOGY CSEC CXC 10/25/ PASCAL is a programming language named after the 17th century mathematician Blaise Pascal. Pascal provides.
C++ Programming: Basic Elements of C++.
CIS-165 C++ Programming I CIS-165 C++ Programming I Bergen Community College Prof. Faisal Aljamal.
Property of Jack Wilson, Cerritos College1 CIS Computer Programming Logic Programming Concepts Overview prepared by Jack Wilson Cerritos College.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
VBScript Language. What is VBScript Based on the Visual Basic family of languages Supports object oriented features Interpreted Loosely Typed Implicitly.
Control Structures (A) Topics to cover here: Introduction to Control Structures in the algorithmic language Sequencing.
Programming Fundamental Slides1 Data Types, Identifiers, and Expressions Topics to cover here: Data types Variables and Identifiers Arithmetic and Logical.
1 Chapter 2 - Algorithms and Design print Statement input Statement and Variables Assignment Statement if Statement Flowcharts Flow of Control Looping.
1 09/15/04CS150 Introduction to Computer Science 1 Life is Full of Alternatives Part 2.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 5, Lecture 1 (Monday)
Programming, an introduction to Pascal
1 st semester Basic Pascal Elements อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees.
CS 240 – Computer Programming I Lab Kalpa Gunaratna –
Fundamental Programming Fundamental Programming More Expressions and Data Types.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 5, Lecture 2 (Tuesday)
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 6, Lecture 1 (Monday)
CPS120: Introduction to Computer Science Decision Making in Programs.
CMP 131 Introduction to Computer Programming
UNIMAP Sem2-07/08EKT120: Computer Programming1 Week 2 – Introduction to Programming.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Lecture #8 SWITCH STATEMENT By Shahid Naseem (Lecturer)
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Data Types, Identifiers, and Expressions
The Selection Structure
Data Types, Identifiers, and Expressions
Programming Funamental slides
` Structured Programming & Flowchart
Selection Control Structure
Chapter 2 Programming Basics.
Life is Full of Alternatives
COMPUTING.
Presentation transcript:

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

TODAY Flow of control and flow charts Conditional statements Boolean expressions Go over Homework #2 programming part

TOMORROW (Lab) Exercises on the computer with I/O and simple conditional statements. Review of topics for Midterm Exam

Syntax Graphs, Flowcharts, & Structure Charts Syntax graph –To define the language syntax Structure chart –To show the relationship between program modules and the data transfer between the modules (Example)Example CONST = Identifier ; value Compute mean of 3 numbers Read 3 numbers Compute mean of inputs Display mean to screen

Flowchart –A form of visual control-flow specification employing arrows and balloons of various shapes. –Shows flow of actions (algorithm) in a program

How the locus (location) of execution changes as the program executes. Example. Let Height = 3 and Width = 5 FOR I := 1 to Height BEGIN FOR J := 1 TO Width DO write(‘*’); writeln; END What is Flow of Control?

007… 008I := 1 009IF I > 3 GOTO J := 1 011IF J > 5 GOTO write(‘*’) 013J := J GOTO writeln 016I := I GOTO … DATA MEMORY I = ? J = ?

007… 008I := 1 009IF I > 3 GOTO J := 1 011IF J > 5 GOTO write(‘*’) 013J := J GOTO writeln 016I := I GOTO … DATA MEMORY I = 1 J = ?

007… 008I := 1 009IF I > 3 GOTO J := 1 011IF J > 5 GOTO write(‘*’) 013J := J GOTO writeln 016I := I GOTO … DATA MEMORY I = 1 J = ?

007… 008I := 1 009IF I > 3 GOTO J := 1 011IF J > 5 GOTO write(‘*’) 013J := J GOTO writeln 016I := I GOTO … DATA MEMORY I = 1 J = 1

007… 008I := 1 009IF I > 3 GOTO J := 1 011IF J > 5 GOTO write(‘*’) 013J := J GOTO writeln 016I := I GOTO … DATA MEMORY I = 1 J = 1

007… 008I := 1 009IF I > 3 GOTO J := 1 011IF J > 5 GOTO write(‘*’) 013J := J GOTO writeln 016I := I GOTO … DATA MEMORY I = 1 J = 1

007… 008I := 1 009IF I > 3 GOTO J := 1 011IF J > 5 GOTO write(‘*’) 013J := J GOTO writeln 016I := I GOTO … DATA MEMORY I = 1 J = 2

007… 008I := 1 009IF I > 3 GOTO J := 1 011IF J > 5 GOTO write(‘*’) 013J := J GOTO writeln 016I := I GOTO … DATA MEMORY I = 1 J = 2

007… 008I := 1 009IF I > 3 GOTO J := 1 011IF J > 5 GOTO write(‘*’) 013J := J GOTO writeln 016I := I GOTO … DATA MEMORY I = 1 J = 2

A few steps later …

007… 008I := 1 009IF I > 3 GOTO J := 1 011IF J > 5 GOTO write(‘*’) 013J := J GOTO writeln 016I := I GOTO … DATA MEMORY I = 1 J = 6

007… 008I := 1 009IF I > 3 GOTO J := 1 011IF J > 5 GOTO write(‘*’) 013J := J GOTO writeln 016I := I GOTO … DATA MEMORY I = 1 J = 6

007… 008I := 1 009IF I > 3 GOTO J := 1 011IF J > 5 GOTO write(‘*’) 013J := J GOTO writeln 016I := I GOTO … DATA MEMORY I = 1 J = 6

007… 008I := 1 009IF I > 3 GOTO J := 1 011IF J > 5 GOTO write(‘*’) 013J := J GOTO writeln 016I := I GOTO … DATA MEMORY I = 2 J = 6

007… 008I := 1 009IF I > 3 GOTO J := 1 011IF J > 5 GOTO write(‘*’) 013J := J GOTO writeln 016I := I GOTO … DATA MEMORY I = 2 J = 6

007… 008I := 1 009IF I > 3 GOTO J := 1 011IF J > 5 GOTO write(‘*’) 013J := J GOTO writeln 016I := I GOTO … DATA MEMORY I = 2 J = 6

007… 008I := 1 009IF I > 3 GOTO J := 1 011IF J > 5 GOTO write(‘*’) 013J := J GOTO writeln 016I := I GOTO … DATA MEMORY I = 2 J = 1

007… 008I := 1 009IF I > 3 GOTO J := 1 011IF J > 5 GOTO write(‘*’) 013J := J GOTO writeln 016I := I GOTO … DATA MEMORY I = 2 J = 1

A few steps later …

007… 008I := 1 009IF I > 3 GOTO J := 1 011IF J > 5 GOTO write(‘*’) 013J := J GOTO writeln 016I := I GOTO … DATA MEMORY I = 3 J = 6

007… 008I := 1 009IF I > 3 GOTO J := 1 011IF J > 5 GOTO write(‘*’) 013J := J GOTO writeln 016I := I GOTO … DATA MEMORY I = 4 J = 6

007… 008I := 1 009IF I > 3 GOTO J := 1 011IF J > 5 GOTO write(‘*’) 013J := J GOTO writeln 016I := I GOTO … DATA MEMORY I = 4 J = 6

007… 008I := 1 009IF I > 3 GOTO J := 1 011IF J > 5 GOTO write(‘*’) 013J := J GOTO writeln 016I := I GOTO … DATA MEMORY I = 4 J = 6

Flow of Control You can show it by animation for the simplest programs or You can show it much more succinctly with Flow Charts A graphical formalism for showing how control flows through a program Standard Flowchart Symbols (later) from icmis/oakman/outline/chap05/slides/symbo ls.htm

Flowchart Symbols

Rectangle Flow Chart I := 1 write(‘*’)J := 1 J := J+1 I := I+1 I > 3 J > 5 writeln no yes no yes BEGIN END

Yes/No Choice The logic: IF some condition or test is TRUE THEN perform some action [ ELSE do nothing ] Examples: –If the car is running out of gas, put in gasoline. –If you are failing your class and you cannot drop it, study more. –If someone says hello and you don’t want to seem impolite, say hello back.

Yes/No Choice: The IF-THEN Statement Syntax: IF THEN Syntax graph: Flowchart: IF condition THEN statement statement 1 condition yes no statement 3statement 2

Either/Or Choice The logic: IF some condition or test is TRUE THEN perform some action ELSE perform a different action Examples: –If the weather is hot wear light clothes, else wear warm clothes. –If a number is odd increment OddCount else increment EvenCount –If a noun starts with a capital letter categorize it as a proper noun (name) else as a common noun.

Either/Or Choice : The IF-THEN-ELSE Statement Syntax: IF THEN ELSE Syntax graph: Flowchart: IF condition THEN statement ELSE statement statement 1 condition yes statement 4 statement 2 no statement 3

What is ? A single statement: –An input or output statement –An assignment –Another IF … THEN … ELSE statement –A looping statement (FOR, WHILE, REPEAT) A compound statement: BEGIN ; ; …. END

What is ? A boolean expression Named after the mathematician George Boole who invented logic. Boolean expressions –Evaluate to TRUE or FALSE –Include different types of operands different types of operators

Boolean Expressions Boolean constants –TRUE –FALSE Boolean variables –Declared as type “boolean” CONST Debug = TRUE; { or FALSE } VAR SaveDebug : BOOLEAN;

Relational expressions: Arithmetic, character, or strings acted upon by relational operators: –Relational operators are: <=less than or equal to <less than >=greater than or equal to >greater than =equal <>not equal –Relational operators compare two compatible values Two characters Two strings Real and integer Two Booleans –Relational operators return a Boolean value

Examples: new < old ‘B’ < ‘A’ value <> < 4 MiddleInitial = ‘L’ 1.0 = 1.0/ 3.0 * 3.0 { Implementation dependent}

Logical expressions: Boolean values acted upon by logical operators –Logical operators are: AND OR NOT –Logical operators take Boolean operands and return Boolean values. –Examples: IF NeedMoney AND (NOT HaveMoneyInHand) AND HAVE MoneyInBank THEN {Go get money at the bank} ELSE {Ask parents for money} IF Value MaxValue THEN writeln (‘Value ‘, Value, ‘ is out of range.’)

Truth Tables for Boolean Operators Op1Op2Op1 AND Op2Op1 OR Op2NOT Op1 truetruetruetruefalse truefalsefalsetruefalse falsetruefalsetruetrue falsefalsefalsefalsetrue binary operators unary operator

Operator Precedence: Arithmetic, Relational and Logical OperatorPrecedence ( )parenthesesHighest (evaluated first) - + NOT (unary operators) * / DIV MOD AND + - OR >= > Lowest (evaluated last)

(4.2 >= 5.0) AND (8 = (3 + 5)) (4.2 >= 5.0) OR (8 = (3 + 5)) writeln('Value of (-2 = 10) is : ', (-2 = 10) ); writeln('Value of (-2 = 10) is : ', (-2 = 10) ); writeln('Value of (3 > 5) AND (14.1 = 0.0) is : ', (3 > 5) AND (14.1 = 0.0) ); writeln('Value of (3 > 5) OR (14.1 = 0.0) is : ', (3 > 5) OR (14.1 = 0.0) ); writeln('Value of NOT (18 = ) is : ', NOT (18 = ) ); writeln('Value of NOT (-4 > 0) is : ', NOT (-4 > 0) ); writeln('Value of ''a'' < ''b'' is : ', 'a' < 'b' ); writeln('Value of ''a'' < ''A'' is : ', 'a' < 'A' );

Summary of Operations on Boolean Values Logical operations (AND, OR, NOT) –Relational operators act on non-Boolean values but return Boolean values Assignment VAR Debug, SaveDebug : BOOLEAN; Debug := TRUE; SaveDebug := Debug; {Save the value of Debug} Output (in Turbo Pascal) writeln (‘The value of Debug is ‘, Debug); (Not Input – can’t read/readln a Boolean value)

Boolean expressions Appear primarily in control structures Used to determine the sequence in which Pascal statement are executed. Boolean variables are often called Boolean ‘flags’ : var Debug, SaveDebug : Boolean; … BEGIN … SaveDebug := Debug; Debug := TRUE; IF Debug THEN {print out the value of several variables} Debug := SaveDebug;... END

Example Expressions See program BOOLEXPR.PAS

Go Over Homework #2, Programming Part

Exercises 1.5, #13 Write a complete program that produces the following table: WIDTH LENGTH AREA

Task Decomposition (1) Write table header: WIDTH LENGTH AREA Write table contents: Simplest code writeln(‘WIDTH LENGTH AREA’); writeln(‘ 4 2 8’); writeln(‘ ’);

Using formatting Assume table header starts in first column 1: Write header as –WIDTH, 8 spaces, LENGTH, 8 spaces, AREA Write columns of numbers as –First number fits in field of width 3 –Second number fits in field = 14 –Third column fits in field of = 14 Code will be writeln(‘WIDTH’,‘ ’:8,‘LENGTH’,‘ ’:8,‘AREA’); writeln(4:3,2:14,8:14); writeln(21:3,5:14,105:14);

Exercises 1.5, #8 Assume the hourly wages of five students are: 3.65, 4.10, 2.89, 5.00, Write a program that produces the following output, where the “E” in Employees is in column 20.

Desired Output Employees Hourly Wage $ $ $ $ $

Task Decomposition (1) Write table header: Employees Hourly Wage Write table contents: 1 $ 3.65 …. Write table footer:

Task Decomposition (2) Write table header: –Write line starting at column 20 –Write column headings starting at column 20 –Write line starting at column Employees Hourly Wage Write table contents: –Write 5 lines each with: Employee number starting at column 23 Employee wage starting at column 36 1 $ 3.65 …. Write table footer: –Write line starting at column

Task Decomposition (2) Write table header: –Write line starting at column 20 –Write column headings starting at column 20 –Write line starting at column Employees Hourly Wage Write table contents: –Write 5 lines each with: Employee number starting at column 23 Employee wage starting at column 36 1 $ 3.65 …. Write table footer: –Write line starting at column

Task Decomposition (3) Write table header: –Write line starting at column 20 –Write column headings starting at column 20 –Write line starting at column Employees Hourly Wage Pascal code: –CONST Line = ‘ ’ –BEGIN writeln(‘ ’:19,Line); writeln(‘ ’:19,‘Employee’,‘ ’:5,‘Hourly Wage’); writeln(‘ ’:19,Line); … END.

Task Decomposition (4) Write table contents: –Write 5 lines each with: Employee number starting at column 23 Employee wage starting at column 36 1 $ 3.65 …. Pascal code: –BEGIN … writeln(1:23,‘$’:13,3.65:5:2); writeln(2:23,‘$’:13,4.10:5:2); writeln(3:23,‘$’:13,2.89:5:2); writeln(4:23,‘$’:13,5.00:5:2); writeln(5:23,‘$’:13,4.50:5:2); … END.

Task Decomposition (5) Write table footer: –Write line starting at column Pascal code: –CONST Line = ‘ ’ –BEGIN... writeln(‘ ’:19,Line); END.