J. Michael Moore Structured Programming CSCE 110 Drawn from James Tam's material.

Slides:



Advertisements
Similar presentations
1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
Advertisements

Pascal Programming Today Chapter 4 1 »Conditional statements allow the execution of one of a number of possible operations. »Conditional statements include:
James Tam Making Decisions In Pascal In this section of notes you will learn how to have your Pascal programs choose between alternative courses of action.
James Tam Repetition In Pascal In this section of notes you will learn how to have a section of code repeated without duplicating the code.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
James Tam Making Decisions In Python In this section of notes you will learn how to have your Pascal programs choose between alternative courses of action.
James Tam Making Decisions In Pascal In this section of notes you will learn how to have your Pascal programs choose between alternative courses of action.
James Tam Making Decisions In Pascal In this section of notes you will learn how to have your Pascal programs choose between alternative courses of action.
James Tam Making Decisions In Pascal In this section of notes you will learn how to have your Pascal programs choose between alternative courses of action.
James Tam Repetition In Pascal In this section of notes you will learn how to rerun parts of your program without having to duplicate the code.
James Tam Loops In Python In this section of notes you will learn how to rerun parts of your program without having to duplicate the code.
J. Michael Moore Structured Programming CPSC 110 Drawn from James Tam's material.
James Tam Loops In Python In this section of notes you will learn how to rerun parts of your program without having to duplicate the code.
J. Michael Moore Other Control Structures CSCE 110 Influenced by material developed by James Tam & Jennifer Welch.
James Tam Loops In Python In this section of notes you will learn how to rerun parts of your program without having to duplicate the code.
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.
Basic Elements of Programming A VB program is built from statements, statements from expressions, expressions from operators and operands, and operands.
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.
Chapter 8 (Control Structure) Slide 1 Control Structures Control structures are used by the programmer to incorporate the desired sequence of execution.
J. Michael Moore Logic and Boolean Expressions CSCE 110.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
true (any other value but zero) false (zero) expression Statement 2
James Tam Loops In Pascal In this section of notes you will learn how to rerun parts of your program without having to duplicate your code.
Programming: Part II In this section of notes you will learn more advanced programming concepts such as branching and repetition as well as how to work.
J. Michael Moore Other Control Structures CSCE 110 Influenced by material developed by James Tam & Jennifer Welch.
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.
James Tam Making Decisions In Pascal In this section of notes you will learn how to have your Pascal programs choose between alternative courses of action.
Making Decisions In Python
James Tam Making Decisions In Pascal In this section of notes you will learn how to have your Pascal programs choose between alternative courses of action.
James Tam Making Decisions In Pascal In this section of notes you will learn how to have your Pascal programs choose between alternative courses of action.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
The switch Statement, DecimalFormat, and Introduction to Looping
Fundamentals of Python: From First Programs Through Data Structures
Fundamentals of Python: First Programs
CONTROL STATEMENTS IF-ELSE, SWITCH- CASE Introduction to Computer Science I - COMP 1005, 1405 Instructor : Behnam Hajian
6/3/2016 CSI Chapter 02 1 Introduction of Flow of Control There are times when you need to vary the way your program executes based on given input.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
1 2. Program Construction in Java. 2.4 Selection (decisions)
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION.
Control Structures - Selections - Repetitions/iterations (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
Flow of Control Chapter 3. Outline Branching Statements Java Loop Statements Programming with Loops The Type boolean.
CPS120 Introduction to Computer Science Iteration (Looping)
James Tam Making Decisions In Python In this section of notes you will learn how to have your programs choose between alternative courses of action.
CSCI 161 Lecture 7 Martin van Bommel. Control Statements Statements that affect the sequence of execution of other statements Normal is sequential May.
CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
James Tam Making Decisions In Python In this section of notes you will learn how to have your programs choose between alternative courses of action.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Flow of Control: Loops Module 4. Objectives Design a loop Use while, do, and for in a program Use the for-each with enumerations Use assertion checks.
COMP Loop Statements Yi Hong May 21, 2015.
Flow of Control Joe McCarthy CSS 161: Fundamentals of Computing1.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
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.
Catie Welsh February 9,  Friday - No Lab! ◦ Bring questions on Project 2  Lab 3 due on Friday 2.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
James Tam Loops In Python In this section of notes you will learn how to rerun parts of your program without having to duplicate the code.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Condition – any expression that evaluates to true/false value Relational operators are BINARY.
REPETITION CONTROL STRUCTURE
Loops In Pascal In this section of notes you will learn how to rerun parts of your program without having to duplicate the code. Repetition in Pascal:
The switch Statement, and Introduction to Looping
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
Iteration: Beyond the Basic PERFORM
Iteration: Beyond the Basic PERFORM
Chapter 7 Conditional Statements
Simple Branches and Loops
Presentation transcript:

J. Michael Moore Structured Programming CSCE 110 Drawn from James Tam's material

J. Michael Moore Programming Structure Sequence Branching / Selection Looping / Iteration

J. Michael Moore Sequence Programs are executed one statement at a time. A compound statement encapsulates a sequence of statements as a single statement.

J. Michael Moore Compound Statement begin statement1; statement2;. end;

J. Michael Moore High Level View Of Decision Making For The Computer Is income below $10,000? False Is income between $10K - $20K? True Income tax = 20% False etc. True Nominal income deduction Eligible for social assistance

J. Michael Moore Decision-Making In Pascal Decisions are questions with answers that are either true or false (Boolean) e.g., Is it true that the variable 'x' is positive? The program branches one way or another depending upon the answer to the question.

J. Michael Moore If-Then Question? Execute a statement True False Remainder of the program

J. Michael Moore Boolean expression Decision-making: checking if a particular condition is true Format: if (boolean-expression) then statement; additional statements; Example: if (age >= 18) then writeln('You are an adult'); writeln('Tell me more about yourself'); If-Then Indicates another statement will follow this statement

J. Michael Moore Allowable Operands For Boolean Expressions Boolean-expression: operand relational-operator operand OR boolean-value Can also stand alone as a Boolean expression (it is a boolean value) integer real boolean char const Operands:

J. Michael Moore Relational Operators For Boolean Expressions If (operand relational-operator operand) then Pascal Mathematical operator equivalent Meaning < < Less than > > Greater than = = Equal to <= ≤ Less than or equal to >= ≥ Greater than or equal to <> ≠ Not equal to

J. Michael Moore Statement / Body Body of if-then consists of a single statement Format: if (Boolean expression) then s1; s2; Example: if (x = 1) then writeln('Body of if'); writeln ('After body'); If-Then (Simple Statement) Indicates another statement will follow this statement. (Note: semicolon is not needed if a statement does not follow.)

J. Michael Moore Body / Compound Statement Body of if-then consists of multiple statements Format: if (Boolean expression) then begin s1; s2; : sn; end; sn+1; If-Then (Compound Statement) Indicates another statement will follow this statement

J. Michael Moore If-Then (Compound Statement) Example: taxRate := 0.2; if (income < 10000) then begin writeln('Eligable for social assistance'); taxCredit = 100; end; tax = income * taxRate ;

J. Michael Moore If-Then: What Gets Executed When true, the if executes the body which is a single statement. Simple statement: the body follows the ' then ' and precedes the first semi-colon. Compound statement: the body is enclosed within the begin - end pair as a compound statement.

J. Michael Moore If-Then-Else Question? Execute a statement True False Execute a statement Remainder of the program

J. Michael Moore Decision-making with two conditions (true or false) Format: if (boolean-expression) then body of 'if' else body of 'else'; additional statements; If-Then-Else No semi-colon so the next word does not start a new statement!!! Semi-colon indicates another statement will follow this statement, i.e. this statement is finished. Note: If no statement follows this semi-colon, it is not needed.

J. Michael Moore If-Then-Else Example: if (age >= 18) then writeln('Adult') else writeln('Not an adult'); writeln('Tell me more about yourself');

J. Michael Moore If-Then-Else (Simple Statement) Body of if-then-else consists of a single statement Format: if (Boolean expression) then s1 else s2; s3; No semi-colon so the next word does not start a new statement!!! Another statement will follow this statement, i.e. this statement is finished.

J. Michael Moore If-Then-Else (Simple Statement) Example: if (x = 1) then writeln('body of if') else writeln('body of else'); writeln('after if-then-else');

J. Michael Moore If-Then-Else (Compound Statement) if (boolean-expression) then begin s1; : sn; end else begin sn+1; : sn + m; end; sn + m + 1; No semi-colon (not the end of if statement!) Semi-colon ( another statement will follow this statement, i.e. this statement is finished ! – optional if no statement follows)

J. Michael Moore If-Then (Compound Statement) Example: if (income < 10000) then begin writeln('Eligible for social assistance'); taxRate = 0.1; end else begin writeln('Not eligible for social assistance'); taxRate = 0.2; end; tax = income * taxRate;

J. Michael Moore Quick Summary: If Vs. If-Else If: –Evaluates a Boolean expression (asks a question) –If the expression evaluates to true then execute the 'body' of the if. –If the expression evaluates to false then no additional statements are executed. –Use when your program evaluates a Boolean expression and code will be executed only when the expression evaluates to true.

J. Michael Moore Quick Summary: If Vs. If-Else If-else: –Evaluates a Boolean expression (asks a question) –If the expression evaluates to true then execute the 'body' of the if. –If the expression evaluates to false then execute the 'body' of the else. –Use when your program evaluates a Boolean expression and different code will execute if the expression evaluates to true than if the expression evaluates to false.

J. Michael Moore Nested Decision Making Decision making is dependent. The first decision must evaluate to true before the successive decisions are even considered for evaluation. Questio n 1? True Questio n 2? True Statement Remainder of the program False

J. Michael Moore Outer body Inner body Nested Decision Making One decision is made inside another Outer decisions must evaluate to true before inner decisions are even considered for evaluation. Format: if (Boolean expression) then inner body Example: if (income < 10000) then if (citizen = true) then writeln('Eligable for social assistance'); tax = income * TAX_RATE;

J. Michael Moore Is this for x or y???? if (x > 0) then if (y > 0) then writeln('x and y greater than zero') else writeln('x is greater than zero'); Nested Decision Making: The Dangling Else

J. Michael Moore The Dangling Else Reformatted if (x > 0) then if (y > 0) then writeln('x and y greater than zero') else writeln('x greater than zero');

J. Michael Moore Decision-Making With Multiple Alternatives if-then –Checks a condition and executes the body if the condition is true if-then-else –Checks a condition and executes one body if the condition is true and another body if the condition is false Approaches for multiple (two or more) alternatives –Multiple if's –Multiple else-if's

J. Michael Moore Decision Making With Multiple If's Question ? True Statement True Statement Question ? Remainder of the program False

J. Michael Moore Multiple If's: Non-Exclusive Conditions Any, all or none of the conditions may be true (independent) Format: if (Boolean expression 1) then body 1; if (Boolean expression 2) then body 2; : statements after the conditions;

J. Michael Moore Multiple If's: Non-Exclusive Conditions (Example) Example: if (x > 0) then writeln('X is positive'); if (y > 0) then writeln('Y is positive'); if (z > 0) then writeln('Z is positive');

J. Michael Moore Multiple If's: Mutually Exclusive Conditions At most only one of many conditions can be true Can be implemented through multiple if's if (gpa = 4) then letter := 'A'; if (gpa = 3) then letter := 'B'; if (gpa = 2) then letter := 'C'; if (gpa = 1) then letter := 'D'; if (gpa = 0) then letter := 'F'; Inefficient combination!

Decision Making With If, Else-If Question ? True Statement False Question ? Remainder of the program Statement False True Statement

J. Michael Moore Multiple If, Else-If's: Mutually Exclusive Conditions Format: if (Boolean expression 1) then body 1 else if (Boolean expression 2) then body 2 : else body n; statements after the conditions;

J. Michael Moore Multiple If, Else-If's: Mutually Exclusive Conditions (Example) if (gpa = 4) then letter := 'A' else if (gpa = 3) then letter := 'B' else if (gpa = 2) then letter := 'C' else if (gpa = 1) then letter := 'D' else if (gpa = 0) then letter := 'F' else writeln('GPA must be one of 4, 3, 2, 1 or 0'); Watch your semi-colons!

J. Michael Moore Testing Decision Making Constructs Make sure that the body of each possible decision executes when it should. Test: 1)Obviously true cases 2)Obviously false cases 3)Boundary cases

