Conditionals CS 103 February 16, 2004. Blast from the Past: C14 Dating Problem Statement: Calculate the age of a fossil from its C-14 radioactivity Problem.

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 - Control Statements
CS 101 Introductory Programming - Lecture 7: Loops In C & Good Coding Practices Presenter: Ankur Chattopadhyay.
True BASIC Ch. 6 Practice Questions. What is the output? PRINT X LET X = -1 PRINT X FOR X = 4 TO 5 STEP 2 PRINT X NEXT X PRINT X END.
TrueBASIC Ch 4 & 5 Sample Problems. What are the errors? (4 total) INPUT age IF age > 7 THEN PRINT "rollercoaster" ELSE PRINT "ferris wheel" IF age
Insight Through Computing More on Conditionals Nested if’s Multiple Alternatives Top-Down Design.
1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.
Program design example Task: Develop an algorithm expressed in pseudocode for a specified problem specified problem.
Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic JavaScript: Control Structure.
If-else 03/13/15. Program 6 Due 4/3/14 See the 115 website.
1 Arithmetic in C. 2 Type Casting: STOPPED You can change the data type of the variable in an expression by: (data_Type) Variable_Name Ex: int a = 15;
The If/Else Statement, Boolean Flags, and Menus Page 180
JavaScript Switch Statement. Switch JavaScript Switch Statement If you have a lot of conditions, you can use a switch statement instead of an if…elseif…
Adding Automated Functionality to Office Applications.
CS140: Intro to CS An Overview of Programming in C by Erin Chambers.
Introduction to Python Lecture 1. CS 484 – Artificial Intelligence2 Big Picture Language Features Python is interpreted Not compiled Object-oriented language.
Python – Part 4 Conditionals and Recursion. Modulus Operator Yields the remainder when first operand is divided by the second. >>>remainder=7%3 >>>print.
Line Continuation, Output Formatting, and Decision Structures CS303E: Elements of Computers and Programming.
Loops Doing the same thing over and over and over and over and over and over…
Flow Charting. Goals Create Algorithms using Flow Charting procedures. Distinguish between Flow Charting and Pseudocode. Top-Down Design Bottom-up Design.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Selection Structures (if & switch statements) (CS1123)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
Selection Control Structures. Simple Program Design, Fourth Edition Chapter 4 2 Objectives In this chapter you will be able to: Elaborate on the uses.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
Flow of Control Part 1: Selection
CMPS 1371 Introduction to Computing for Engineers CONDITIONAL STATEMENTS.
CSE1222: Lecture 6The Ohio State University1. Common Mistakes with Conditions (1)  Consider the following code: int age(26); if (age = 18) { cout
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
CHAPTER#3 PART1 STRUCTURED PROGRAM DEVELOPMENT IN C++ 2 nd semester King Saud University College of Applied studies and Community Service Csc.
Introduction to Matlab Module #4 Page 1 Introduction to Matlab Module #4 – Programming Topics 1.Programming Basics (fprintf, standard input) 2.Relational.
Chapter 4 Controlling Execution CSE Objectives Evaluate logical expressions –Boolean –Relational Change the flow of execution –Diagrams (e.g.,
Lecture 5: Stopping with a Sentinel. Using a Sentinel Problem Develop a class-averaging program that will process an arbitrary number of grades each time.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
Review if imag(x) > 0 fprintf('Sorry, but I don''t understand complex numbers.\n'); return end if x < 10 % leave this out, type the next line first, and.
Chapter#3 Part1 Structured Program Development in C++
Introduction to C Programming CE Lecture 5 Program Design in C.
Algorithm Discovery and Design Objectives: Interpret pseudocode Write pseudocode, using the three types of operations: * sequential (steps in order written)
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.
The Department of Engineering Science The University of Auckland Welcome to ENGGEN 131 Engineering Computation and Software Development Lecture 2 Debugging,
Loops CS 103 February 23, Review FOR is a control construct that provides a loop for ii = 1:10 fprintf(‘%d \n’, ii) end FOR is a control construct.
The If Statement There are no switch statements in Python. You need to use just if statements. There are no switch statements in Python. You need to use.
UFCFY5-30-1Multimedia Studio Scripting for Interactive Media Further Structures for Branching and Looping.
Python Basics  Values, Types, Variables, Expressions  Assignments  I/O  Control Structures.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
Design Document Sample Given two concentrated circles with different radii, calculate the area which falls inside the big circle but outside the small.
General Condition Loop A general condition loop just loops while some condition remains true. Note that the body of the loop should (eventually) change.
1 JavaScript/Jscript 2 Control Structures I. 2 Introduction Before programming a script have a –Thorough understanding of problem –Carefully planned approach.
More on conditional statements. Conditionals In some situations the typical if-else statements may become cumbersome Depending on the situation, there.
 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.
Line Continuation, Output Formatting, and Decision Structures
The Pseudocode Programming Process
Scripts & Functions Scripts and functions are contained in .m-files
SELECTION STATEMENTS (1)
Loops CS140: Introduction to Computing 1 Savitch Chapter 4 Flow of Control: Loops 9/18/13 9/23/13.
Conditional Statements
More Selections BIS1523 – Lecture 9.
When I want to execute the subroutine I just give the command Write()
Line Continuation, Output Formatting, and Decision Structures
محاضرة 1: مقدمة للمسـاق و مراجعـة للأساسيـات
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
Coding Concepts (Basics)
Visual Basic – Decision Statements
JavaScript.
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Selection Statements Chapter 3.
Flow of Control Flow of control is the order in which a program performs actions. Up to this point, the order has been sequential. A branching statement.
Presentation transcript:

Conditionals CS 103 February 16, 2004

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

C-14 Dating Algorithm Algorithm Get percentage Get percentage Calculate the age Calculate the age Print the age Print the age

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

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

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

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!

C-14 Dating Example IF percentage > 100 fprintf(‘That’’s too big’) END IF percentage <= 0 fprintf(‘That’’s too small’) END

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

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

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?

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.