Download presentation
Presentation is loading. Please wait.
Published byRachel Miles Modified over 8 years ago
1
Conditionals CS 103 February 16, 2004
2
Blast from the Past: C14 Dating Problem Statement: Calculate the age of a fossil from its C-14 radioactivity Problem Statement: Calculate the age of a fossil from its C-14 radioactivity Black Box Design: Inputs: percentage of C-14 in the fossil Outputs: age of the sample in years Black Box Design: Inputs: percentage of C-14 in the fossil Outputs: age of the sample in years
3
C-14 Dating Algorithm Algorithm Get percentage Get percentage Calculate the age Calculate the age Print the age Print the age
4
C-14 Dating Algorithm – Stepwise Refinement Algorithm – Stepwise Refinement Get percentage Get percentage Prompt user for percentage and read it in Prompt user for percentage and read it in Calculate age Calculate age Lambda = ? Lambda = ? Ratio = percentage/100 Ratio = percentage/100 Age = -ln(Q/Qo) / lambda Age = -ln(Q/Qo) / lambda Print age with message Print age with message
5
Control Constructs How do we choose what should be done next? How do we choose what should be done next? Sequential Control – in the order that they appear in the program Sequential Control – in the order that they appear in the program Selection – next statement is one of two or more chosen according to the values of some variables Selection – next statement is one of two or more chosen according to the values of some variables Loops Loops
6
Selection / Branches A branch is a point in a program where the control can switch to more than one command. A branch is a point in a program where the control can switch to more than one command. The most common type of branching control is the if statement The most common type of branching control is the if statement If statements are Boolean – they seek to determine if a condition is true or false If statements are Boolean – they seek to determine if a condition is true or false
7
The IF Construct Format: IF expression statements END Format: IF expression statements END Note: If expression is not a scalar, then ALL of its elements have to be nonzero (true) to get truth! Note: If expression is not a scalar, then ALL of its elements have to be nonzero (true) to get truth!
8
C-14 Dating Example IF percentage > 100 fprintf(‘That’’s too big’) END IF percentage <= 0 fprintf(‘That’’s too small’) END
9
Using ELSEIF We can improve the efficiency of our code by using the ELSEIF construct IF expression statements ELSEIF expression statements ELSEIF expression statements ELSE statements END We can improve the efficiency of our code by using the ELSEIF construct IF expression statements ELSEIF expression statements ELSEIF expression statements ELSE statements END
10
C-14 Example IF percentage > 100 fprintf(‘The number is too big’) ELSEIF percentage <= 0 fprintf(‘The number is too small’) ELSE fprintf(‘The number is just right’) END
11
Making Programs Robust What is a robust program? What is a robust program? It does something reasonable with all inputs It does something reasonable with all inputs Catches errors Catches errors Avoids showing syntax errors to the user Avoids showing syntax errors to the user Has bells and whistles Has bells and whistles Looks pretty Looks pretty Is properly commented and documented Is properly commented and documented What do we need to do to the C-14 program to make it robust? What do we need to do to the C-14 program to make it robust?
12
Pricing Widgets We sell widgets. We’d like to give our best customers a discount, so we’ve implemented volume pricing. If you purchase 1-10 widgets, they cost $11 each. If you buy 11 or more widgets, they cost $10.50 each. We sell widgets. We’d like to give our best customers a discount, so we’ve implemented volume pricing. If you purchase 1-10 widgets, they cost $11 each. If you buy 11 or more widgets, they cost $10.50 each. We want to write a program that will calculate the total cost of an order of widgets. We want to write a program that will calculate the total cost of an order of widgets.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.