Download presentation
Presentation is loading. Please wait.
Published byEvelyn French Modified over 8 years ago
2
Copyright © 2004-2012 Curt Hill The C++ IF Statement The most important decision statement Part 1
3
Copyright © 2004-2012 Curt Hill Flow of Control in C/C++/Java Recall the flow statements in C: –Decisions If Switch Case –Loops for while do Break Here we consider the if
4
Copyright © 2004-2012 Curt Hill Why? There are too many times when we need a conditional statement –One statement is executed in one set of circumstances and another otherwise We also want our programs to be robust –Unlikely to abnormally terminate regardless of what the user does
5
Copyright © 2004-2012 Curt Hill Example Suppose the following statement: avg = total/count; If count is zero the program will abnormally terminate with a divide by zero exception The solution is nesting this in an if: if(count > 0) avg = total/count;
6
Copyright © 2004-2012 Curt Hill Example Revisited The basic form is: if (cond) statement; The if is a reserved word The parentheses are required –They show the limit of the condition Only a single statement is allowed
7
Copyright © 2004-2012 Curt Hill A Condition Any expression Usually of type boolean Usually a comparison May include boolean methods May include boolean operators Other possibilities also exist
8
Copyright © 2004-2012 Curt Hill Comparisons A comparison has the form: expr op expr Expr may be any expression –Involves constants, variables, operators –Produces a value The comparison operators include: – >= <= == != Do not confuse = with == The precedence of comparisons is lower than any arithmetic and higher than assignment
9
Copyright © 2004-2012 Curt Hill Example comparisions: OK: if(a b*2) If(a*b<c-4*d) // Not so simple Not so good if (a = b) Even worse if(a<b<c)
10
Copyright © 2004-2012 Curt Hill Mixed mode again The same mixed mode rules apply to comparisons as arithmetic Comparing an integer to a double automatically casts the integer as a double for the purposes of the comparisons The arithmetic may generate casts The comparisons may generate additional ones
11
Copyright © 2004-2012 Curt Hill Example Now that the minimal basics are complete, lets look at an example program We will write a program that evaluates the quadratic formula to find the zeros of a quadratic equation
12
Copyright © 2004-2012 Curt Hill Quadratic Formula For those of you who forgot Any equation that may be put into this form: ax 2 + bx + c = 0 has the following solution:
13
Copyright © 2004-2012 Curt Hill Quadratics Quadratics take the form of a parabola This parabola may point up or down It may have zero, one or two solutions
14
Copyright © 2004-2012 Curt Hill The Discriminant The key item here is the expression under the radical b 2 – 4ac which determines how many solutions If it is negative, no real solution If it is zero, one real solution If it is positive, two real solutions Thus it is the focus of the ifs
15
Copyright © 2004-2012 Curt Hill The Solution The C++ program needs three distinct paths Each path is chosen based on the value of the discriminant Only one of these evaluates the square root Lets write the program!
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.