Presentation is loading. Please wait.

Presentation is loading. Please wait.

If/Else Statements.

Similar presentations


Presentation on theme: "If/Else Statements."— Presentation transcript:

1 If/Else Statements

2 Blocks A Block is a set of 0 or more statements between braces { } It allows several statements to syntactically operate as one statement.

3 Blocks Blocks usually mark the begin and end of some control statement, such as the main() program void main() { // 0 or more statements here }

4 Blocks Style 1: recommended
The braces should be on their own line, lined up with each other and the control statement, with all statements between indented: void main() { // 0 or more statements here, indented }

5 Blocks Style 2: common (space saver) Another common style is to put the { at the end of the control statement, the } on a line below, lined up with the control statement, and all lines between indented. void main() { // 0 or more statements here }

6 Conditional Expressions
A Conditional Expression is an expression that evaluates to true or false. Relational Operators are used to create conditional expressions. Logical Operators may also be used.

7 Relational Operators Relational Operators compare two values of compatible types and result in the value true or false. C++ operator Meaning < Less Than <= Less Than or Equal To > Greater Than >= Greater Than or Equal To == Equal To != Not Equal To

8 Relational Operators = vs. == x = 3 is a command: Change the value of x to 3 x == 3 is a question: Is x equal to 3? (yes or no)

9 Relational Operators Examples: 3 == 3.0 true 4 < 4 false string x=”Apple”; x == ”apple” false (case sensitive)

10 Logical Operators operate on one or two Conditional Expressions and result in true or false. C++ Operator Meaning and Syntax && AND: Syntax: CondEx && CondEx || OR: Syntax: CondEx || CondEx ! NOT: Syntax: !CondEx

11 Logical Operators Semantics: The Truth Table A B A && B A || B ! A
true false

12 Logical Operators Examples: (4 > 5) || (3 != 3) false (4 > 5) || (3 == 3) true (4 > 5) && (3 == 3) false (4 < 5) && (3 == 3) true !(4 != 5) false

13 if/else statements if/else statements are used to determine whether or not certain actions are carried out. They work the same as in Matlab, Python and other languages; the syntax is slightly different.

14 if/else statements Syntax: if (conditionalExpression) oneStatement_T else oneStatement_F The else oneStatement_F is optional. Note the parentheses are ALWAYS required.

15 if/else statements Syntax: oneStatement can be either: - Exactly one statement with a semicolon Or - A Block (no semicolon after the } )

16 if/else statements Semantics: When the conditionalExpression is true - execute oneStatement_T - skip oneStatement_F When the conditionalExpression is false - skip oneStatement_T - execute oneStatement_F (when it exists)

17 if/else statements Style: one statement if (conditionalExpression)
oneStatement_T; //indent else //line up with if oneStatement_F; //indent

18 if/else statements Style: blocks #1 if (conditionalExpression)
{ //line up with if statements //indent } //line up with { else oneStatement_F;

19 if/else statements Style: blocks #2 if (conditionalExpression) {
statements //indent } //line up with if else oneStatement_F;

20 if/else statements Style: blocks #2 if (conditionalExpression) {
statements //indent } //line up with if else oneStatement_F;

21 if/else statements Examples: int a = 3;
if ((a >= 2) || (a < 0)) Prints: cout << ”Apple\n”; Apple else Banana cout << ”Orange\n”; cout << ”Banana\n”; // STYLE doesn’t matter!

22 if/else statements Examples: int a = 3;
if ((a >= 2) || (a < 0)) Prints: cout << ”Apple\n”; Apple else { cout << ”Orange\n”; cout << ”Banana\n”; }

23 if/else statements Examples: int a = 3;
if ((a >= 2) && (a < 0)) { Prints: cout << ”Apple\n”; Banana cout << ”Orange\n”; } cout << ”Banana\n”;

24 if/else statements Examples: Nested if/else int a = 3, b = 2;
if (a < 0) Prints: if (b > 3) Banana cout << ”Apple\n”; else // proper style cout << ”Orange\n”; cout << ”Banana\n”;

25 if/else statements Examples: Nested if/else int a = 3, b = 2;
if (a < 0) Prints: if (b > 3) Banana cout << ”Apple\n”; else // improper style, but cout << ”Orange\n”; // no effect on semantics cout << ”Banana\n”;

26 if/else statements Examples: Nested if/else int a = 3, b = 2;
if (a < 0) { Prints: if (b > 3) Orange cout << ”Apple\n”; Banana } else cout << ”Orange\n”; cout << ”Banana\n”;

27 Multiway Decisions Instead of just “this or that”, sometimes one of several (3 or more) paths needs to be chosen. The following slide has an example of setting a letter grade based on a percentage (no D’s in this course!)

28 Multiway Decisions if (perc >= 90) // Multiway Style: grade = ’A’; // just a different else if (perc >=80) // style grade = ’B’; else if (perc >=70) grade = ’C’; else //default decision grade = ’F’;

29 Multiway Decisions if (perc >= 90) // ”Standard” Style grade = ’A’; // same Semantics else if (perc >=80) grade = ’B’; if (perc >=70) grade = ’C’; else //default decision grade = ’F’;

30 switch statement Used to make a multi-way decision when 1. The decision is based on ONE variable 2. The variable is not string

31 switch statement Syntax: switch (variable) { case literal1: statement(s) break; // optional case literal2: default: // optional statement(s) // optional }

32 switch statement Semantics: Top to bottom, checks if variable == literal If true: begins executing statement(s) does NOT stop at the next case If false: tries the next case break; means skip to end }

33 switch statement Semantics: default: Execute the following statement(s) if the variable did not match ANY literal

34 switch statement Style: Varies greatly depending on the problem being solved. As always, choose a style to make the code clear. It should allow for “quick spotting” of case literals.

35 switch statement Example: typical, compact style char letterGrade = ’C’; string s = ”?”; switch (letterGrade) { case ’A’: s = ”Excellent”; break; case ’B’: s = ”Good”; break; case ’C’: s = ”Average”; break; default: s = ”Invalid”; }

36 switch statement Example: multiple values per “case” (2 styles) char letterGrade = ’C’; string s=”?”; switch (letterGrade) { case ’A’: // long style case ’a’: s = ”Excellent”; break; case ’B’: case ’b’: s = ”Good”; break; }

37 switch statement Example: without breaks char letterGrade = ’B’; switch (letterGrade) { Prints: case ’A’: cout << ”Excellent\n”; Good case ’B’: cout << ”Good\n”; Average case ’C’: cout << ”Average\n”; Invalid default: cout << ”Invalid\n”; }

38 Vocabulary Term Definition Block
Set of 0 or more statements between braces. Syntactically acts as one statement Conditional Expression Expression that evaluates to TRUE or FALSE Relational Operator Operators that compare two values and evaluate to TRUE or FALSE. Ex: < <= > >= == != Logical Operator Operators that operate on one or two conditional expressions and evaluate to TRUE or FALSE. Ex: && || ! Nested When one statement is inside the control of another statement of the same type. Ex: if/else inside an if/else.


Download ppt "If/Else Statements."

Similar presentations


Ads by Google