Relational & Logical Topics Operators, if and switch Statements Relational Operators and Expressions The if Statement The if-else Statement Nesting of if-else Statements switch Logical Operators and Expressions Truth Tables
1 2
Practice with Relational Relational Operators Expressions
< less than var a = 1, b = 2, c = 3 ; > greater than <= less than or equal to >= greater than or equal to Expression true/false Expression true/false == is equal to != is not equal to a < c a + b >= c b <= c a + b == c Relational expressions evaluate to true or false. c <= a a != b All of these operators are called binary operators a > b a + b != c because they take two expressions as operands. b >= c
3 4
Arithmetic Expressions: True Practice with Arithmetic or False Expressions
Arithmetic expressions evaluate to numeric var a = 1, b = 2, c = 3 ; values . var x = 3.33, y = 6.66 ; An arithmetic expression that has a value of Expression Numeric Value True/False zero is false. a + b An arithmetic expression that has a value b - 2 * a other than zero is true. c - b - a c - a y - x 5 y - 2 * x 6 Review: Structured Programming Selection: the if statement
All programs can be written in terms of only if( condition ) three control structures { The sequence structure statement(s) // body of if statement Unless otherwise directed, the statements are } executed in the order in which they are written. The selection structure The braces are not required if the body contains only Used to choose among alternative courses of action. a single statement. However, they are a good idea The repetition structure and are required by the 104 C Coding Standards. Allows an action to be repeated while some condition remains true.
7 8
Examples Alert Screenshot
if(value == 0) { alert("You entered zero."); } 9 10
Selection: the if-else Good Programming Practice statement
Always place braces around the body of an if if( condition ) statement. { statement(s) /* the if clause */ Advantages: } Easier to read else Will not forget to add the braces if you go back { and add a second statement to the body statement(s) /* the else clause */ Less likely to make a semantic error } Indent the body of the if statement 2 to 3 Note that there is no condition for the else. spaces -- be consistent!
11 12 Example Another Example
if(age >= 18) if(value == 0) { { alert("Go Vote!"); alert("You entered zero."); } } else else { { alert("Maybe next time!"); alert("Value = " + value); } }
13 14
Good Programming Practice Nesting of if-else Statements
if(condition1) Always place braces around the bodies of the { if and else clauses of an if-else statement. statement(s) Advantages: } else if(condition2) Easier to read { Will not forget to add the braces if you go back statement(s) and add a second statement to the clause } Less likely to make a semantic error . . . /* more else if clauses may be here */ else Indent the bodies of the if and else clauses 2 { to 3 spaces -- be consistent! statement(s) /* the default case */
15 } 16
Another Example Gotcha! = versus ==
var a = 2; if(value == 0) { if(a = 1) /* semantic (logic) error! */ alert("You entered zero."); { } alert("a is one"); else if(value < 0) } { else if(a == 2) alert(value + " is negative."); { alert("a is two"); } } else else { { alert(value + " is positive."); alert("a is " + a); 17 18 } } Multiple Selection with if Multiple Selection with if-else
(continued) if (day == 0 ) { if (day == 0 ) { alert ("Sunday") ; alert ("Sunday") ; if (day == 4) { } else if (day == 1 ) { This if-else structure is more } alert ("Thursday") ; alert ("Monday") ; if (day == 1 ) { } } else if (day == 2) { efficient than the corresponding if (day == 5) { alert ("Monday") ; alert ("Tuesday") ; if structure. Why? alert ("Friday") ; } else if (day == 3) { } } if (day == 2) { alert ("Wednesday") ; if (day == 6) { } else if (day == 4) { alert ("Tuesday") ; alert ("Saturday") ; alert ("Thursday") ; } } } else if (day == 5) { if (day == 3) { if ((day < 0) || (day > 6)) { alert ("Friday") ; alert ("Wednesday") ; alert("Error - invalid day.") ; } else if (day == 6) { } } alert ("Saturday") ; } else { 19 alert ("Error - invalid day.") ; 20 }
The switch Multiple-Selection Structure switch Example
switch ( day ) switch ( expression ) { { case 0: alert ("Sunday") ; case value1 : break ; Is this structure more case 1: alert ("Monday") ; statement(s) break ; efficient than the break ; case 2: alert ("Tuesday") ; break ; equivalent nested if-else case value2 : case 3: alert ("Wednesday") ; statement(s) break ; structure? break ; case 4: alert ("Thursday") ; break ; . . . case 5: alert ("Friday") ; default : break ; statement(s) case 6: alert ("Saturday") ; break ; break ; default: alert ("Error -- invalid day.") ; } break ; } 21 22
switch Statement Details Good Programming Practices
The last statement of each case in the switch Include a default case to catch invalid data. almost should always be a break. Inform the user of the type of error that has The break causes program control to jump to occurred (e.g., "Error - invalid day."). the closing brace of the switch structure. If appropriate, display the invalid value. Without the break, the code flows into the If appropriate, terminate program execution next case. This is almost never what you (discussed in CMSC 201). want. A switch statement will work without a default case, but always consider using one.
23 24 Why Use a switch Statement? Logical Operators
So far we have seen only simple conditions. A switch statement can be more efficient than if ( count > 10 ) . . . an if-else. Sometimes we need to test multiple conditions in order to make a A switch statement may also be easier to decision. read. Logical operators are used for combining simple conditions to make complex conditions. Also, it is easier to add new cases to a switch statement than to a nested if-else structure. && is AND if (x > 5 && y < 6) || is OR if (z == 0 || x > 10)
! is NOT if (!(bob > 42))
25 26
Example Use of && Truth Table for &&
Expression 1 Expression 2 Expression 1 && Expression 2 if(age < 1 && gender == "f") 0 0 0 { 0 nonzero 0 alert ("You have a baby girl!"); nonzero 0 0 } nonzero nonzero 1
Exp 1 && Exp 2 && … && Exp n will evaluate to 1 (true) only if ALL subconditions are true.
27 28
Example Use of || Truth Table for ||
Expression 1 Expression 2 Expression 1 || Expression 2 if(grade == "D" || grade == "F") 0 0 0 { 0 nonzero 1 alert ("See you next semester!"); nonzero 0 1 } nonzero nonzero 1
Exp 1 || Exp 2 || … || Exp n will evaluate to 1 (true) if only ONE subcondition is true.
29 30 Example Use of ! Truth Table for !
Expression ! Expression if(!(age >= 18)) /*same as (age < 18)*/ { 0 1 alert("Sorry, you can’t vote."); nonzero 0 } else { alert("You can vote."); }
31 32
Operator Precedence and Associativity Some Practice Expressions
var a = 1, b = 0, c = 7; Precedence Associativity Expression True/False a ( ) left to right/inside-out b * / % left to right a + b + (addition) - (subtraction) left to right a && b a || b < <= > >= left to right !c == != left to right !!c && left to right a && !b a < b && b < c || left to right a > b && b < c a >= b || b > c = right to left 33 34
More Practice
Given var a = 3, b = 7, c = 21 ;
evaluate each expression as true or false.
1. c / b == 2 2. c % b <= a % b 3. b + c / a != c – a 4. (b < c) && (c == 7) 5. (c + 1 - b == 0) || (b = 5)
35