ANALYSIS AND ALGORITHM DESIGN - III

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

Making Decisions in Python Sec 9-10 Web Design. Objectives The student will: Understand how to make a decision in Python Understand the structure of an.
Selection (decision) control structure Learning objective
Understanding the Three Basic Structures
Subject: Information Technology Grade: 10
If Statements & Relational Operators Programming.
Basic Elements of Programming A VB program is built from statements, statements from expressions, expressions from operators and operands, and operands.
Computer Science 1620 Programming & Problem Solving.
1 Selection Structures. 2 Making Decisions Sample assignment statements to figure worker pay with possible overtime PayAmount = Hours * Rate PayAmount.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Decision Structures and Boolean Logic.
Decision Structures and Boolean Logic
Branching and Conditions CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Decision Structures Chapter 4 Part 2. Chapter 4 Objectives To understand o What relational operators are and how they are used o Boolean logic o Testing.
CHAPTER 4: CONDITIONAL STRUCTURES Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Selection Structure If... Then.. Else Case. Selection Structure Use to make a decision or comparison and then, based on the result of that decision or.
Selection Control Structures Simple Program Design Third Edition A Step-by-Step Approach 4.
Problem Solving with Decisions
Agenda Exam #1 Review Modulus Conditionals Boolean Algebra Reading: Chapter Homework #5.
CHAPTER 3 SELECTION STRUCTURE.
Selection Control Structures. Simple Program Design, Fourth Edition Chapter 4 2 Objectives In this chapter you will be able to: Elaborate on the uses.
1 CSC103: Introduction to Computer and Programming Lecture No 7.
Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.
ANALYSIS AND ALGORITHM DESIGN - IV. Repeat statements/looping/counting/iterations It is often necessary to repeat certain parts of a program a number.
CHAPTER 4: Selection Control Structure. Objectives  Use the relational comparison operators  Learn about AND logic  Learn about OR logic  Make selections.
PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
ALGORITHMS AND FLOWCHARTS CSCI 105 – Computer Fluency.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
Making Decisions (True or False) Relational Operators >greater than =greater than or equal to
Lecture #7 CONTROL STRUCTURE & FLOW CHARTS
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
Topics: Selection Statements. Processing Involving Selecting Instructions An instruction that allows deviation and selection to take place uses the ‘IF’
Programming Logic and Design Fourth Edition, Comprehensive Chapter 5 Making Decisions.
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.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Flow Charts And Pseudo Codes Grade 12. An algorithm is a complete step-by- step procedure for solving a problem or accomplishing a task.
SELECTION CONTROL STRUCTURE Prepared by: Careene McCallum-Rodney.
Decision making If.. else statement.
Introduction to Decision Structures and Boolean Variables
Control Flow (Python) Dr. José M. Reyes Álamo.
Control Structures I Chapter 3
More on the Selection Structure
COVERED BASICS ABOUT ALGORITHMS AND FLOWCHARTS
Topics The if Statement The if-else Statement Comparing Strings
The order in which statements are executed is called the flow of control. Most of the time, a running program starts at the first programming statement,
Chapter 4: Decision Structures and Boolean Logic
Lecture 3 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz
Chapter 6: Conditional Statements and Loops
Selection By Ramin && Taimoor
Topics The if Statement The if-else Statement Comparing Strings
ALGORITHMS AND FLOWCHARTS
Control Structure Senior Lecturer
Understanding the Three Basic Structures
Chapter 4: Decision Structures and Boolean Logic
Decision making If statement.
If selection construct
Visual Basic – Decision Statements
Chapter 5: Control Structure
The if Statement Control structure: logical design that controls order in which set of statements execute Sequence structure: set of statements that execute.
Boolean Expressions to Make Comparisons
CHAPTER 4: Conditional Structures
CS2011 Introduction to Programming I Selections (I)
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Chapter 4: Decision Structures and Boolean Logic
REPETITION Why Repetition?
Presentation transcript:

ANALYSIS AND ALGORITHM DESIGN - III

The IF-THEN construct The IF-THEN construct contains a condition which is tested before an action is undertaken. If the condition holds true, then the action is taken. Otherwise, the instruction statements between IF-THEN and ENDIF are not taken, but are ignored. Syntax: IF <condition> THEN <Action to be taken if condition is true> (that is, instruction statements that would be performed if the conditions are met) ENDIF

The IF-THEN construct Example 1 A company gives out bonuses based on the amount of income generated by their sales representatives per month. Once the income is greater than $5000.00, a bonus of 10% of the generated income is given to the employees. Read the income generated and print the bonus.

The IF-THEN construct Solution 1: Case 2 In this case, the PRINT statement is placed outside the IF-THEN-ENDIF construct. So the bonus is printed regardless of whether the bonus is actually calculated or not. PRINT “Enter the income generated” READ inc_gen IF inc_gen > $5000.00 THEN bonus = inc_gen * 10% ENDIF PRINT “Bonus = ”, bonus

The IF-THEN construct Solution 1 The decision to give a bonus is based on the amount of income the sales representative generates. Input/read the Income Generated. The action to be taken is the calculation and printing of the bonus. Only if the Income Generated is greater than $5000.00 is the bonus calculated.

