BACS 287 Programming Logic 2. BACS 287 Sequence Construct The sequence construct is the default execution mode for the CPU. The instructions are executed.

Slides:



Advertisements
Similar presentations
Chapter 4 - Control Statements
Advertisements

Dry Run You can test your program without using a computer by dry running it on paper You act as the computer – following the instructions of the program,
Microsoft® Small Basic
Logic & program control part 3: Compound selection structures.
Selection (decision) control structure Learning objective
Chapter 8 and 9 Review: Logical Functions and Control Structures Introduction to MATLAB 7 Engineering 161.
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.
BACS 287 Programming Logic 3. BACS 287 Iteration Constructs Iteration constructs are used when you want to execute a segment of code several times. In.
Basic Elements of Programming A VB program is built from statements, statements from expressions, expressions from operators and operands, and operands.
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
06 Testing selection1June Testing selection CE : Fundamental Programming Techniques.
Control Structures: Getting Started Sequence and Selection also arithmetic operators, data types, logical operators.
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
1 Selection Structures. 2 Making Decisions Sample assignment statements to figure worker pay with possible overtime PayAmount = Hours * Rate PayAmount.
C++ for Engineers and Scientists Third Edition
Programming with MATLAB. Relational Operators The arithmetic operators has precedence over relational operators.
CIS162AD - C# Decision Statements 04_decisions.ppt.
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
Chapter 4: The Selection Structure
Computer Science Selection Structures.
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Visual C# 2005 Decision Structures. Visual C# Objectives Understand decision making Learn how to make decisions using the if statement Learn how.
Scott Marino MSMIS Kean University MSAS5104 Programming with Data Structures and Algorithms Week 5 Scott Marino.
Chapter 4: Making Decisions. Understanding Logic-Planning Tools and Decision Making Pseudocode – A tool that helps programmers plan a program’s logic.
Selection Structures (if & switch statements) (CS1123)
Selection Control Structures Simple Program Design Third Edition A Step-by-Step Approach 4.
Chapter 4 Selection Structures: Making Decisions.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
2 Objectives You should be able to describe: Relational Expressions Relational Expressions The if-else Statement The if-else Statement Nested if Statements.
Selection Control Structures. Simple Program Design, Fourth Edition Chapter 4 2 Objectives In this chapter you will be able to: Elaborate on the uses.
BACS 287 Programming Logic 1. BACS 287 Programming Basics There are 3 general approaches to writing programs – Unstructured – Structured – Object-oriented.
1 Boolean Expressions to Make Comparisons Boolean expression –Represents only one of two states –Expression evaluates to either true or false Expressions.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
22/11/ Selection If selection construct.
Chapter 4 Controlling Execution CSE Objectives Evaluate logical expressions –Boolean –Relational Change the flow of execution –Diagrams (e.g.,
Programming with App Inventor Computing Institute for K-12 Teachers Summer 2012 Workshop.
The IF-statements 31-Jan-2005 Venkatesh Ramamoorthy.
PEG200/Saidatul Rahah 1.  Selection Criteria › if..else statement › relational operators › logical operators  The if-then-else Statement  Nested if.
Programming Fundamentals1 Chapter 4 SELECTION STRUCTURES.
EGR 115 Introduction to Computing for Engineers Branching & Program Design – Part 3 Friday 03 Oct 2014 EGR 115 Introduction to Computing for Engineers.
An Object-Oriented Approach to Programming Logic and Design Chapter 5 Making Decisions.
Pascal Programming Making decisions - Selection Statements National Certificate Unit 4 Carl Smith.
8. DECISION STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
A First Book of C++ Chapter 4 Selection. Objectives In this chapter, you will learn about: –Relational Expressions –The if-else Statement –Nested if Statements.
Decision Making and Branching
Programming Logic and Design Fourth Edition, Comprehensive Chapter 5 Making Decisions.
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
While loops. Iteration We’ve seen many places where repetition is necessary in a problem. We’ve been using the for loop for that purpose For loops are.
CSE202: Lecture 5The Ohio State University1 Selection Structures.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Today… Operators, Cont. Operator Precedence Conditional Statement Syntax. Winter 2016CISC101 - Prof. McLeod1.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Condition – any expression that evaluates to true/false value Relational operators are BINARY.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
A First Book of C++ Chapter 4 Selection.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Making Choices with if Statements
Decisions Chapter 4.
Chapter 5 Decisions. Chapter 5 Decisions ssential uestion: How are Boolean expressions or operators used in everyday life?
More Selections BIS1523 – Lecture 9.
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
Iteration: Beyond the Basic PERFORM
3. Decision Structures Rocky K. C. Chang 19 September 2018
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
Chapter 3: Selection Structures: Making Decisions
Chapter 3: Selection Structures: Making Decisions
REPETITION Why Repetition?
Presentation transcript:

BACS 287 Programming Logic 2

BACS 287 Sequence Construct The sequence construct is the default execution mode for the CPU. The instructions are executed in the order they are encountered.

BACS 287 Selection Construct The selection construct is used to determine which logic branch to follow. The 2 basic forms of this construct are: – IF-ELSE-ENDIF – CASE Several variations are possible within each type and equivalent logic can be expressed in different ways.

BACS 287 Simple If-Then-Else Structure IF condition THEN true action1... true action n ELSE false action1... false action n ENDIF IF GPA > 4.0 THEN Print “GPA in Error” ELSE Print “GPA not over 4.0” ENDIF IF GPA > 3.3 THEN Print “Dean’s List” ENDIF

BACS 287 Simple If-Then-Else Structure The condition statement can be any valid statement that evaluates to True or False. The condition can be compound and connected by the logical operators (And, Or, Not, Xor). Evaluation of the condition follows the rules of Boolean Logic.

BACS 287 ‘AND’ Boolean Logic The ‘AND’ operator returns true only if both inputs are true IF a=1 AND b=2 THEN Print “Both true” ELSE Print “Both not true” ENDIF IF gpa >= 0.0 AND gpa <= 4.0 THEN Print “GPA OK” ELSE Print “GPA Not OK” ENDIF

BACS 287 ‘OR’ Boolean Logic The ‘OR’ operator returns true if either input is true IF a=1 OR b=5 THEN Print “It’s 1 or 5 or both” ELSE Print “It’s neither” ENDIF IF gpa 4.0 THEN Print “GPA Error” ELSE Print “GPA OK” ENDIF

BACS 287 ‘NOT’ Boolean Logic A ‘NOT’ Boolean operator inverts the condition evaluation NOT(1) = 0 NOT(0) = 1 - same as - NOT(True) = False NOT(False) = True IF NOT(a=1) then Print “A is not 1” ELSE Print “A is 1” ENDIF IF NOT End-Of-File THEN Read a record ELSE Close File ENDIF

BACS 287 ‘XOR’ Boolean Logic The ‘XOR’ operator returns true if only one input is true. False is returned otherwise IF a=1 XOR b=5 THEN Print “It’s 1 or 5, not both” ELSE Print “It’s both or none” ENDIF IF class=“JR” XOR class=“SR” THEN Print “Its JR or SR” ELSE Print “neither” ENDIF

BACS 287 Complex Boolean Expressions Boolean expressions can be combined in complex ways. For example: – IF (a=1 and b=6) or (a=2 and b=3) then... – IF (ACT > 21 or SAT>900) and gpa>2.0 or gpa > 3.0 then... Use () to determine order of evaluation If () are not used, then NOT comes first followed by AND then OR Equal logical operators are evaluated left to right within the expression

BACS 287 Complex Boolean Expressions When a NOT Boolean operator is combined with AND and OR then De Morgan’s rule comes into play. De Morgan’s rule: NOT(c OR d) = NOT c AND NOT d NOT(c AND d) = NOT c OR NOT d

BACS 287 De Morgan’s Rule Example NOT(c OR d) = NOT c AND NOT d Given: c = True and d = False not(T or F) = not T and not F not(T) = F and T F = F √ correct It also works for the other 3 possible cases. Try it and see for yourself!

BACS 287 Complex Boolean Expressions Another complexity occurs when NOTs are used with comparison operators. NOT(a > b) same as a <= b NOT(a = b NOT(a = b) same as a <> b NOT(a <> b) same as a = b The Moral: Be careful when you use the NOT operator

BACS 287 Nested If-Then-Else Structure IF condition1 THEN IF condition2 THEN action 1 ELSE action 2 ENDIF ELSE action 3 ENDIF IF a=5 THEN IF b=2 THEN Print “a = 5, b = 2” ELSE Print “a = 5, b <> 2” ENDIF ELSE Print “a <> 5, b = ?” ENDIF

BACS 287 Nested vs. Compound Expression IFs Nested Code IF status=“Senior” Then IF hours>110 Then Print “2nd Sem. SR” Else Print “1st Sem. SR” ENDIF ELSE Print “Not a SR” ENDIF Compound Code IF status=“Senior” and hours > 110 then Print “2nd Sem. SR” ENDIF IF status = “Senior” and hours <= 110 then Print “1st Sem. SR” ENDIF IF status<>“Senior” then Print “Not a SR” ENDIF

BACS 287 ElseIf Structure There is a compromise structure that combines the ELSE and IF lines into a single statement. This is the ELSEIF structure. IF condition1 then action ELSEIF condition2 then action ELSEIF condition3 then action..... ENDIF

BACS 287 ElseIf Structure vs. Compound Expressions Compound Code IF status=“Senior” and hours > 110 then Print “2nd Sem. SR” ENDIF IF status = “Senior” and hours <= 110 then Print “1st Sem. SR” ENDIF IF status<>“Senior” then Print “Not a SR” ENDIF ELSEIF Code IF status=“Senior” and hours > 110 then Print “2nd Sem.SR” ELSEIF status=“Senior” and Hours <= 110 then Print “1st Sem. SR” ELSE Print “Not a SR” ENDIF

BACS 287 ElseIf Structure The ELSEIF structure allows you to test a range of possibilities without the complexity of nesting. In practice, only 1 branch of the ELSEIF is executed, so it is equivalent to a multi-branch IF statement. Another multi-branch selection is the CASE statement.

BACS 287 IF Statement Summary Simple If-Then-Else Nested If-Then-Else Compound If statements If-ElseIf structure – All IFs can use complex Boolean logic

BACS 287 CASE Selection Structure The CASE selection structure allows you to build a flexible multi-branch IF statement without the complexity of deeply nested IF groups. The CASE is functionally equivalent to the ELSEIF, but normally preferred because it is more efficient and better documents the multi-branch nature of the logic.

BACS 287 CASE Selection Structure SELECT CASE test-exp CASE expression1 action... CASE expression2 action... CASE ELSE action... ENDSELECT SELECT CASE Color CASE “red” Print “red car” CASE “blue” Print “blue car” CASE “green” Print “green car” CASE Else Print “unknown color” ENDSELECT

BACS 287 CASE Structure Example Get Input-Value Select Case Input-Value Case 1,2,5 Print “It’s either 1, 2, or 5” Case 7 to 14 Print “It’s in the range of 7 thru 14 inclusive” Case 15, 17, 19 to 25 Print “Either 15, 17, or 19 thru 25 inclusive” Case > 25 Print “It’s greater than 25” EndSelect

BACS 287 If-ElseIf vs. Case Structure In some situations, the two structures are interchangeable; however, this is not always true. The condition of an IF statement can be complex and evaluates to True or False (only). The test-expression of the Case structure is a simple expression that evaluates to a value that is matched within the Case statement.

BACS 287 Practice Problem 1 Write the pseudocode to accept the temperature and print a message telling if it is above or below freezing. You should assume that freezing is anything equal to or below 32 degrees.

BACS 287 Practice Solution 1 Get Temp If Temp > 32 then Print “It is above freezing” Else Print “It is equal to or below freezing” EndIf

BACS 287 Practice Problem 1A Modify the previous problem to display a 3rd message if the temperature is below 0.

BACS 287 Practice Solution 1A Get Temp If Temp > 32 then Print “Above freezing” ElseIf Temp >= 0 Print “Below freezing” Else Print “It is below 0” EndIf Get Temp Select Case Temp Case > 32 Print “Above Freezing” Case >= 0 Print “Below Freezing” Case Else Print “It is below 0” EndSelect

BACS 287 Practice Problem 2 Write the pseudocode to get 2 numbers from the user and print a message that tells if both were positive, both negative, the 1st negative (only), or the 2nd negative (only). Use good structured technique. Assume that positive is defined as 0 or above.

BACS 287 Practice Solution 2 Get A,B If A >= 0 then If B >= 0 then Print “Both positive” Else Print “Second number negative” EndIf ElseIf B < 0 then Print “Both negative” Else Print “First number negative” EndIf

BACS 287 Practice Solution 2 alternative Get A,B If A >= 0 and B >= 0 then Print “Both positive” Endif If A < 0 and B < 0 then Print “Both negative” Endif If A >= 0 and B < 0 then Print “Second number negative” Endif If A = 0 then Print “First number negative” EndIf

BACS 287 Practice Problem 3 Write the pseudocode to calculate the tax using the ‘single filing status’ table below. $ RangeFormula 0-22,750Income * 15% 22,751-55,100$3,412 + Income * 28% 55, ,000$12,470 + Income * 31% 115, ,000$31,039 + Income * 36% > 250,000$79,639 + Income * 39.6%

BACS 287 Practice Solution 3 Read Income If Income <= then Tax = Income *.15 ElseIf Income <= then Tax = ((Income ) *.28) ElseIf Income <= then Tax = ((Income ) *.31) ElseIf Income <= then Tax = ((Income ) *.36) Else Tax = ((Income ) *.396) Endif Print Tax

BACS 287 Practice Solution 3A Read Income Select Case Income Case <= Tax = Income *.15 Case <= Tax = ((Income ) *.28) Case <= Tax = ((Income ) *.31) Case <= Tax = ((Income ) *.36) Case Else Tax = ((Income ) *.396) EndCase Print Tax

BACS 287 Would This Work? Read Income Select Case Income Case > Tax = ((Income ) *.396) Case <= Tax = ((Income ) *.36) Case <= Tax = ((Income ) *.31) Case <= Tax = ((Income ) *.28) Case Else Tax = Income *.15 EndCase Print Tax

BACS 287 Practice Problem 4 Get a series of numbers from the user. When the user enters -999 stop collecting numbers. Print the largest positive number entered. Hint: This uses an iteration construct too…

BACS 287 Practice Solution 4 max_value = 0 Get a number Do While number <> -999 If number > max_value then max_value = number EndIf Get a number EndDo Print max_value