Insight Through Computing More on Conditionals Nested if’s Multiple Alternatives Top-Down Design.

Slides:



Advertisements
Similar presentations
Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Advertisements

Chapter 4 Computation Bjarne Stroustrup
The LC-3 – Chapter 6 COMP 2620 Dr. James Money COMP
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
Week 6 - Programming I So far, we’ve looked at simple programming via “scripts” = programs of sequentially evaluated commands Today, extend features to:
3-2 What are relational operators and logical values? How to use the input and disp functions. Learn to use if, if-else and else-if conditional statements.
Top-Down Design CSC 161: The Art of Programming Prof. Henry Kautz 9/16/2009.
Example – calculating interest until the amount doubles using a for loop: will calculate up to 1000 years, if necessary if condition decides when to terminate.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
Insight Through Computing 3. Introduction to Conditionals Boolean expressions The If-Else Construct And, or, not.
6. More on the For-Loop Using the Count Variable Developing For-Loop Solutions.
Precedence Parentheses Arithemetic ^ * / + - (exception logical not ~ ) Relational > =
Program Design and Development
Top-down Program Design, Relational and Logical Operators
Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic JavaScript: Control Structure.
1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection Statement 4.6 if else Selection Statement 4.7 while.
Precedence Parentheses Arithemetic ^ * / + - (exception logical not ~ ) Relational > =
Program Design, Relational and Logical Operators Selim Aksoy Bilkent University Department of Computer Engineering
Algorithms. Introduction Before writing a program: –Have a thorough understanding of the problem –Carefully plan an approach for solving it While writing.
Chapter 4 MATLAB Programming Logical Structures Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Programming with MATLAB. Relational Operators The arithmetic operators has precedence over relational operators.
Chapter 3 Planning Your Solution
ALGORITHMS AND FLOWCHARTS
CSC 1701B Computing: Science and Creativity. Outline  Types  Variables  Operators  Control: sequence, selection, repetition  Functions (block headings.
The University of Texas – Pan American
 2003 Prentice Hall, Inc. All rights reserved.  2004 Prentice Hall, Inc. All rights reserved. Chapter 8 - JavaScript: Control Statements I Outline 8.1.
Programming & Scratch. Programming Learning to program is ultimately about learning to think logically and to approach problems methodically. The building.
1 Flow of control Sequential Executing instructions one by one, in exact order given Selection Choosing to execute a particular set of statements depending.
1. Exam Topics Difference between computers and calculators John creates a new device. It will compute the orbit of all the planets in the solar system.
Boolean expressions, part 2: Logical operators. Previously discussed Recall that there are 2 types of operators that return a boolean result (true or.
1 Computer Programming (ECGD2102 ) Using MATLAB Instructor: Eng. Eman Al.Swaity Lecture (4): Control Flow (Chapter 2)
PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION.
Pseudocode Simple Program Design Third Edition A Step-by-Step Approach 2.
ALGORITHMS AND FLOWCHARTS CSCI 105 – Computer Fluency.
Conditionals CS 103 February 16, Blast from the Past: C14 Dating Problem Statement: Calculate the age of a fossil from its C-14 radioactivity Problem.
 2003 Prentice Hall, Inc. All rights reserved. Chapter 8 - JavaScript: Control Statements I Outline 8.1 Introduction 8.2 Algorithms 8.3 Pseudocode 8.4.
Algorithm Design.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Advanced Program Design. Review  Step 1: Problem analysis and specification –Specification description of the problem’s inputs and output –Analysis generalize.
Agenda Basic Logic Purpose if statement if / else statement
Chapter 4 Controlling Execution CSE Objectives Evaluate logical expressions –Boolean –Relational Change the flow of execution –Diagrams (e.g.,
ENG 1181 College of Engineering Engineering Education Innovation Center P. 1 MAT - Conditional Statements Topics Covered: 1. if based conditional statements.
CS 100Lecture 21 CS100J: Lecture 2 n Previous Lecture –Programming Concepts n problem, algorithm, program, computer, input, output, sequential execution,
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
CSCI 161 Lecture 7 Martin van Bommel. Control Statements Statements that affect the sequence of execution of other statements Normal is sequential May.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
Structured Programming II: If Statements By the end of this class you should be able to: implement branching in a program describe and use an “if” statement.
Chapter 3 Decisions Three control structures Algorithms Pseudocode Flowcharts If…then …else Nested if statements Code blocks { } multi statement blocks.
EGR 115 Introduction to Computing for Engineers Branching & Program Design – Part 3 Friday 03 Oct 2014 EGR 115 Introduction to Computing for Engineers.
Lesson thirteen Conditional Statement "if- else" ©
If statement.  It is made up of three main components:  The keyword itself,  an expression that is tested for its truth value,  and a code suite to.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 4: Introduction to C: Control Flow.
L6. More on the For-Loop Using the Count Variable Developing For-Loop Solutions.
Introduction to Flowcharts
 Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do.
Algorithms and Pseudocode CS Principles Lesson Developed for CS4 Alabama Project Jim Morse.
Chapter 7 JavaScript: Control Statements, Part 1
Chapter 4 MATLAB Programming
ALGORITHMS AND FLOWCHARTS
MSIS 655 Advanced Business Applications Programming
Control Structures: Selection Statement
ALGORITHMS AND FLOWCHARTS
Java Programming Control Structures Part 1
Programming We have seen various examples of programming languages
Programming Concepts and Database
Nested if’s Multiple Alternatives
DATA TYPES AND OPERATIONS
L3. Introduction to Conditionals
Presentation transcript:

Insight Through Computing More on Conditionals Nested if’s Multiple Alternatives Top-Down Design

Insight Through Computing Recall the if-else “Template” if end else boolean expression Commands to execute if the expression if TRUE Commands to execute if the expression if FALSE

Insight Through Computing Question Time Variables a, b, and c have whole number values. True or false: This fragment prints “Yes” if there is a right triangle with side lengths a, b, and c and prints “No” otherwise. if a^2 + b^2 == c^2 disp(‘Yes’) else disp(‘No’) end A: True B: False

Insight Through Computing a = 5; b = 3; c = 4; if a^2 + b^2 == c^2 disp(‘Yes’) else disp(‘No’) end Prints “no” even though we have: 5 4 3

Insight Through Computing if a^2 + b^2 == c^2 disp(‘Yes’) else disp(‘No’) end The boolean expression should be true if a 2 +b 2 =c 2 or a 2 +c 2 =b 2 or b 2 +c 2 =a 2 is true. a^2 + b^2 == c^2 (a^2+b^2==c^2) || (a^2+c^2==b^2) || (b^2+c^2==a^2)

Insight Through Computing Developing “If” Solutions Illustrate the thinking associated with the design of if statements. The methodology of stepwise refinement. Two examples...

Insight Through Computing Write a script that solicits a positive integer Y and prints the number of days in year Y as determined by the Gregorian calendar. Problem 1

Insight Through Computing Leap Year Rule A non-century year is a leap year if it is divisible by 4. A century year is a leap year only if it is divisible by 400.

Insight Through Computing Will Need the Built-In Function rem abrem(a,b) The value of rem(a,b) is the remainder when the value of a is divided by the value of b. (Assume a, b are whole numbers.)

Insight Through Computing Input y. If y is not divisible by 100 Use the non-century year rule. Otherwise Use the century year rule. “Pseudocode” Solution

Insight Through Computing y = input(‘Enter the Year:’); if rem(y,100) ~= 0 % y is not a multiple of 100 Use the non-century rule else % y is a multiple of 100 Use the century rule end Refine…

Insight Through Computing Refine the If-Box % y is not a multiple of 100 Use the non-century rule % y is not a multiple of 100 If y is divisible by 4 Print 366 Otherwise Print 365

Insight Through Computing % y is not a multiple of 100 if rem(y,4)==0 % y is divisible by 4 disp(‘366’) else % y is not divisible by 4 disp(‘365’) end

Insight Through Computing y = input(‘Enter the Year:’); if rem(y,100) ~= 0 % y is not a multiple of 100 Use the non-century rule else % y is a multiple of 100 Use the century rule end Refine…  Done  Next

Insight Through Computing Refine the Else-Box % y is divisible by 100 Use the Century rule % y is divisible by 100 If y is divisible by 400 Print 366 Otherwise Print 365

Insight Through Computing % y is divisible by 100 if rem(y,400)==0 % y is divisible by 400 disp(‘366’) else % y is not divisible by 400 disp(‘365’) end

Insight Through Computing y = input(‘Enter the Year:’); if rem(y,100) ~= 0 % y is not a multiple of 100 Use the non-century rule else % y is a multiple of 100 Use the century rule end Refine…  Done

Insight Through Computing y = input(‘Enter the Year:’); if rem(y,100)~=0 if rem(y,4)==0 disp(‘366’) else disp(‘365’) end else if rem(y,400)==0 disp(‘366’) else disp(‘365’) end The whole thing without comments

Insight Through Computing Key Problem-Solving Strategy Progress from pseudocode to Matlab through a sequence of refinements. Comments have an essential role during the transitions. They “stay on” all the way to the finished fragment.

Insight Through Computing Starting Points Vary In “Friendliness” A non-century year is a leap year if it is divisible by 4. A century year is a leap year only if it is divisible by 400. A year is a leap year if it is divisible by 4 with the exception of century years that are not divisible by 400.

Insight Through Computing Problem 2 Write a fragment that prints the minimum value of in the interval L <= x <= R

Insight Through Computing LR One Possibility L<=xc<=R minVal at xc

Insight Through Computing LR Another Possibility R<xc minVal at R

Insight Through Computing LR Still Another Possibility xc<L minVal at L

Insight Through Computing We conclude that... If xc is in the interval The minimum is at xc Otherwise The minimum is at an endpoint

Insight Through Computing We Start With Pseudocode… If xc is in the interval The minimum is at xc Otherwise The minimum is at an endpoint Task: Convert to “legal’’ Matlab.

Insight Through Computing First refinement… if (L <= xc) && (xc <= R) % L <= xc <= R % The minimum is at xc. else % xc < L or R < xc % The minimum is at an endpoint. end (1) Boolean expression, (2) commented if-box, (3) commented else box

Insight Through Computing Refine the If-Box % L <= xc <= R % The minimum is at xc. % L <= xc <= R % The minimum is at xc. minVal = xc^2 + b*xc + c

Insight Through Computing Refine the Else-Box % xc < L or R < xc % The minimum is at an endpoint. R<xc minVal at R xc<L minVal at L

Insight Through Computing % xc < L or R < xc % The minimum is at an endpoint. if xc is to the left of L The minimum is at L Otherwise The minimum is at R

Insight Through Computing % xc < L or R < xc % The minimum is at an endpoint. if xc < L % The minimum is at L minVal = L^2 + b*L + c else % The minimum is at R minVal = R^2 + b*R + c end

Insight Through Computing Overall (w/o Comments) if (L <= xc) && (xc <= R) minVal = xc^2 + b*xc + c. else if xc < L minVal = L^2 + b*L + c else minVal = R^2 + b*R + c end

Insight Through Computing Notice there are 3 Alternatives… if (L <= xc) && (xc <= R) minVal = xc^2 + b*xc + c. else if xc < L minVal = L^2 + b*L + c else minVal = R^2 + b*R + c end

Insight Through Computing The if-elseif-else Construct if (L <= xc) && (xc <= R) minVal = xc^2 + b*xc + c. elseif xc < L minVal = L^2 + b*L + c else minVal = R^2 + b*R + c end Execute exactly one block.

Insight Through Computing When there are Many Alternatives if elseif else end Boolean Expression Find the first true boolean expression & execute its block. Otherwise execute the else block.

Insight Through Computing A Common Situation… if end Boolean Expression When there is nothing to do if the boolean expression is false.

Insight Through Computing Question Time What can you say about the value of x and y after this fragment is executed? x = 3; Y = 4; if x < y x = y; y = x end if x > y x = 3; y = 4; end A: X is 3 and Y is 4 B: X is 4 and Y is 3 C: X is 4 and Y is 4

Insight Through Computing Top-Down Design State problem Define inputs & outputs Design algorithm Convert algorithm to program Test and debug An algorithm is an idea. To use an algorithm you must choose a programming language and implement the algorithm. Decomposition Stepwise refinement