J. Michael Moore Testing Decisions: An Example program testDecisions (input, output); begin var num : integer; write('Enter a value for num: '); readln(num); if (num >= 0) then writeln('Num is non-negative: ', num) else writeln('Num is negative: ', num); end.

J. Michael Moore Avoid Using Real Values When An Integer Will Do program testExample; begin var num : real; num := ; if (num = 0.61) then writeln('Sixty one cents') else writeln('Not sixty one cents'); end.

J. Michael Moore The Need For Repetition (Iteration / Loops) Writing out a simple counting program (1 – 3). program counting (output); begin writeln('1'); writeln('2'); writeln('3'); end.

J. Michael Moore The Need For Repetition (2) Simple program but what if changes need to be made? –The source code must be re-edited and re-compiled each time that a change is needed. What if you need the program to count many times?

J. Michael Moore Basic Structure Of Loops 1)Initialize the control a)Control – typically a variable that determines whether or not the loop executes or not. 2)Testing the control against a condition 3)Executing the body of the loop 4)Update the value of the control

J. Michael Moore Types Of Loops Pre-test loops 1.Initialize control 2.Check if a condition is met (using the control in some Boolean expression) a) If the condition has been met then continue on with the loop (go to step 3) b) If the condition is not met then break out of the loop (loop ends) 3.Execute the body of the loop 4.Update the value of the control 5.Repeat step 2

