Download presentation
Presentation is loading. Please wait.
1
© Janice Regan, CMPT 102, Sept. 2006 0 CMPT 102 Introduction to Scientific Computer Programming Conditional Statements Control Structures
2
© Janice Regan, CMPT 102, Sept. 2006 1 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
3
© Janice Regan, CMPT 102, Sept. 2006 2 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)
4
© Janice Regan, CMPT 102, Sept. 2006 3 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
5
© Janice Regan, CMPT 102, Sept. 2006 4 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
6
© Janice Regan, CMPT 102, Sept. 2006 5 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
7
© Janice Regan, CMPT 102, Sept. 2006 6 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 condition Statement 1; Statement n; T F ⋮
8
© Janice Regan, CMPT 102, Sept. 2006 7 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);
9
© Janice Regan, CMPT 102, Sept. 2006 8 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
10
© Janice Regan, CMPT 102, Sept. 2006 9 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; }
11
© Janice Regan, CMPT 102, Sept. 2006 10 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) }
12
© Janice Regan, CMPT 102, Sept. 2006 11 Flowchart: two way selection A selection structure uses a decision box, and sequence boxes. There may be multiple sequence boxes along each path condition Statement 1; Statement n; T F Statement 1; Statement n; ⋮ ⋮
13
© Janice Regan, CMPT 102, Sept. 2006 12 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”;
14
© Janice Regan, CMPT 102, Sept. 2006 13 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
15
© Janice Regan, CMPT 102, Sept. 2006 14 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; }
16
© Janice Regan, CMPT 102, Sept. 2006 15 if-else Statement Syntax Selection structure for C Formal syntax: if ( ) else Note each alternative is only ONE statement! To have multiple statements execute in either branch use compound statement
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 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”;
19
© Janice Regan, CMPT 102, Sept. 2006 18 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
20
© Janice Regan, CMPT 102, Sept. 2006 19 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; }
21
© Janice Regan, CMPT 102, Sept. 2006 20 Multiway if-else: Example Not new, just different indenting Avoids "excessive" indenting Syntax:
22
© Janice Regan, CMPT 102, Sept. 2006 21 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.
23
© Janice Regan, CMPT 102, Sept. 2006 22 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
24
© Janice Regan, CMPT 102, Sept. 2006 23 Binary Relational Operators in C <less than <=less than or equal to > greater than >= greater than or equal to == equal to !=not equal to Evaluated left to right Binary Equality Operators in C
25
© Janice Regan, CMPT 102, Sept. 2006 24 Binary Logical Operators &&Logical AND ||Logical OR !Not Evaluated left to right Arguments of logical operators have values of true or false Unary Logical Operators
26
© Janice Regan, CMPT 102, Sept. 2006 25 Truth Table && The && (And) operator EXPRESSION1 EXPRESSION2 EXPRESSION1 && EXPRESSION2 T T T T F F F T F F F F
27
© Janice Regan, CMPT 102, Sept. 2006 26 Truth Tables || The || (Inclusive Or) operator EXPRESSION1 EXPRESSION2 EXPRESSION1 || EXPRESSION2 T TT T FT F TT F F F
28
© Janice Regan, CMPT 102, Sept. 2006 27 Truth Tables ^ The ^ (Exclusive or) operator EXPRESSION1 EXPRESSION2 EXPRESSION1 ^ EXPRESSION2 T T F T F T F TT F FF
29
© Janice Regan, CMPT 102, Sept. 2006 28 Truth Tables ! The ! (Not) operator EXPRESSION1 ! EXPRESSION1 T F F T
30
© Janice Regan, CMPT 102, Sept. 2006 29 Precedence of operators in C ( ) []. innermost first ++ -- (pre) + - ! ~(unary) (right to left) * / % + - >= == != && || = += -= *= /= %=(right to left)
31
© Janice Regan, CMPT 102, Sept. 2006 30 Precedence Examples Arithmetic before logical x + 1 > 2 || 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
32
© Janice Regan, CMPT 102, Sept. 2006 31 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 A > B
33
© Janice Regan, CMPT 102, Sept. 2006 32 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 < B) || B<A A < B && B < C++
34
© Janice Regan, CMPT 102, Sept. 2006 33 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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.