review the following: if-else one branch if conditional operators logical operators switch statement...

24
Conditional Statements Review Lesson 3

Upload: ann-randall

Post on 04-Jan-2016

239 views

Category:

Documents


0 download

TRANSCRIPT

Compiling Microsoft Visual C++ 2008 Lesson 2

Conditional Statements ReviewLesson 3

In this module, we will review the different types of conditional statements. Then, well write a program to show you how they are used.1ObjectivesReview the following:

if-elseOne branch ifConditional operatorsLogical operatorsSwitch statementConditional expression operator Nested ifsif else ifProgram using conditional statements

We will review the syntax of the following statements: if-else, one branch if, conditional operators, logical operators, switch statement, conditional expression operator, nested ifs, if-else if. After reviewing material, well write a program to demonstrate how conditional statements are used in a program.2if-else if ( condition ) { //code for true branch }else { //code for false branch }

conditionTrue branchFalse branchTF

The if else construct is probably the most very common conditional statement. You write the keyword if and inside a set of parentheses, you write the condition. Right afterwards, enclosed inside a set of { }, you write the the set of code that is performed if the condition is true. Then, you write the word else followed by a set of code in another set of { }s that is performed if the condition is false. Notice the code for the true branch and the false branch are indented for readability purposes.3if-else Exampleif ( x >= y ) { cout greater than=greater than or equal, =, 7 ) { x = x +3; cout 7, we add 3 to x and we print out the contents of x. If x b && c < d ) { cout 18 ) {cout bX=1ffif (a > b) { if ( c > d) { X = 3; } else { X = 2; } }else { X = 1; }

Nested ifs are if statements inside of if statements. In our example, if a >b and c >d, x will be initialized to 3. If a >b and c is not > d, x will be 2. If a 89) cout 79 ) cout 69) cout 79g> 69Grade = BadGrade = AGrade = BGrade = C

The if else-if is a variation of the if else statement. In this example, if g > 89, the person gets an A. If g is not >89, we see if it is > 79, if it is, the person gets a B. If g 69, he gets a grade of C. If g 0 && a != 0Calculate 2 roots r1,r2r1,r2Roots imaginary or a == 0TF

The flowchart here shows you the steps and order necessary to solve the quadratic equation. 1) First we read in the values for a, b and c; 2) we calculate the discriminant which is the b2-4ac. 3) we check to be sure that the disciminant is > 0 and that a is not 0. We need to perform this test because you cannot take the square root of a negative number or divide by zero. 4) if the test is false, we print a message saying we cannot solve the problem 5) if we have good data, we can calculate the 2 roots and print out the solution.17Code#include #include using std::cin; using std::cout; using std::endl;

int main() { double a, b, c; cout > a >> b >> c;

double d = b * b - 4.0 * a * c; if ((d >= 0.0) && (a != 0.0)) { double x1 = (-b + sqrt(d)) / (2.0 * a); double x2 = (-b - sqrt(d)) / (2.0 * a); cout