Presentation is loading. Please wait.

Presentation is loading. Please wait.

If Statements Sections 1.25, 2.4-2.6. Control Structures o All code thus far executes every line of code sequentially o We want to be able to repeat,

Similar presentations


Presentation on theme: "If Statements Sections 1.25, 2.4-2.6. Control Structures o All code thus far executes every line of code sequentially o We want to be able to repeat,"— Presentation transcript:

1 If Statements Sections 1.25, 2.4-2.6

2 Control Structures o All code thus far executes every line of code sequentially o We want to be able to repeat, to choose some lines of code o Three types of control: sequence, conditional, repetition/iteration

3 Types of control structures Sequence – default in C++, execute each instruction sequentially as it reached Conditional – choose whether to execute some C++ statement (if, if - else switch) Iteration – loop. Repeat some set of statements multiple times

4 Conditional Choose which statement to execute. Form: if (some condition is true), then do some action If (comparison) then (action) Many examples in English: If raining, wear raincoat. If cold, wear winter coat. When in Rome, do as the Romans (if in Rome, act like a Roman)

5 Conditionals in C++ If (comparison) then (action) Need a way to write the comparison and the action Comparison is boolean expression (evaluates to be true or false) Action is any (series of) C++ statements

6 Comparisons Boolean expressions can have many different forms: Using bool variables bool done=true; if (done) // then do some action Relational expression (compares values) Logical Expression (manipulates values with and, or, not) Can be combined

7 Relational Operators int x, y; x < y x <= y x > y x >= y x != y x == y // NOT x=y

8 Evaluating Boolean Expressions 0 is false Non-zero is true true, false int x=3; x is true. sometimes useful, but can cause problems x=3 is NOT comparing x and 3.

9 int x=3; int y = 4; int z=5; x < y x < y < z x = y y == 4 z >= x x != 3 (x + 4) < (y-1) Evaluating Boolean Expressions

10 int x=3; int y = 4; int z=5; x < y x < y < z x = y y == 4 z >= x x != 3 (x + 4) < (y-1) true true. x is now 4, 4 is nonzero, so this is true true false 7 < 3 false

11 Logical Operators and (&&, single & very different) both values must be true for the expression to be true if it is cold and rainy, wear your winter raincoat (if either isn't true, don't) or (|| - on keyboard, called pipe symbol) either value can be true if it is cold or rainy, wear a coat (if one is true, do) not (!) changes the truth value of the expression if it is not cold, do not wear a winter coat

12 Logical Operators int x=3; int y=10; (x < y) && (y < 20) (x == 3) || (y == 3) x < y; 3 < 10; true y < 20; 10 < 20; true true && true is true x == 3 true. short circuit evaluation (y==3 false true || false is true)

13 More logical operators int x=3; int y=10; !(y=10) (x != 3) || (y != 3) trick question y=10 is 10 (true); !true is false false x != 3 false y != 3 true false || true is true

14 Yet more logical operators int x=3; int y=10; !((x+1 < 4) || (y <= 10)) !((x+1 < 4) && (y <= 10)) x+1 = 4 4 < 4 false.keep going y <= 10 true false || true true ! true is false 4 < 4 false. DONE with &&. Do not look at y <=10. !false true

15 if statements if statement form: if (boolean expression) c++ statement if (x < y) cout << "x < y" << endl;

16 if statements cautions MUST have ()s around boolean expression no syntax error for non-boolean like expressions only ONE statement in an if statement no ';' after if condition Make sure you account for values that are equal

17 Your turn Write an if statement to assign x to y if x is greater than y Talk to your neighbor. See if you have the same answer Keep this paper.

18 if-else If you want to do one thing if a condition is true and something else if not, use if-else. form: if (condition) C++ statement else C++ statement if (x < y) cout << x << " is less than the other number"; else cout << y << " is less than the other number";

19 > one statement in an if If you want to have more than one statement inside an if or an else, use {}s: if (x < y) { cout << x << " is less than the other number"; x = 0; } else { cout << y << " is less than the other number"; y = 0; }

20 If-else cautions either if clause or else clause or both may have {}s. After statements inside if and else clause are executed, control passes back to next sequential statement no ';' after else Make sure you account for values that are equal

21 Watch Out if (3 < 4) x = 3; else cout << "3 < 4 is false" << endl; x = 0; cout << "the value of x is " << x << endl;

22 Embedded ifs If statements and if-else statements may be embedded (if within if). simply evaluate them as the C++ code is executed: if-else example is most common. sets up a table of conditions

23 Embedded if-else AverageGrade >90A 80-89B 70-79C 60-69D <60F

24 Embedded if-else for table if (ave >= 90) grade = 'A'; else if ((ave = 80)) // note: need ()s around entire condition grade = 'B'; else if ((ave =70)) grade = 'C'; else if ((ave =60)) grade = 'D'; else if ((ave < 70) && (ave < 60)) grade = 'F';

25 Tracing through the embeded if

26 Fixing the embedded if if (ave >= 90) grade = 'A'; else if (ave >= 80) // We know (ave < 90) or we wouldn't be here grade = 'B'; else if (ave >=70) // we know ave < 80 grade = 'C'; else if (ave >=60) grade = 'D'; else // if ((ave < 70) && (ave < 60)) grade = 'F';

27 Cautions for embedded ifs Don't use redundant comparisons Make sure you check for values that are equal Account for out of range values

28 Program style Put {}s on a line by themselves indent {}s 2-3 spaces, statements one more than that All code outside if statements should line up All code inside of if statements should line up.

29 More complicated embedded ifs if (x < 3) if (y < 6) cout << "x and y between 3 and 6"; else cout = 6"; else if (y > 6) cout << "x and y not in 3-6 range"; else cout = 3; y <= 6";

30 You do it Writing if statements (on the same paper you used earlier)


Download ppt "If Statements Sections 1.25, 2.4-2.6. Control Structures o All code thus far executes every line of code sequentially o We want to be able to repeat,"

Similar presentations


Ads by Google