Week 3 Control Structures Selection BJ Furman 13SEP2010 The Plan for Today Comments on lab and HW assignments Review control structures Sequence Selection Repetition Selection structures If If else Switch Relational operators Selection structure example Comments on lab and HW assignments Turn in hardcopy of HW in lecture at the beginning Turn in softcopy of HW to D2L before the deadline Turn in hardcopy of lab report to your lab instructor during your lab session Turn in softcopy of lab report to D2L before the deadline Lab report for Week 2 lab is due one week after you get your first lab report back should be next week week of 9 20 10 Avoid plagiarism Re read the section in the syllabus on Academic Integrity D2L uses automatic plagiarism detection Do your own work Collaboration is encouraged but what you turn in must be yours Non original work must be properly cited Google how to cite sources Learning Objectives Explain what is meant by a control structure Explain the three basic types of control structures Determine the result of relational comparisons Apply the if and if else control structures Control Structures Review All programs can be written in terms of three control structures like building blocks Sequence Built in to C Unless otherwise directed one statement after the next is executed Selection three types Depending on a condition select between one statement or another If var1 is greater than 10 do this else do that Repetition three types Depending on a condition execute one or more statements repeatedly Selection Structure Three kinds of selections structures if also called single selection if condition is true Perform action if condition is false action is skipped if else also called double selection if condition is true Perform action else if condition is false Perform a different action switch also called multiple selection Allows selection among many actions depending on the value of a variable or expression IF statement single selection Syntax if expression statement1 statement2 Notes if expression is TRUE not equal to zero then execute this statement otherwise execute this statement Indent statements Can have multiple statements Enclose a block of statements using curly braces if x 2 statement1 statement2 statement3 IF statement example Pseudocode notice indentation If speed is greater than 65 mph print You re speeding C code if speed 65 printf You re speeding n C code with 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 MPH n Single Selection IF Flowchart connector flow line action symbol decision symbol Speed 65 FALSE TRUE Print You re speeding IF ELSE statement Double Selection Syntax if expression statement1 else statement2 Notes if expression is TRUE execute this statement else execute the following statement If expression is non zero statement1 is executed then the program continues with the statement after statement2 i e statement2 is skipped If expression is equal to zero statement1 is skipped and statement2 is executed then the program continues with the statement after statement2 Double Selection IF Flowchart FALSE Print Within limit Speed 65 TRUE Print Over speed limit IF ELSE statement example Pseudocode notice indentation If speed is greater than 65 mph print Over speed limit else print Within speed limit C code if speed 65 printf Over speed limit n else printf Within limit n Relational Operators Operations Important for constructing the decision expression Practice left to right Function name right to left left to right type sizeof right to left 5 7 result is 5 7 result is 7 7 result is 8 7 result is 5 5 result is 5 7 result is 5 0 5 result is 6 5 result is Associativity left to right left to right left to right left to right left to right left to right left to right left to right left to right left to right left to right right to left right to left left to right Adapted from H Cheng chap04 ppt slide 5 Compound Condition Logical operators for more complex decisions Logical AND operator double ampersand if switch1 0 switch2 1 turn on the motor The condition evaluates to TRUE if and only if BOTH expressions on either side of evaluate to TRUE 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 Logical operators for more complex decisions cont Logical OR operator double vertical bar if switch1 0 switch2 1 turn on the motor The condition evaluates to TRUE if one or the other or both expressions on either side of evaluate to TRUE 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 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 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 3rd ed p 64 Nesting If else C Code Two Ways Or Adapted from Deitel Deitel C How to Program 3rd ed p 64 SWITCH Good when faced with testing multiple alternatives that depend on a single variable The test is done once Must be an integral expression int or char NOT float double case items must be constant integral expressions No variables The structure is very organized and readable SWITCH Flowchart Adapted from Deitel Deitel C How to Program 6th ed p 111 Practice 1 Pair up with someone next to you that you do not know Develop an algorithm for 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 Develop an algorithm for the ignition control in a car Requirements The starter will only start when Key must be in the ignition slot Transmission selector must be in Park Key must be turned to Start position The starter is energized with the statement starter on Review References Darnell P A Margolis P E 1996 C a software engineering approach 3rd ed Springer New York Cheng H H 2010 C for Engineers
View Full Document