Presentation is loading. Please wait.

Presentation is loading. Please wait.

© Janice Regan, CMPT 102, Sept. 2006 0 CMPT 102 Introduction to Scientific Computer Programming Logical and Relational Expressions Nested if statements.

Similar presentations


Presentation on theme: "© Janice Regan, CMPT 102, Sept. 2006 0 CMPT 102 Introduction to Scientific Computer Programming Logical and Relational Expressions Nested if statements."— Presentation transcript:

1 © Janice Regan, CMPT 102, Sept. 2006 0 CMPT 102 Introduction to Scientific Computer Programming Logical and Relational Expressions Nested if statements Case statements

2 © Janice Regan, CMPT 102, Sept. 2006 1 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; }

3 © Janice Regan, CMPT 102, Sept. 2006 2 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

4 © Janice Regan, CMPT 102, Sept. 2006 3 Precedence of operators in C  ( ) []. innermost first  ++ -- (pre) + - ! ~(unary) (right to left)  * / %  + -  >=  == !=  &&  ||  = += -= *= /= %=(right to left)

5 © Janice Regan, CMPT 102, Sept. 2006 4 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

6 © Janice Regan, CMPT 102, Sept. 2006 5 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

7 © Janice Regan, CMPT 102, Sept. 2006 6 Expressions: relational operators  (A + B) >= C X >= C  Let A=9, B=5, C=2 + A B X C >= 9 14 5 2 T A B C + >= Value of expression is Boolean:

8 © Janice Regan, CMPT 102, Sept. 2006 7 Expressions: relational operators  A * B <= C X <= C  Let A=9, B=5, C=2 * A B X C <= 9 35 5 2 F A B C * <= Value of expression is Boolean:

9 © Janice Regan, CMPT 102, Sept. 2006 8 Expressions: relational operators  Let A=9, B=5, C=2  A % C == A % B X == A % B X == Y % A C X 9 2 1 Y A 9 5 % B == F A C % % B 4 Value of expression

10 © Janice Regan, CMPT 102, Sept. 2006 9 Expressions: relational operators  A != -C A != X  Let A=9, B=5, C=2 != A X C - 9 -2 2 T A C - != Value of expression

11 © Janice Regan, CMPT 102, Sept. 2006 10 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 + 10 15 5 2 7 Value of expression

12 © Janice Regan, CMPT 102, Sept. 2006 11 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++

13 © Janice Regan, CMPT 102, Sept. 2006 12 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

14 © Janice Regan, CMPT 102, Sept. 2006 13 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

15 © Janice Regan, CMPT 102, Sept. 2006 14 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 !

16 © Janice Regan, CMPT 102, Sept. 2006 15 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

17 © Janice Regan, CMPT 102, Sept. 2006 16 Flowchart for multiple selection condition2 Statement 1; Statement n; T F Statement 1; Statement n; condition F Statement 1; Statement n; T

18 © Janice Regan, CMPT 102, Sept. 2006 17 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

19 © Janice Regan, CMPT 102, Sept. 2006 18 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; }

20 © Janice Regan, CMPT 102, Sept. 2006 19 Flowchart for multiple selection condition2 Statement 1; Statement n; T F Statement 1; Statement n; condition F Statement 1; Statement n; T

21 © Janice Regan, CMPT 102, Sept. 2006 20 Flowchart for multiple selection Statement 1; Statement n; T F condition

22 © Janice Regan, CMPT 102, Sept. 2006 21 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; }

23 © Janice Regan, CMPT 102, Sept. 2006 22 Flowchart for multiple selection condition2 Statement 1; Statement n; F T Statement 1; Statement n; condition F Statement 1; Statement n; T

24 © Janice Regan, CMPT 102, Sept. 2006 23 Flowchart for multiple selection Statement 1; Statement n; F T condition

25 © Janice Regan, CMPT 102, Sept. 2006 24 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

26 © Janice Regan, CMPT 102, Sept. 2006 25 switch Structures switch(controling expression) { case value1: statements1; break; case value2: statements2; break;... case valuen: statementsn; break; default: statements; }

27 © Janice Regan, CMPT 102, Sept. 2006 26 switch Statement expression = value1 expression = value2 expression = valuen

28 © Janice Regan, CMPT 102, Sept. 2006 27 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++; }


Download ppt "© Janice Regan, CMPT 102, Sept. 2006 0 CMPT 102 Introduction to Scientific Computer Programming Logical and Relational Expressions Nested if statements."

Similar presentations


Ads by Google