Lecture 3 Control Structures Selection BJ Furman 10
Lecture 3: Control Structures - Selection BJ Furman 10 SEP 2012
The Plan for Today n Review from last two weeks ¨ Flowcharts ¨ Pseudocode ¨ Data types Variables and Constants Structure of a C program Formatted output: printf( ) ¨ ¨ ¨ n n n Operators and their precedence Review control structures ¨ Sequence ¨ Selection ¨ Repetition Selection structures ¨ If/else ¨ Switch Relational operators Selection structure example
Learning Objectives n Apply concepts for developing algorithms, using variables, and structuring a C program n Explain what is meant by a control structure n Explain the three basic types of control structures n Determine the result of relational comparisons n Apply the if and if/else control structures
Control Structures - Review n All programs can be written in terms of three control structures (like building blocks) ¨ Sequence n ‘Built-in’ to C ¨ ¨ Selection (three types) n Depending on a condition, select between one statement or another ¨ ¨ Unless otherwise directed, one statement after the next is executed If var 1 is greater than 10, do this…, else do that… Repetition (three types) n Depending on a condition, execute one or more statements repeatedly
Selection Structure Overview n Three kinds of selections structures ¨ if (also called, ‘single-selection’) n if condition is true Perform action n ¨ ¨ if condition is false, action is skipped, program continues if/else (also called, ‘double-selection’) n if condition is true n Perform action else (if condition is false) Perform a different action (this will be skipped if condition is true) switch (also called ‘multiple-selection’) n Allows selection among many actions depending on the integral value of a variable or expression
Single Selection IF - Flowchart connector flow line action symbol decision symbol Speed > 65 FALSE TRUE Print “You’re speeding”
Relational Operators Important for constructing the decision expression Practice 5 < 7 result is ____ 5 > 7 result is _____ 7 <= 7 result is ____ 8 >= 7 result is ____ 5 == 5 result is ____ 5 == 7 result is ____ var 1 = 7 result is____ 5. 0 == 5 result is ___ 6 != 5 result is ____ Adapted from H. Cheng chap 04. ppt, slide 5
Double-Selection IF - Flowchart Print “Within speed limit” Speed > 65 FALSE TRUE Print “Over speed limit”
SWITCH - Flowchart Adapted from Deitel & Deitel, C How to Program, 6 th ed. , p. 111
IF statement (single-selection) Syntax if(expression) statement 1; statement 2; n Notes n ¨ ¨ /* then execute this statement */ /* execute this statement next*/ Indent the statements for clarity Can have multiple statements n Enclose a ‘block’ of statements using { } (curly braces) if( x <= 2 ) { /* if expression is TRUE (i. e. , NOT EQUAL to zero) */ statement 1; statement 2; } statement 3;
IF statement example n Pseudocode (notice indentation!) If speed is greater than 65 mph print “You’re speeding!” n C code if(speed > 65) printf(“You’re speeding!n”); n C code with multiple statement block if(speed > 65) /* statements below executed only if speed > 65 is true */ { } printf(“You’re speeding!n”); printf(“Slow down!n”); printf(“Keep speed below 65 MPHn”);
IF-ELSE statement - Double Selection n Syntax if(expression) statement 1; else statement 2; Notes: /* if expression is TRUE (i. e. , NOT EQUAL to zero) */ /* execute this statement */ /* else execute the following statement */ If expression is non-zero, statement 1 is executed, then the program continues with the statement after statement 2, i. e. , statement 2 is skipped ¨ If expression is equal to zero, statement 1 is skipped and statement 2 is executed, then the program continues with the statement after statement 2 ¨
IF-ELSE statement example n Pseudocode (notice indentation!) If speed is greater than 65 mph print “Over speed limit!” else print “Within speed limit” n C code if(speed > 65) printf(“Over speed limit!n”); else printf(“Within limitn”);
Compound Condition - && n Logical operators for more complex decisions ¨ Logical AND operator && (double ampersand) n if(switch 1 = = 0 && switch 2 = = 1) turn on the motor n The condition evaluates to TRUE if and only if BOTH expressions on either side of && evaluate to TRUE ¨ n Note operator precedence Otherwise condition evaluates to FALSE ¨ Beware of ‘short circuit evaluation’ § Make the condition most likely to be FALSE the leftmost condition
Compound Condition - | | n Logical operators for more complex decisions, cont. ¨ Logical OR operator | | (double vertical bar) n if(switch 1 = = 0 || switch 2 = = 1) turn on the motor n The condition evaluates to TRUE if one or the other or both expressions on either side of && evaluate to TRUE ¨ n Note operator precedence Otherwise condition evaluates to FALSE ¨ Beware of ‘short circuit evaluation’ § Make the condition most likely to be TRUE the left-most condition
Nesting selection structures n Selection structures can Grade Determination for Overall Percentage (OP) be stacked and nested OP >= 90 ‘A’ to handle more sophisticated 80 <= OP < 90 ‘B’ decision/action 70 <= OP < 80 ‘C’ functionality ¨ Ex. Figuring grades n Pseudocode Notes: ¨ 60 <= OP < 70 ‘D’ OP < 60 ‘F’ “an else is always associated with the nearest previous if” (Darnell & Margolis, 1996) ¨ Use braces ({ })to clarify the association of the else for other situations where the decision structure is more complicated Adapted from Deitel & Deitel, C How to Program, 3 rd ed. , p. 64
Nesting If/else – C Code – Two Ways Or Adapted from Deitel & Deitel, C How to Program, 3 rd ed. , p. 64
SWITCH n Good when faced with testing multiple alternatives that depend on a single variable ¨ The test is done once n Must be an integral expression ¨ ¨ ¨ case items must be constant integral expressions n ¨ int or char NOT float, double No variables The structure is very organized and readable
SWITCH - Flowchart Adapted from Deitel & Deitel, C How to Program, 6 th ed. , p. 111
Practice - 1 n n Pair up with someone next to you that you do not know Develop an algorithm for: ¨ n n n Finding and printing out the largest of two numbers (3 min) One person work on the pseudocode, the other on a flowchart (1 min) Compare pseudocode and flowchart (3 min) Write out the algorithm in C
Practice - 2 n Develop an algorithm for the ignition control in a car: Requirements: ¨ The starter will only start when: Key must be in the ignition slot n Transmission selector must be in ‘Park’ n Key must be turned to ‘Start’ position n ¨ The starter is energized with the statement starter_on();
Review
References Darnell, P. A. & Margolis, P. E. (1996) C, a software engineering approach, 3 rd ed. , Springer, New York. n Cheng, H. H. (2010). C for Engineers and Scientists: An Interpretive Approach, Mc. Graw-Hill, New York. n Deitel, H. M. & Deitel, P. J. (2001). C How to Program, 3 rd ed. , Prentice-Hall, New Jersey. n
Nesting selection structures n Selection structures can be stacked and nested to handle more sophisticated decision/action functionality /* File: ifc. c */ #include <stdio. h> int main () { int i; i = 10; if(i==2 || i == 4) { printf("i = 2 or 4n"); } else if(i == 10) { printf("i = 10n"); } else { printf("i = %dn", i); } return 0; } Adapted from H. Cheng chap 05. ppt, slide 12
Operators Adapted from H. Cheng chap 04. ppt, slide 5
- Slides: 25