The IF-THEN construct Solution 1: Case 1 We must be aware of where the PRINT statement is placed. If it is placed within the IF-THEN-ENDIF construct, only if the income generated is greater than $5000.00 is the bonus printed. PRINT “Enter the income generated” READ inc_gen IF inc_gen > $5000.00 THEN bonus = inc_gen * 10% PRINT “Bonus = ”, bonus ENDIF

The IF-THEN construct Example 2 A car rental firm leases its cars for $250.00 per day. The manager gives a discount based on the number of days that the car is rented. If the rental period is greater than or equal to 7 days, then a 25% discount is given. Read the rental period and print the discount given.

The IF-THEN construct Solution 2 Input the rental period. The decision is based on the length of time the car is rented. The action is to calculate the discount to be given for the rental period. Only if the car is rented for 7 days or more is the discount calculated.

The IF-THEN construct Solution 2 PRINT “Enter car rental period” READ rental_period IF rental_period >= 7 THEN discount = ($250.00 * rental_period) * 25% ENDIF PRINT “Discount = ”, discount

The IF-THEN-ELSE construct The IF-THEN-ELSE construct contains two parts: the THEN part and and ELSE part. The condition is tested before an action can be undertaken. If the condition holds TRUE, the THEN action is taken, otherwise the ELSE action is taken if the condition is FALSE

The IF-THEN-ELSE construct Indentation is used for readability, so that you can see at a glance the structure of the construct - especially which statements belong to the THEN part, and which belong to the ELSE part. Syntax: IF<Condition> THEN <THEN part: Action to be taken if condition is TRUE> ELSE <ELSE part: Action to be taken if condition is FALSE> ENDIF

The IF-THEN-ELSE construct Example 3 A company gives out bonuses based on the amount of income generated by their sales representatives per month. Once the income is greater than $5000.00 then a bonus of 10% of the generated income is payable; otherwise the bonus is 3% of the generated income. Read the income generated and print the bonus.

The IF-THEN-ELSE construct Solution 3 PRINT “Enter income generated” READ inc_gen IF inc_gen > $5000.00 THEN bonus = inc_gen * 10% ELSE bonus = inc_gen * 3% ENDIF Print “Bonus = ”, bonus

Boolean Operators When selection is based upon one or more expressions/decisions being TRUE/FALSE. It is possible to combine the expressions/decisions together using the Boolean operators AND or OR. IF the AND operator is used both conditions must be met, in order for the entire expression to be true or false. IF the OR operator is used, either condition must be met, in order for the total expression to be true or false.

Boolean Operators Example 4 A club plays cricket only on Sundays, and only if it is not raining. Read the day and the weather and print ‘Game on’ if it is a suitable day for playing.

Boolean Operators Solution 4 – With the AND operator Both conditions must be met for the expression to be true and ‘Game on’ is printed. If either condition is not met, such as the day is not ‘Sunday’ or the weather is ‘Rain’, then the action (printing ‘Game on’) is not taken. PRINT “Enter the day and the weather” READ day, weather IF day = “Sunday” AND weather = “No Rain” THEN PRINT “Game on” ENDIF

Boolean Operators Example 5 A company gives out bonuses based on the amount of income generated by their sales representatives per month. Once the income is greater than $5000.00 and less than or equal to $8000.00 then a bonus of 10% of the generated income is given to employee. Read the generated income and print the bonus.

Boolean Operators Solution 5 PRINT “Enter income generated” READ inc_gen IF (inc_gen >= $5000.00) AND (inc_gen<=$8000.00) THEN bonus = inc_gen*10% ENDIF PRINT “Bonus = ”, bonus

Nested selections IF statements embedded one within another are said to be nested. For every IF-THEN statement there must be an ENDIF. Syntax: IF<Condition 1> THEN <Action to be taken if Condition 1 is true> ELSE IF <Condition 2> THEN <Action to be taken if Condition 2 is true> IF <Condition 3> THEN <Action to be taken if Condition 3 is true>

Nested selections Syntax (cont’d): ELSE <Action to be taken if Conditions 1-3 are false> ENDIF If the first condition is not met, the second condition is checked, if the second is not met, then the third condition is checked, and so on.

Nested selections Example 6 A company gives out bonuses based on the amount of income generated by their sales representatives per month. Once the income is greater than or equal to $10,000.00, a bonus of 20% is given. If the income generated is greater than or equal to $8000.00 but less than $10,000.00, a bonus of 15% is given. If the income generated is greater than or equal to $5000.00 but less than $8,000.00, a bonus of 10% is given. If the income generated is less than $5000.00, a bonus of 3% is given. Read the generated income and print the bonus.

Nested selections Solution 6 PRINT “Enter income generated” READ inc_gen IF (inc_gen >= $10,000.00) THEN bonus = inc_gen * 20% ELSE IF (inc_gen >= $8,000.00) AND (inc_gen < $10,000.00) THEN bonus = inc_gen * 15% IF (inc_gen >= $5,000.00) AND (inc_gen < $8,000.00) THEN bonus = inc_gen * 10%

Nested selections Solution 6 (cont’d) ELSE bonus = inc_gen * 3% ENDIF PRINT “Bonus = ”, bonus