CMPT 102 Introduction to Scientific Computer Programming Conditional Statements Control Structures © Janice Regan, CMPT 102, Sept. 2006 0 Flowcharts Flowcharts use some basic symbols To start or end a function To contain calculations To make decisions To connect different parts of an algorithm © Janice Regan, CMPT 102, Sept. 2006 1 Control Structures Three methods of processing a program In sequence Branching Looping Branch: Altering the flow of program execution by making a selection or choice Loop: Altering the flow of program execution by repetition of a particular block of statement(s) © Janice Regan, CMPT 102, Sept. 2006 2 Selection Based on Branching One-Way Selection One alternative: “Things to do” if a condition is true C implementation: if statement Two-Way Selection Two alternatives: “Things to do” if the condition is true, a different “things to do” if the condition is false C implementation: if-else statement Compound Statements Allows a group of statements to be considered as a block That block can replace a single statement in any selection structure © Janice Regan, CMPT 102, Sept. 2006 3 Selection Based on Branching Multiple Selections A series of different “things to do” if a series of conditions hold C implementation: if-elseif-else structure Alternate C implementation: Nested if statements Switch Structures A series of different “things to do” depending on the value of a particular variable © Janice Regan, CMPT 102, Sept. 2006 4 Decisions Use a decision statement when an action is to be taken only if a particular condition holds The condition which must hold may be logical or relational expression or a Boolean variable. The value of the condition must be True or False Each possible path through a condition statement will contain a sequence of steps to be executed The condition and the sequences of steps that are executed for each outcome of the condition statement form a selection structure. A selection structure is a type of control structure © Janice Regan, CMPT 102, Sept. 2006 5 Flowchart: one way selection A simple decision uses a decision box to hold the condition (Boolean value: relational or logical expression) The sequence of statements is held in a sequence box or boxes © Janice Regan, CMPT 102, Sept. 2006 condition T Statement 1; ⋮ Statement n; F 6 Example of one-way selection if statements An action is taken only if a particular condition is true. The action is described by a single C statement. Example: if (examScore < 50) fprintf(“Printing email address of failing student so they can be contacted to arrange tutoring: email is %s”, emailAddress); © Janice Regan, CMPT 102, Sept. 2006 7 Compound/Block Statement Only one statement following the if statement is part of the if control structure Must use a compound statement, implemented in C as { }, to include more than one statement in a list of things to do if the condition is true A compond statement can also be called a "block" statement You should always use a block statement { } Even if the block contains just one statement Using the { } if there is only one statement is a style decision, not a requirement of C © Janice Regan, CMPT 102, Sept. 2006 8 One way selection Do a series of actions only if a given condition holds If the condition does not hold skip the actions if ( condition ) { /* Series of actions to be taken */ /* when the condition is TRUE */ action 1; ⋮ action n; } © Janice Regan, CMPT 102, Sept. 2006 9 Compound Statement in Action Note indenting in this example: if (myScore > yourScore) { printf(“My score was higher than yours “); difference = myScore – yourScore; printf(“I got %d more points than you did”, difference) } © Janice Regan, CMPT 102, Sept. 2006 10 Flowchart: two way selection A selection structure uses a decision box, and sequence boxes. There may be multiple sequence boxes along each path condition F T Statement 1; ⋮ Statement n; Statement 1; ⋮ Statement n; © Janice Regan, CMPT 102, Sept. 2006 11 Example of two-way selection if-else statements Choice of two alternate statements based on condition expression Example: if (examScore > 50) myCourseGrade = “PASS”; else myCourseGrade = “FAIL”; © Janice Regan, CMPT 102, Sept. 2006 12 Compound/Block Statement Only one statement following the if statement is part of the if control structure Only one statement following the else statement is part of the control structure Must use a compound statement { } to include more than one statement Style Pointer: Each block (after the if and after the else) should have block statement even if the block contains just one statement © Janice Regan, CMPT 102, Sept. 2006 13 Two way selection Complete one of two possible series of actions First series of actions is complete if condition is true Second series of actions is completed if condition is false if (condition) { //Series of actions to be taken when the condition is TRUE action 1; ⋮ action n; } else { // Series of actions to be taken when the condition is FALSE action 1; ⋮ action n; } © Janice Regan, CMPT 102, Sept. 2006 14 if-else Statement Syntax Selection structure for C Formal syntax: if (<boolean_expression>) <yes_statement> else <no_statement> Note each alternative is only ONE statement! To have multiple statements execute in either branch use compound statement © Janice Regan, CMPT 102, Sept. 2006 15 Flowchart for multiple selection Statement 1; T condition F condition2 T ⋮ Statement n; Statement 1; ⋮ Statement n; F Statement 1; ⋮ Statement n; © Janice Regan, CMPT 102, Sept. 2006 16 Example of Multiple selection if-else statements Choice of two alternate statements based on condition expression Example: if (examScore > 80) myCourseGrade = “A”; else if (examScore > 60) myCourseGrade = “C”; else myCourseGrade = “FAIL”; © Janice Regan, CMPT 102, Sept. 2006 17 Compound/Block Statement Only one statement following the if statement is part of the if control structure Only one statement following the else statement is part of the control structure Only one statement following the else if statement of the control structrue Must use a compound statement { } to include more than one statement Style Pointer: Each block should have block statement even if it contains one statement © Janice Regan, CMPT 102, Sept. 2006 18 Multiple Selections (else if) if (condition1) { // Series of actions to be taken when condition 1 is TRUE action 1; action n; } else if (condition 2) { // actions to be taken when condition 1 is FALSE and condition2 is TRUE action 1; action n; } else { // Series of actions to be taken when condition 1and condition 2 are FALSE action 1; action n; } © Janice Regan, CMPT 102, Sept. 2006 19 Multiway if-else: Example Not new, just different indenting Avoids "excessive" indenting Syntax: © Janice Regan, CMPT 102, Sept. 2006 20 CMPT 102 Programming Style When using decision structures (if statements) you should always use the { } regardless of whether you use a single or multiple statements in each branch of the if statement. The opening and closing brackets for the block should each be written on their own line. (examples follow) WHY? As your code evolves it is common to add statements (functionality) within a decision statement. When you add statements and forget to add the {} to indicate the extent of the block unexpected things happen. The resulting problems can be difficult to find. © Janice Regan, CMPT 102, Sept. 2006 21 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 is a boolean expression and 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. 2006 22 Binary Relational Operators in C < <= > >= less than less than or equal to greater than greater than or equal to Binary Equality Operators in C == != equal to not equal to Evaluated left to right © Janice Regan, CMPT 102, Sept. 2006 23 Binary Logical Operators && || Logical AND Logical OR Unary Logical Operators ! Not Evaluated left to right Arguments of logical operators have values of true or false © Janice Regan, CMPT 102, Sept. 2006 24 Truth Table && The && (And) operator EXPRESSION1 EXPRESSION2 T T F F © Janice Regan, CMPT 102, Sept. 2006 T F T F EXPRESSION1 && EXPRESSION2 T F F F 25 Truth Tables || The || (Inclusive Or) operator EXPRESSION1 EXPRESSION2 T T F F © Janice Regan, CMPT 102, Sept. 2006 T F T F EXPRESSION1 || EXPRESSION2 T T T F 26 Truth Tables ^ The ^ (Exclusive or) operator EXPRESSION1 EXPRESSION2 T T F F © Janice Regan, CMPT 102, Sept. 2006 T F T F EXPRESSION1 ^ EXPRESSION2 F T T F 27 Truth Tables ! The ! (Not) operator EXPRESSION1 ! EXPRESSION1 T F © Janice Regan, CMPT 102, Sept. 2006 F T 28 Precedence of operators in C ( ) [] . ++ -- (pre) + - ! ~(unary) * / % + < <= > >= == != && || = += -= *= /= %= © Janice Regan, CMPT 102, Sept. 2006 innermost first (right to left) (right to left) 29 Precedence Examples Arithmetic before logical x + 1 > 2 || x + 1 < -3 means: (x + 1) > 2 || ( x + 1) < -3 Short-circuit evaluation (x >= 0) && (y > 1) Be careful with increment operators! (x > 1) && x<y++ In C Boolean values (true and false) are represented as integers All non-zero integer values true Zero value false © Janice Regan, CMPT 102, Sept. 2006 30 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) >= C Let A=9, B=5, C=2 A * B <= C A % C == A % B A != -C A>B © Janice Regan, CMPT 102, Sept. 2006 31 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 < B) && A >= C (A < B) || A > C !(A < B) || B<A A < B && B < C++ © Janice Regan, CMPT 102, Sept. 2006 32 Common Pitfalls in C Operator "=" vs. operator "==" One means "assignment" (=) One means "equality" (==) These two operators are VERY different Example of common error: if (x = 12) Note operator used! Do_Something else Do_Something_Else © Janice Regan, CMPT 102, Sept. 2006 33