Outline. Review Choice Statements. Review Sequential Flow. Review Choice Before Loops. Review Choice After Loops

Similar documents
More Complex Versions of the if Statement. Class 13

Class 2: Variables and Memory. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

Outline. Review of Last Week II. Review of Last Week. Computer Memory. Review Variables and Memory. February 7, Data Types

Outline. First Quiz Results. Exercise Five Goals. Question Three. Questions One and Two. Exercise five if statements February 28, 2006

Control Statements. If Statement if statement tests a particular condition

Outline. Why do we write functions? Introduction to Functions. How do we write functions? Using Functions. Introduction to Functions March 21, 2006

while Loops Lecture 13 Sections Robb T. Koether Wed, Sep 26, 2018 Hampden-Sydney College

1. a) What #include statement do you put at the top of a program that does uses cin, cout or endl?

Logical Operators and if/else statement. If Statement. If/Else (4.3)

Score score < score < score < 65 Score < 50

5. Selection: If and Switch Controls

LAB 4.1 Relational Operators and the if Statement

Selections. CSE 114, Computer Science 1 Stony Brook University

Reviewing all Topics this term

Chapter 3 Selection Statements

Selection Control Structure CSC128: FUNDAMENTALS OF COMPUTER PROBLEM SOLVING

Outline. Debugging. In Class Exercise Solution. Review If Else If. Immediate Program Errors. Function Test Example

Repetition Algorithms

Chapter 4 - Notes Control Structures I (Selection)

Boolean Data-Type. Boolean Data Type (false, true) i.e. 3/6/2018. The type bool is also described as being an integer: bool bflag; bflag = true;

Motivations. Chapter 3: Selections and Conditionals. Relational Operators 8/31/18. Objectives. Problem: A Simple Math Learning Tool

CSI33 Data Structures

CHAPTER 2.1 CONTROL STRUCTURES (SELECTION) Dr. Shady Yehia Elmashad

Introduction to: Computers & Programming: Review prior to 1 st Midterm

Computer Department. Question (1): State whether each of the following is true or false. Question (2): Select the correct answer from the following:

Object Oriented Programming Using C++ Mathematics & Computing IET, Katunayake

Chapter 4 : Selection (pp )

Computer Programming : C++

Control Structures in Java if-else and switch

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

School Year:

Add Subtract Multiply Divide

The American University in Cairo Department of Computer Science & Engineering CSCI &09 Dr. KHALIL Exam-I Fall 2011

UEE1302(1066) F12: Introduction to Computers and Programming Function (II) - Parameter

Control Structures: The IF statement!

CS 105 Lecture 5 Logical Operators; Switch Statement. Wed, Feb 16, 2011, 5:11 pm

University of Dublin

Due Date: Friday, September 9 th Attached is your summer review packet for the Algebra 1 course.

More Flow Control Functions in C++ CS 16: Solving Problems with Computers I Lecture #4

Control Structures in Java if-else and switch

Math Released Item Grade 7. Grid Coordinates of a Square VH225469

The American University in Cairo Computer Science & Engineering Department CSCE Dr. KHALIL Exam II Spring 2010

CPE 112 Spring 2015 Exam II (100 pts) March 4, Definition Matching (8 Points)

4.1. Chapter 4: Simple Program Scheme. Simple Program Scheme. Relational Operators. So far our programs follow a simple scheme

In this chapter you will learn:

CSCI 111 First Midterm Exam Spring Solutions 09.05am 09.55am, Wednesday, March 14, 2018

BRAZOSPORT COLLEGE LAKE JACKSON, TEXAS SYLLABUS COSC 1320: INTRODUCTION TO C++ PROGRAMMING COMPUTER TECHNOLOGY & OFFICE ADMINISTRATION DEPARTMENT

CS 115 Lecture 8. Selection: the if statement. Neil Moore

BITG 1223: Selection Control Structure by: ZARITA (FTMK) LECTURE 4 (Sem 1, 16/17)

Selection Statements. Pseudocode

Excel Functions & Tables

Programming Language. Control Structures: Repetition (while) Eng. Anis Nazer Second Semester

Chapter 3 Selections. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

Decision Control Structure. Rab Nawaz Jadoon DCS. Assistant Professor. Department of Computer Science. COMSATS IIT, Abbottabad Pakistan

Computer Science Grade 10 Sample -Term 2 Date: February 2018 Time: TBC Duration: 45 minutes

Introduction to Programming

Chapter 3 Problem Solving and the Computer

CS 101 Computer Programming and utilization. Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal Rekhi Building IIT Bombay