J. Michael Moore Types Of Loops Pre-test loops General characteristics –The body of the loop executes zero or more times –Execute the body only if the condition is true (stop executing when it becomes false) –Examples: while-do, for

J. Michael Moore Types Of Loops (2) Post-test loops 1.Initialize control (sometimes this step is unneeded because the control is set in the body, step 3) 2.Execute the body of the loop 3.Update the value of the control 4.Check if a condition is met (using the control in some Boolean expression) a) If the condition has been met then break out of loop (loop ends) b) If the condition has not been met then continue on with loop (go to step 2)

J. Michael Moore Types Of Loops (2) Post-test loops General characteristics –The body of the loop executes one or more times –Execute the body only if condition is false (stop executing when it's true) –Examples: repeat-until

J. Michael Moore Generally used when the number of times that the loop executes is not known in advance. Format: while (Boolean expression) do body Example: var i : integer; i: = 1; while (i <= 5) do begin writeln('i = ', i); i := i + 1; end; (* while *) Pre-Test Loop: While-Do

J. Michael Moore Can be used for almost any stopping condition. Loop executes as long as the Boolean expression is true. Format: while (Boolean expression) do body Example var i : integer; i: = 1; while (i <= 5) do begin writeln('i = ', i); i := i + 1; end; (* while *) Pre-Test Loop: While-Do 1) Initialize control 2) Check condition 3) Execute body 4) Update control

J. Michael Moore Tracing The While Loop

J. Michael Moore Infinite Loops Infinite loops never end (the stopping/exit condition is never met). They tend to be caused by logical errors. To stop a program with an infinite loop in Unix simultaneously press the and the keys

J. Michael Moore Infinite Loops The loop control is never updated var i : integer; i := 1; while (i <=10) do writeln('i=', i); i := i + 1; To stop a program with an infinite loop in Unix simultaneously press the and the keys

J. Michael Moore Infinite Loops The loop control is never updated var i : integer; i := 1; while (i <=10) do writeln('i=', i); i := i + 1; To stop a program with an infinite loop in Unix simultaneously press the and the keys Notice how the formatting helps us see the problem.

J. Michael Moore Infinite Loops The updating of the loop control never brings it closer to the stopping condition var i : integer; i := 10; while (i > 0) do begin writeln('i = ', i); i := i + 1; end; To stop a program with an infinite loop in Unix simultaneously press the and the keys