© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Logical and Relational Expressions Nested if statements Case statements
© Janice Regan, CMPT 102, Sept Multiple Selections (if-else if-else) if (condition) {/* Series of actions to be taken when the condition is TRUE */ action 1; action n; } else if (condition 2) { /* actions to be taken when condition is FALSE and condition2 is TRUE */ action 1; action n; } else { /*Series of actions to be taken when condition and condition 2 are FALSE */ action 1; action n; }
© Janice Regan, CMPT 102, Sept Defining the condition Each decision statement is based upon a condition The condition is an expression with a logical value (true or false) The condition may be A relational expression (a type of logical expression) Two numerical values combined using a binary relational operator (a simple relational expression) A more complex relational expression Another type of logical expression Two logical values combined with a binary logical operator One logical value One logical value operated on by a unary logical operator A more complex logical expression
© Janice Regan, CMPT 102, Sept Precedence of operators in C ( ) []. innermost first (pre) + - ! ~(unary) (right to left) * / % + - >= == != && || = += -= *= /= %=(right to left)
© Janice Regan, CMPT 102, Sept Expressions with relational operators Value of a relational expression (expression including a relational or binary equality operator) is true or false. Arguments of a relational operator are numerical (or character) A < C (A + B) >= CLet A=9, B=5, C=2 A * B <= C A % C == A % B A != -C
© Janice Regan, CMPT 102, Sept Expressions: relational operators A < C Let A=9, B=5, C=2 A C < 9 2 F A C < Value of expression is Boolean: In C Boolean (T or F) is represented by an integer. T=0, F any non zero integer
© Janice Regan, CMPT 102, Sept Expressions: relational operators (A + B) >= C X >= C Let A=9, B=5, C=2 + A B X C >= T A B C + >= Value of expression is Boolean:
© Janice Regan, CMPT 102, Sept Expressions: relational operators A * B <= C X <= C Let A=9, B=5, C=2 * A B X C <= F A B C * <= Value of expression is Boolean:
© Janice Regan, CMPT 102, Sept Expressions: relational operators Let A=9, B=5, C=2 A % C == A % B X == A % B X == Y % A C X Y A 9 5 % B == F A C % % B 4 Value of expression
© Janice Regan, CMPT 102, Sept Expressions: relational operators A != -C A != X Let A=9, B=5, C=2 != A X C T A C - != Value of expression
© Janice Regan, CMPT 102, Sept Importance of order of operations Order of operations is determined by operator precedence rules () before / (A + B) / C X / C Let A=10, B=5, C=2 2 A B C + / + A B X C Value of expression
© Janice Regan, CMPT 102, Sept Expressions: logical operators Value of a logical expression (expression including a logical operator) is true or false. Arguments of the logical operator are also true or false Let A=9, B=5, C=2 Then (C = C (A C !(A C A < B && B < C++
© Janice Regan, CMPT 102, Sept Expressions: logical operators Let A=9, B=5, C=2 (C = C) X && (A >= C) X && Y < C B X 2 5 T Y A 9 2 >= C && T A C < >= B && T Value of expression
© Janice Regan, CMPT 102, Sept Expressions: logical operators Let A=9, B=5, C=2 (A C) X || (A > C) X || Y < A B X 9 5 F || T A C < > B Value of expression Y A 9 2 > C T T
© Janice Regan, CMPT 102, Sept Expressions: logical operators Let A=9, B=5, C=2 !(A C) !X || (A > C) Y || (A > C) Short Circuit < A B X 9 5 F Y T || F A B < ! C Value of expression !
© Janice Regan, CMPT 102, Sept Expressions: logical operators Let A=9, B=5, C=2 A < B && B < C++ X && B < C++ Short Circuit (increment not evaluated!) < A B X 9 5 F && F A B < C Value of expression
© Janice Regan, CMPT 102, Sept Flowchart for multiple selection condition2 Statement 1; Statement n; T F Statement 1; Statement n; condition F Statement 1; Statement n; T
© Janice Regan, CMPT 102, Sept Multiple Selections (if-else if-else) if (condition) {/* Series of actions to be taken when the condition is TRUE */ action 1; action n; } else if (condition 2) { /* actions to be taken when condition is FALSE and condition2 is TRUE */ action 1; action n; } else { /*Series of actions to be taken when condition and condition 2 are FALSE */ action 1; action n; } NEXT STATEMENT
© Janice Regan, CMPT 102, Sept Multiple Selections (Nested if) if (condition) { /* Series of actions to be taken when the condition is TRUE */ action 1; action n; } else { if (condition 2) { /* Series of actions to be taken when condition is FALSE and condition2 TRUE */ action 1; action n; } else { /* Series of actions to be taken when condition and condition 2 are FALSE */ action 1; action n; }
© Janice Regan, CMPT 102, Sept Flowchart for multiple selection condition2 Statement 1; Statement n; T F Statement 1; Statement n; condition F Statement 1; Statement n; T
© Janice Regan, CMPT 102, Sept Flowchart for multiple selection Statement 1; Statement n; T F condition
© Janice Regan, CMPT 102, Sept Multiple Selections (Nested if) if (condition) { if (condition 2) { /* Series of actions to be taken when condition is FALSE and condition2 TRUE */ action 1; action n; } else { /* Series of actions to be taken when condition and condition 2 are FALSE */ action 1; action n; } else { /* Series of actions to be taken when the condition is TRUE */ action 1; action n; }
© Janice Regan, CMPT 102, Sept Flowchart for multiple selection condition2 Statement 1; Statement n; F T Statement 1; Statement n; condition F Statement 1; Statement n; T
© Janice Regan, CMPT 102, Sept Flowchart for multiple selection Statement 1; Statement n; F T condition
© Janice Regan, CMPT 102, Sept The switch statement An alternative method to if statements with multiple else clauses The controlling expression (selector) must have an integral type Characters also work because they can be converted to integers Case labels are particular values of the controlling expression Break statements exit from the switch structure
© Janice Regan, CMPT 102, Sept switch Structures switch(controling expression) { case value1: statements1; break; case value2: statements2; break;... case valuen: statementsn; break; default: statements; }
© Janice Regan, CMPT 102, Sept switch Statement expression = value1 expression = value2 expression = valuen
© Janice Regan, CMPT 102, Sept Sample case statement /* This is in a loop that reads */ /* nextChar each time */ while ( endInput != nextChar ) { switch (nextChar) { case 0: count1++; break; case 1: count2++; break; case 2: count3++; break; case 4: count4++; break; case 5: count5++; break; case 6: count6++; break; case 7: count7++; break; default: countOther++; }