GRADE 7 MATH LEARNING GUIDE

LECTURE 04 MAKING DECISIONS

I SEMESTER EXAM : : XI :COMPUTER SCIENCE : MAX MARK a) What is the difference between Hardware and Software? Give one example for each.

Python - Week 3. Mohammad Shokoohi-Yekta

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS240 BRANCHING STATEMENTS

MSE Comprehensive Exam

Decision Making -Branching. Class Incharge: S. Sasirekha

FORM 2 (Please put your name and form # on the scantron!!!!)

Final Examination Semester 2 / Year 2005

Laboratory 0 Week 0 Advanced Structured Programming An Introduction to Visual Studio and C++

Chapter 3 Structured Program Development

Programming Language. Functions. Eng. Anis Nazer First Semester

Excel Functions & Tables

CSE 215: Foundations of Computer Science Recitation Exercises Set #4 Stony Brook University. Name: ID#: Section #: Score: / 4

LAB: INTRODUCTION TO FUNCTIONS IN C++

UEE1302(1102) F10: Introduction to Computers and Programming

Introduction to C++ 2. A Simple C++ Program. A C++ program consists of: a set of data & function definitions, and the main function (or driver)

بسم اهلل الرمحن الرحيم

Chapter 4: Making Decisions

Flow of Control: Programs can control the order in which their instructions are executed. Four types of flow:

Learning Objectives: General Description: DONE DONE Structure Chart

Read this before starting!

Section 4.2 selected answers Math 131 Multivariate Calculus D Joyce, Spring 2014

Chapter 4: Making Decisions

From Pseudcode Algorithms directly to C++ programs

Week 2. Relational Operators. Block or compound statement. if/else. Branching & Looping. Gaddis: Chapters 4 & 5. CS 5301 Spring 2018.

Decisions in Java The IF Statement. In performing a task, we often want to take one of two possible actions depending on the conditions at the time.

CSCI 111 First Midterm Exam Fall Solutions 09.00am 09.50am, Wednesday, October 18, 2017

QUIZ: What value is stored in a after this

gg--c}/-/j) r x Revision: New: COBOL I TWO Date: Author: Date SAULT COLLEGE OF APPLIED ARTS & TECHNOLOGY SAULT STE. MARIE, ONTARIO

STUDENT OUTLINE. Lesson 8: Structured Programming, Control Structures, if-else Statements, Pseudocode

Dinwiddie County Public Schools Subject: Math 7 Scope and Sequence

Conditions and Logical Expressions. C Programming

The design recipe. Readings: HtDP, sections 1-5. (ordering of topics is different in lectures, different examples will be used)

Revision: August 31, E Main Suite D Pullman, WA (509) Voice and Fax

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. and Java. Chapter 2 Primitive Data Types and Operations

Chapter 5. Repetition. Contents. Introduction. Three Types of Program Control. Two Types of Repetition. Three Syntax Structures for Looping in C++

Discussion 1H Notes (Week 3, April 14) TA: Brian Choi Section Webpage:

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Computer Engineering 1 (1E3)

Review Questions I Spring 2010

Lecture 5. Review from last week. Selection Statements. cin and cout directives escape sequences

Transcription:

Programming with If Statements using Multiple Conditions Larry Caretto Computer Science 106 Computing in Engineering and Science February 23, 2006 Outline Review last class Program flow controls if s Exercises with if s Multiple choices Exercises with multiple choices Sequential if s versus the if-if structure 2 Review Sequential Flow Review Choice Statements Previous Statement Current Statement Statement Statement after Statements are normally executed in sequential order but you must have the correct sequence What is wrong with y = x * x; cin >> x; cout << y; 3 False True 4 Review Choice Before Loops Review Choice After Loops Statements in loop must change Loop Statements in loop must change Loop 5 6 1

Review Function Transfer control and data to separate part of program Return control and data Math library functions are an example You will create your own functions later Function Review Conditions A is an expression that evaluates to a boolean value of true or false Use relational operators: greater than >, equal to ==, less than <, not equal to!=, greater than or equal to >=, less than or equal to <= Logical operators: not!, and &&, or Examples: hours > 40, wind > 20 && temperature < 30 7 8 Exercise on Conditions Use relational (<, >, <=, >=, ==,!=) and logical (!, &&, ) operators to write s for the following: An integer variable year is not evenly divisible by four year % 4!= 0 or!( year % 4 == 0 ) A string variable status equals single and an integer variable dependents is 0 status == single && dependents == 0 Review if Statements Implementation of choice s in most high-level languages uses an if The C++ format is if (<>) <s done if true> 9 10 Review if- Statements Executes different s if is true or false if (<>) <s done if true> <s done if false> < after one done> 11 Writing if Statements Exercise Define variable inc for income, deduct for deductions and ti taxable income Taxable income is income minus deductions, but is never less than zero Write code to compute taxable income double ti = inc deduct; if ( ti < 0 ) ti = 0; 12 2

Exercise Write a program that delcares and reads a type double variable x, and determines if it is greater than zero If x > 0 compute and print the natural logarithm using the log() function Also print the value of x input by the user Otherwise print an error message that you cannot compute log of a negative number 13 Exercise Solution double x; cout << Enter a value for x: cin << x; if ( x > 0 ) cout << The natural log of << x << is << ln(x); cout << Cannot compute log for << negative input x = << x; 14 Multiple Conditions Can have several choices Example is an empirical function for y(x) with different equations for y used in different ranges of x Structure to handle this is called if-if Allows initial if (and associated code) to be followed by several other s like if ( <new > ) 15 if if Structure if (<1>) <s done if 1 true> if (<2>) <s done if 2 true> // Place additional s here // Continue on next chart 16 if if Structure // Continued from previous chart if (<N>) <s done if N true> // optional to have this final <s done if all s false> < after any done> 17 if if Operation In this structure only one of code the code associated with the first true is executed Conditions are scanned from top to bottom until the first true is found The code associated with that is executed and control is transferred to the first after the final in the if if structure 18 3

if if Operation II Because only one of code the code associated with the first true is executed we have information at -if s Example, what do we know about x at the -if in the following? if ( x < 0 ) If x < 0, we would set y = 0 and exit the if-if structure. If we get y = 0; if ( x to the -if 19 we know x 0 Example/Exercise How do you program the following definition of an empirical function y(x)? If x < 0, then y = 0. If 0 x < 1, then y = 0.1 x If 1 x < 10, then y = ( x 0.8 ) / 2 If 10 x < 100, then y = 4.6 + 0.2(x 10) 3 If x >= 100, then y = 1624.6 20 Answer to Exercise If x < 0, then y = 0. If 0 x < 1, then y = 0.1 x If 1 x < 10, then y = ( x 0.8 ) / 2 if ( x < 0 ) y = 0; if ( x < 1 ) // ( x >= 0 && x < 1 )?? y = 0.1 * x; if ( x < 10 ) // ( x >= 1 && x < 10 )?? y = ( x 0.8 ) / 2; 21 Answer to Exercise II If 10 x < 100, then y = 4.6 + 0.2(x 10) 3 If x >= 100, then y = 1624.6 if ( x < 100 ) //( x >= 10 && x < 100 ) y = 4.6 + 0.2 * pow( x 10, 3); // if ( x >= 100)?? y = 1624.6; 22 Another Exercise A diagnostic test has the following result Score 75 take first course 65 score < 75 take two-week prep course Score < 65 take four-week prep course Complete the following code, using if s to print out the correct result int score; cout << Enter your score: ; cin >> score; 23 Another Exercise Solution if ( score >= 75 ) cout << Take college course ; if ( score >= 65 ) cout << Take two-week prep course ; cout << Take four-week prep course ; 24 4

Use of if versus if--if x = 0; if ( a == 0 && b == 0 ) x = 1; What is difference between code above and code below? x = 0; if ( a == 0 && b == 0 ) x = 1; 25 Use of if versus if--if II x = 0; if ( a == 0 && b == 0 ) x = 1; Code above has two separate ifs Braces not needed since there is only one for each if Second if is always executed and, in fact, is only one that matters if a, b, and c are all zero we will get x = 1 with this code 26 Use of if versus if--if III Code below is a single if--if Braces not necessary here If a, b, and c are all zero we set x= 0 and exit the if--if structure If a and b are zero and c is nonzero we set x = 1 and exit the structure x = 0; if ( a == 0 && b == 0 ) x = 1; 27 Another Exercise Credit, no-credit grading rules Graduate students: B- or better is credit Undergraduates: C- or better is credit Data system has string variable status (grad or ugrd) and grade variable as type double (1.7/2.7 for C-/B-) Write code to examine these variables and print correct value of credit or no credit 28 Another Exercise Solution if ( status == grad && grade >= 2.7 ) cout << Grade is credit. ; if ( status == ugrd && grade >= 1.7 ) cout << Grade is credit. ; cout << Grade is not credit. ; 29 5